Class: Bootstrap4Helper::Accordion
- Defined in:
- lib/bootstrap4_helper/accordion.rb
Overview
Builds a Accordion out of the collapse component of Bootstrap 4.
Constant Summary collapse
- CARD_METHODS =
%i[ title text image_overlay footer ].freeze
Instance Method Summary collapse
-
#body(opts = {}, &block) ⇒ String
Builds the body component for the accordion, which is actually the body of a Card.
-
#header(opts = {}, &block) ⇒ String
Builds a header component for the accordion, which is actually the header of a Card.
-
#initialize(template, opts = {}, &block) ⇒ Accordion
constructor
Class constructor.
-
#method_missing(method, *args, &block) ⇒ Object
Because Accordions are basically ‘Cards` with a wrapper, we might as well catch common `Card` methods and send them to the card object.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Checks if the Object reponds to missing.
-
#to_s ⇒ String
Leaving off the default ‘accordion` class because with only 1 accordion, there is no bottom.
Methods inherited from Component
#capture, #concat, #config, #content_tag, #parse_arguments, #uuid
Constructor Details
#initialize(template, opts = {}, &block) ⇒ Accordion
Class constructor
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bootstrap4_helper/accordion.rb', line 24 def initialize(template, opts = {}, &block) super(template) @id = opts.fetch(:id, uuid) @class = opts.fetch(:class, '') @data = opts.fetch(:data, {}) @parent = opts.fetch(:parent, nil) @expanded = opts.fetch(:expanded, false) @target = @data.fetch(:target, uuid) @content = block || proc { '' } @card = Card.new(@template) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Because Accordions are basically ‘Cards` with a wrapper, we might as well catch common `Card` methods and send them to the card object. No point in creating similar methods for `Accordions`.
81 82 83 84 85 86 87 |
# File 'lib/bootstrap4_helper/accordion.rb', line 81 def method_missing(method, *args, &block) if CARD_METHODS.include?(method) @card.send(method, *args, &block) else super end end |
Instance Method Details
#body(opts = {}, &block) ⇒ String
The ‘@parent` gets used to set the parent element for the accordion. This gets used primarily in the `AccordionGroup`.
Builds the body component for the accordion, which is actually the body of a Card.
64 65 66 67 68 69 70 71 72 |
# File 'lib/bootstrap4_helper/accordion.rb', line 64 def body(opts = {}, &block) data = { parent: "##{@parent}" } if @parent.present? klass = 'collapse' klass += ' show' if @expanded content_tag :div, id: @target, class: klass, data: data do @card.body(opts, &block) end end |
#header(opts = {}, &block) ⇒ String
Builds a header component for the accordion, which is actually the header of a Card.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bootstrap4_helper/accordion.rb', line 43 def header(opts = {}, &block) @card.header(opts) do content_tag(config(:accordion_header, :h5)) do content_tag( :a, data: { toggle: 'collapse', target: "##{@target}" }, &block ) end end end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Checks if the Object reponds to missing.
92 93 94 |
# File 'lib/bootstrap4_helper/accordion.rb', line 92 def respond_to_missing?(method, include_private = false) CARD_METHODS.include?(method) || super end |
#to_s ⇒ String
Leaving off the default ‘accordion` class because with only 1 accordion, there is no bottom.
101 102 103 104 105 |
# File 'lib/bootstrap4_helper/accordion.rb', line 101 def to_s content_tag :div, id: @id, class: "card #{@class}", data: @data.except(:target) do @content.call(self) end end |