Class: Bootstrap3Helper::Alert

Inherits:
Component show all
Defined in:
lib/bootstrap3_helper/alert.rb

Overview

The Alert helper is meant to help you rapidly build Bootstrap Alert components quickly and easily. The dissmiss button is optional.

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) ⇒ Alert

Used to generate Bootstrap alert components quickly.

Parameters:

  • template (ActionView)

    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):

  • :id (String)

    The ID of the element

  • :class (String)

    Custom class for the component.



15
16
17
18
19
20
21
22
23
# File 'lib/bootstrap3_helper/alert.rb', line 15

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

  @context, args = parse_context_or_options(context_or_options, opts)
  @id            = args.fetch(:id, nil)
  @class         = args.fetch(:class, '')
  @dismissible   = args.fetch(:dismissible, false)
  @content       = block || proc { '' }
end

Instance Method Details

#close_buttonString

The dissmiss button, if the element has one.

Returns:

  • (String)


29
30
31
32
33
34
35
36
37
38
# File 'lib/bootstrap3_helper/alert.rb', line 29

def close_button
  (
    :button,
    class: 'close',
    data:  { dismiss: 'alert' },
    aria:  { label: 'Close' }
  ) do
    (:span, aria: { hidden: true }) { '×'.html_safe }
  end
end

#to_sString

Note:

Concat needs to be used to add the content of the button to the output buffer. For some reason, if though the block returns a String, trying to add that string to the dismiss button string, returns with the dismiss button missing. Was forced to caoncat the button to the current output buffer in order to get it to persist after the block call.

Used to render out the Alert component.

Returns:

  • (String)


50
51
52
53
54
55
# File 'lib/bootstrap3_helper/alert.rb', line 50

def to_s
   :div, id: @id, class: container_class do
    concat(@dismissible ? close_button : '')
    @content.call(self)
  end
end