Class: Bootstrap5Helper::Alert

Inherits:
Component show all
Defined in:
lib/bootstrap5_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

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

Constructor Details

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

Class constructor

Parameters:

  • template (Class)
    • Template in which your are binding too.
  • context_or_options (NilClass|String|Symbol|Hash) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :dismissible (Boolean)


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bootstrap5_helper/alert.rb', line 16

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

  @attrs       = args
  @id          = @attrs.delete(:id)          { uuid }
  @class       = @attrs.delete(:class)       { '' }
  @data        = @attrs.delete(:data)        { {} }
  @dismissible = @attrs.delete(:dismissible) { false }
  @content     = block || proc { '' }
end

Instance Method Details

#close_buttonString

The dissmiss button, if the element has one.

Returns:

  • (String)


32
33
34
35
36
37
38
39
40
# File 'lib/bootstrap5_helper/alert.rb', line 32

def close_button
  (
    :button,
    '',
    class: 'btn-close',
    data:  { 'bs-dismiss' => 'alert' },
    aria:  { label: 'Close' }
  )
end

#to_sString

Used to render out the Alert component.

Returns:

  • (String)


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bootstrap5_helper/alert.rb', line 46

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