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

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

Instance Method Details

#close_buttonString

The dissmiss button, if the element has one.

Returns:

  • (String)


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

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)


45
46
47
48
49
50
# File 'lib/bootstrap5_helper/alert.rb', line 45

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