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



72
73
74
75
76
# File 'lib/block_kit/concerns/has_rich_text_elements.rb', line 72

def append(element)
  elements << element

  self
end

#as_jsonObject



78
79
80
# File 'lib/block_kit/concerns/has_rich_text_elements.rb', line 78

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

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



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

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

#initialize(attributes = {}) ⇒ Object



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

def initialize(attributes = {})
  attributes = attributes.with_indifferent_access
  attributes[:elements] ||= []

  super
end


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

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



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

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



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

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



53
54
55
56
57
58
59
60
# File 'lib/block_kit/concerns/has_rich_text_elements.rb', line 53

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