Class: Bs5::ListGroupComponent::Item

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/bs5/list_group_component.rb

Constant Summary collapse

CLASS_NAME_BASE =
'list-group-item'
CLASS_NAME_ACTION =
"#{CLASS_NAME_BASE}-action"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Item

Returns a new instance of Item.



60
61
62
63
64
65
66
67
68
# File 'app/components/bs5/list_group_component.rb', line 60

def initialize(options = {})
  @options = options.symbolize_keys

  @active = @options.delete(:active) || false
  @disabled = @options.delete(:disabled) || false
  @color = @options.delete(:color)

  set_attributes
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



58
59
60
# File 'app/components/bs5/list_group_component.rb', line 58

def options
  @options
end

Instance Method Details

#actionable?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/components/bs5/list_group_component.rb', line 74

def actionable?
  !!actionable_element
end

#actionable_elementObject



78
79
80
81
82
83
84
85
# File 'app/components/bs5/list_group_component.rb', line 78

def actionable_element
  @actionable_element ||= begin
    if (elements = Nokogiri::HTML::DocumentFragment.parse(content).elements).one? &&
       (element = elements.first).name.in?(%w[a button label])
      element
    end
  end
end

#callObject



70
71
72
# File 'app/components/bs5/list_group_component.rb', line 70

def call
  actionable? ? decorated_content : content
end

#decorated_contentObject



87
88
89
90
# File 'app/components/bs5/list_group_component.rb', line 87

def decorated_content
  set_actionable_element_attributes
  actionable_element.to_html.html_safe # rubocop:disable Rails/OutputSafety
end