Class: Bootstrap3Helper::Accordion

Inherits:
Component
  • Object
show all
Defined in:
lib/bootstrap3_helper/accordion.rb

Overview

Used to generate Bootstrap Accordion objects.

Examples:

Render out an Accordion component in a template:

<code>
  <%= accordion_helper :primary do |accordion| %>
      <%= accordion.header do %>
          <span class="something">This is the heading....</span>
      <% end %>
      <%= accordion.body do %>
          <p>This is the body of the accordion....</p>
      <% end %>
  <% end %>
</code>

Instance Method Summary collapse

Methods inherited from Component

#concat, #content_tag, #parse_arguments, #uuid

Constructor Details

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

Initlize a new accordion object. If this part of a parent element, i.e AccordionGroup, we need to keep track of the parent element id, so we can pass it down to the other components.

Parameters:

  • template (Class)
    • Template in which your are binding too.

  • - (NilClass|String|Symbol|Hash)

    Bootstrap class context, or options hash.

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

Options Hash (opts):

  • :parent_id (String)

    The parent element ID if this accordion is part of a group.

  • :id (String)

    The ID of the element

  • :class (String)

    Custom class for the component.

  • :collapse_id (String)

    The ID of the element to collapse when clicked.

  • :expanded (Boolean)

    Initial state of the accordion.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bootstrap3_helper/accordion.rb', line 31

def initialize(template, context_or_options = nil, opts = {}, &block)
  super(template)
  @context, args = parse_arguments(context_or_options, opts)

  @parent_id   = args.fetch(:parent_id, nil)
  @id          = args.fetch(:id, nil)
  @class       = args.fetch(:class, '')
  @collapse_id = args.fetch(:collapse_id, uuid)
  @expanded    = args.fetch(:expanded, false)
  @content     = block || proc { '' }
  @panel       = Panel.new(@template, context_or_options, opts)
end

Instance Method Details

#body(args = {}, &block) ⇒ Object

Note:

NilClass :to_s returns an empty String

Creates the body element for the accordion.

Parameters:

  • opts (Hash)

    a customizable set of options

Yield Returns:

  • (String)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bootstrap3_helper/accordion.rb', line 86

def body(args = {}, &block)
  klass = 'panel-collapse collapse '
  klass += ' in' if @expanded

  (
    :div,
    id:    @collapse_id,
    role:  'tabpanel',
    class: klass,
    aria:  { labelledby: "##{@collapse_id}" }
  ) do
    @panel.body(args, &block)
  end
end

Creates the footer element for the accordion

Parameters:

  • opts (Hash)

    a customizable set of options

Yield Returns:

  • (String)


110
111
112
# File 'lib/bootstrap3_helper/accordion.rb', line 110

def footer(args = {}, &block)
  @panel.footer(args, &block)
end

#header(args = {}, &block) ⇒ Object

Note:

NilClass :to_s returns an empty String

Creates the header element for the accordion

Parameters:

  • opts (Hash)

    a customizable set of options

Yield Returns:

  • (String)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bootstrap3_helper/accordion.rb', line 56

def header(args = {}, &block)
  data          = args.fetch(:data, {})
  data[:toggle] = 'collapse'
  data[:parent] = "##{@parent_id}" if @parent_id.present?

  @panel.header(args) do
    (
      :a,
      href: "##{@collapse_id}",
      role: 'button',
      data: data,
      aria: { expanded: @expanded, controls: "##{@collapse_id}" },
      &block
    )
  end
end

#to_sString

The to string method here is what is responsible for rendering out the accordion element. As long as the main method is rendered out in the helper, you will get the entire accordion.

Returns:

  • (String)


120
121
122
123
124
# File 'lib/bootstrap3_helper/accordion.rb', line 120

def to_s
  content =  :div, id: @id, class: container_classes do
    @content.call(self)
  end
end