Class: Bs5::Modal::ControllerComponent

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

Instance Method Summary collapse

Constructor Details

#initialize(modal_id:) ⇒ ControllerComponent

Returns a new instance of ControllerComponent.



6
7
8
# File 'app/components/bs5/modal/controller_component.rb', line 6

def initialize(modal_id:)
  @modal_id = modal_id
end

Instance Method Details

#actionable_elementObject



26
27
28
29
30
31
32
33
# File 'app/components/bs5/modal/controller_component.rb', line 26

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

#actionable_element?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/components/bs5/modal/controller_component.rb', line 35

def actionable_element?
  !!actionable_element
end

#contentObject



10
11
12
13
14
15
16
17
18
19
# File 'app/components/bs5/modal/controller_component.rb', line 10

def content
  return nil if @content.blank?

  if actionable_element?
    set_actionable_element_attributes
    actionable_element.to_html.html_safe # rubocop:disable Rails/OutputSafety
  else
    @content
  end
end

#set_actionable_element_attributesObject



21
22
23
24
# File 'app/components/bs5/modal/controller_component.rb', line 21

def set_actionable_element_attributes
  actionable_element['data-bs-toggle'] = 'modal'
  actionable_element['data-bs-target'] = "##{@modal_id}"
end