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(error success info block)

Instance Method Summary collapse

Instance Method Details

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

Returns html for alert

Options Hash (options):

  • :heading (String)

    if present, include a heading in the alert

  • :close (Boolean)

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



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

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 
  
  (:div, options) do
    alert_close(show_close) + 
    alert_heading(heading) + 
    body
  end
end

#alert_close(show = true) ⇒ String

Return an alert box close button



49
50
51
52
# File 'app/helpers/bootstrap/alert_helper.rb', line 49

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



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

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