Class: Bootstrap3Helper::Accordion

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

Overview

Used to generate Bootstrap Accordion objects.

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, 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.

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

    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.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bootstrap3_helper/accordion.rb', line 20

def initialize(template, context_or_options = nil, opts = {}, &block)
  super(template)
  @context, args = parse_context_or_options(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(tag_or_options = nil, opts = {}, &block) ⇒ Object

Note:

NilClass :to_s returns an empty String

Creates the body element for the accordion.

Parameters:

  • args (Hash)

Yield Returns:

  • (String)


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

def body(tag_or_options = nil, opts = {}, &block)
  tag, args = parse_tag_or_options(tag_or_options, opts)

  klass = 'panel-collapse collapse '
  klass += ' in' if @expanded

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

Creates the footer element for the accordion

Parameters:

  • args (Hash)

Returns:

  • (String)


112
113
114
115
# File 'lib/bootstrap3_helper/accordion.rb', line 112

def footer(tag_or_options = nil, opts = {}, &block)
  tag, args = parse_tag_or_options(tag_or_options, opts)
  @panel.footer(tag, args.merge!(config_for: :accordions), &block)
end

#header(tag_or_options = nil, opts = {}, &block) ⇒ String

Creates the header element for the accordion

Parameters:

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

Options Hash (opts):

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

Returns:

  • (String)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bootstrap3_helper/accordion.rb', line 43

def header(tag_or_options = nil, opts = {}, &block)
  tag, args = parse_tag_or_options(tag_or_options, opts)

  data          = {}
  data[:toggle] = 'collapse'
  data[:parent] = "##{@parent_id}" if @parent_id.present?
  data[:target] = "##{@collapse_id}"

  @panel.header(tag, args.merge!(config_for: :accordions)) do
    (
      :span,
      role: 'button',
      data: data,
      aria: { expanded: @expanded, controls: "##{@collapse_id}" },
      &block
    )
  end
end

#title(tag_or_options = nil, opts = {}, &block) ⇒ String

Builds a title component for the accordion header.

Parameters:

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

Options Hash (opts):

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

Returns:

  • (String)


72
73
74
75
# File 'lib/bootstrap3_helper/accordion.rb', line 72

def title(tag_or_options = nil, opts = {}, &block)
  tag, args = parse_tag_or_options(tag_or_options, opts)
  @panel.title(tag, args.merge!(config_for: :accordions), &block)
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)


123
124
125
126
127
# File 'lib/bootstrap3_helper/accordion.rb', line 123

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