Class: Bootstrap5Helper::Accordion

Inherits:
Component
  • Object
show all
Defined in:
lib/bootstrap5_helper/accordion.rb,
lib/bootstrap5_helper/accordion/item.rb

Overview

Builds a Accordion component.

Defined Under Namespace

Classes: Item

Instance Method Summary collapse

Methods inherited from Component

#capture, #concat, #config, #content_tag, #parse_arguments, #parse_context_or_options, #parse_tag_or_options, #parse_text_or_options, #uuid

Constructor Details

#initialize(template, opts = {}, &block) ⇒ Accordion

Class constructor

Parameters:

  • template (ActionView)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)
  • :always_open (Boolean)
  • :flush (Boolean)


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bootstrap5_helper/accordion.rb', line 16

def initialize(template, opts = {}, &block)
  super(template)

  @attrs       = opts
  @id          = @attrs.delete(:id)          { uuid }
  @class       = @attrs.delete(:class)       { '' }
  @data        = @attrs.delete(:data)        { {} }
  @always_open = @attrs.delete(:always_open) { false }
  @flush       = @attrs.delete(:flush)       { false }
  @content     = block || proc { '' }
end

Instance Method Details

#item(opts = {}, &block) ⇒ Accodion::Item

Used to generate a Accordion::Item component.

Returns:

  • (Accodion::Item)


32
33
34
# File 'lib/bootstrap5_helper/accordion.rb', line 32

def item(opts = {}, &block)
  Accordion::Item.new(self, (@always_open ? nil : @id), opts, &block)
end

#to_sString

String representation of the object.

Returns:

  • (String)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bootstrap5_helper/accordion.rb', line 40

def to_s
  (
    :div,
    {
      id:    @id,
      class: "accordion #{@flush ? 'accordion-flush' : ''} #{@class}",
      data:  @data
    }.merge(@attrs)
  ) do
    @content.call(self)
  end
end