Module: BlockKit::Concerns::HasRichTextElements

Extended by:
ActiveSupport::Concern
Included in:
Layout::RichText::Preformatted, Layout::RichText::Quote, Layout::RichText::Section
Defined in:
lib/block_kit/concerns/has_rich_text_elements.rb

Instance Method Summary collapse

Instance Method Details

#append(element) ⇒ Object



65
66
67
68
69
# File 'lib/block_kit/concerns/has_rich_text_elements.rb', line 65

def append(element)
  elements << element

  self
end

#as_jsonObject



71
72
73
# File 'lib/block_kit/concerns/has_rich_text_elements.rb', line 71

def as_json(*)
  super.merge(elements: elements&.map(&:as_json)).compact
end

#channel(channel_id:, styles: []) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/block_kit/concerns/has_rich_text_elements.rb', line 19

def channel(channel_id:, styles: [])
  style = if styles.present?
    styles = Array(styles).map { |s| [s.to_s, true] }.to_h
    Layout::RichText::Elements::MentionStyle.new(**styles.slice(*Layout::RichText::Elements::MentionStyle.attribute_names))
  end

  append(Layout::RichText::Elements::Channel.new(channel_id: channel_id, style: style))
end


28
29
30
31
32
33
34
35
# File 'lib/block_kit/concerns/has_rich_text_elements.rb', line 28

def link(url:, text:, unsafe: nil, styles: [])
  style = if styles.present?
    styles = Array(styles).map { |s| [s.to_s, true] }.to_h
    Layout::RichText::Elements::TextStyle.new(**styles.slice(*Layout::RichText::Elements::TextStyle.attribute_names))
  end

  append(Layout::RichText::Elements::Link.new(url: url, text: text, unsafe: unsafe, style: style))
end

#text(text:, styles: []) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/block_kit/concerns/has_rich_text_elements.rb', line 37

def text(text:, styles: [])
  style = if styles.present?
    styles = Array(styles).map { |s| [s.to_s, true] }.to_h
    Layout::RichText::Elements::TextStyle.new(**styles.slice(*Layout::RichText::Elements::TextStyle.attribute_names))
  end

  append(Layout::RichText::Elements::Text.new(text: text, style: style))
end

#user(user_id:, styles: []) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/block_kit/concerns/has_rich_text_elements.rb', line 56

def user(user_id:, styles: [])
  style = if styles.present?
    styles = Array(styles).map { |s| [s.to_s, true] }.to_h
    Layout::RichText::Elements::MentionStyle.new(**styles.slice(*Layout::RichText::Elements::MentionStyle.attribute_names))
  end

  append(Layout::RichText::Elements::User.new(user_id: user_id, style: style))
end

#usergroup(usergroup_id:, styles: []) ⇒ Object Also known as: user_group



46
47
48
49
50
51
52
53
# File 'lib/block_kit/concerns/has_rich_text_elements.rb', line 46

def usergroup(usergroup_id:, styles: [])
  style = if styles.present?
    styles = Array(styles).map { |s| [s.to_s, true] }.to_h
    Layout::RichText::Elements::MentionStyle.new(**styles.slice(*Layout::RichText::Elements::MentionStyle.attribute_names))
  end

  append(Layout::RichText::Elements::Usergroup.new(usergroup_id: usergroup_id, style: style))
end