Module: Bootstrap::AlertHelper

Defined in:
app/helpers/bootstrap/alert_helper.rb

Overview

Rails helpers for producing Bootstrap alert boxes.

See: twitter.github.io/bootstrap/components.html#alerts

Examples:

<%= alert('Default alert') %>

<%= alert('Watch out!', :error) %>

<%= alert('This is the body', heading: 'Title') %>

<%= alert :success do %>
  <%= alert_heading('A List') %>
  <ul>
    <li>One</li>
    <li>Two</li>
  </ul>
<% end %>

Constant Summary collapse

ALERT_ATTRIBUTES =
%w(error success info block)

Instance Method Summary collapse

Instance Method Details

#alert(text, alert_type, options = {}) ⇒ String

Returns html for alert

Parameters:

  • text (String)

    text of the label

  • alert_type (Symbol, String)

    if present must be one of ALERT_ATTRIBUTES

  • options (Hash) (defaults to: {})

    unrecognized options become html attributes for returned alert <div>

Options Hash (options):

  • :heading (String)

    if present, include a heading in the alert

  • :close (Boolean)

    if false, don’t include a close link (‘x’)

Returns:

  • (String)

    Returns html for alert



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/bootstrap/alert_helper.rb', line 30

def alert(*args, &block)
  text = args.shift unless block_given?
  options = canonicalize_options(args.extract_options!)
  options = ensure_class(options, 'alert')
  options = add_alert_classes(options, args)
  heading = options.delete(:heading)
  show_close = options.delete(:close) != false 
  
  if block_given?
    (:div, options) do
      alert_close(show_close) + 
      alert_heading(heading) + 
      capture(&block)
    end
  else
    (:div, options) do
      alert_close(show_close) + 
      alert_heading(heading) + 
      text
    end
  end
end

#alert_close(show = true) ⇒ String

Return an alert box close button

Returns:

  • (String)

    html for alert close button



56
57
58
59
# File 'app/helpers/bootstrap/alert_helper.rb', line 56

def alert_close(show=true)
  return '' unless show
  (:button, '&times;'.html_safe, class: 'close', data: {dismiss: 'alert'})
end

#alert_heading(heading) ⇒ String

Return an alert heading

Returns:

  • (String)

    html for alert heading



64
65
66
67
# File 'app/helpers/bootstrap/alert_helper.rb', line 64

def alert_heading(heading)
  return '' unless heading.present?
  (:h4, heading)
end