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

InvalidAlertTypeError =
Class.new(StandardError)
ALERT_ATTRIBUTES =
%w(default success info warning danger)
COMPILED_ALERT_CLASSES =
ALERT_ATTRIBUTES.map{|a| "alert-#{a}"}

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/bootstrap/alert_helper.rb', line 32

def alert(*args, &block)
  body = alert_body(args, &block)
  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 
  options = ensure_class(options, 'alert-dismissible') if show_close
  
  (:div, options) do
    alert_close(show_close) + 
    alert_heading(heading) + 
    body
  end
end

#alert_close(show = true) ⇒ String

Return an alert box close button

Returns:

  • (String)

    html for alert close button unless show is false



51
52
53
54
# File 'app/helpers/bootstrap/alert_helper.rb', line 51

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

#alert_heading(heading) ⇒ String

Return an alert heading

Returns:

  • (String)

    html for alert heading unless heading is blank.



59
60
61
62
# File 'app/helpers/bootstrap/alert_helper.rb', line 59

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