Class: Bootstrap3Helper::AccordionGroup

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

Overview

This class is used to general groups of accordions.

Examples:

Rendering out an Accordion Group in template:

<code>
  <%= accordion_group_helper do |group| %>
    <%= group.accordion :primary do |accordion| %>
        <%= accordion.header { "accordion 1" } %>
        <%= accordion.body do %>
            <p>This is accordion 1</p>
        <% end %>
    <% end %>
    <%= group.accordion :info do |accordion| %>
        <%= accordion.header { "accordion 2" } %>
        <%= accordion.body do %>
            <p>This is accordion 2</p>
        <% end %>
    <% end %>
    <%= group.accordion :danger do |accordion| %>
        <%= accordion.header { "accordion 3" } %>
        <%= accordion.body do %>
            <p>This is accordion 3</p>
        <% end %>
    <% end %>
  <% end %>
</code>

Instance Method Summary collapse

Methods inherited from Component

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

Constructor Details

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

Used to initialize the main object. This objects sole purpose is to generate a wrapper element with a distinct id and pass that id down to all the child elements.

Parameters:

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

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)


39
40
41
42
43
44
45
46
# File 'lib/bootstrap3_helper/accordion_group.rb', line 39

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

  @id      = opts.fetch(:id,    uuid)
  @class   = opts.fetch(:class, '')
  @data    = opts.fetch(:data,  {})
  @content = block || proc { '' }
end

Instance Method Details

#accordion(context_or_options = nil, opts = {}, &block) ⇒ Object

This method is the main method for generating individual accordions. This is where you would pass in the html attributes.

Parameters:

  • context_or_options (NilClass|String|Symbol|Hash) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)

Yield Returns:



57
58
59
60
61
62
63
64
65
# File 'lib/bootstrap3_helper/accordion_group.rb', line 57

def accordion(context_or_options = nil, opts = {}, &block)
  if context_or_options.is_a?(Hash)
    context_or_options[:parent_id] = @id
  else
    opts[:parent_id] = @id
  end

  Accordion.new(@template, context_or_options, opts, &block)
end

#to_sString

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

Returns:

  • (String)


73
74
75
76
77
# File 'lib/bootstrap3_helper/accordion_group.rb', line 73

def to_s
   :div, id: @id, class: "panel-group #{@class}" do
    @content.call(self)
  end
end