Module: Kadmin::AlertHelper

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

Overview

Provide helpers for displaying alerts, as well as matching those alerts to different flash keys.

Defined Under Namespace

Classes: Type

Constant Summary

TYPES =
[
  Type.new(['danger', 'alert'], 'danger', 'exclamation-sign'),
  Type.new(['success'], 'success', 'ok-sign'),
  Type.new(['notice', 'info'], 'info', 'info-sign'),
  Type.new(['warn', 'warning'], 'warning', 'question-sign')
].freeze

Instance Method Summary collapse

Instance Method Details

#alert(type, options = {}, &block) ⇒ Object

Generates HTML for a bootstrap alert message, optionally closable.

Parameters:

  • type (String)

    the type of the alert: danger, success, info, warning, or any alert-<TYPE> defined in your CSS

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

    options to customize the alert

  • block (Proc)

    optional block to pass if you want add more complex HTML inside

Options Hash (options):

  • content (String) — default: '') text to show if you're not going to pass a block

    '') text to show if you're not going to pass a block

  • dismissible (Boolean) — default: true

    if true, adds a close button to the alert. Requires JS enabled



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/kadmin/alert_helper.rb', line 12

def alert(type, options = {}, &block)
  dismissible = options.fetch(:dismissible, true)
  text_content = options.fetch(:content, '')

  css_classes = %W(alert alert-#{type})
  css_classes << 'alert-dismissible' if options.fetch(:dismissible, true)
  block_content = capture(&block) if block_given?

  return (:div, '', class: css_classes.join(' '), role: 'alert') do
    content = text_content.html_safe

    if dismissible
      button = button_tag(type: 'button', class: 'close', 'data-dismiss': 'alert') do
        (:span, '&times', {}, false)
      end
      content.concat(button)
    end

    content.concat(block_content.html_safe) unless block_content.blank?
    content
  end
end

#render_flash_alert(type, &block) ⇒ Object



51
52
53
54
55
56
57
# File 'app/helpers/kadmin/alert_helper.rb', line 51

def render_flash_alert(type, &block)
  messages = type.flash_keys.map { |key| Array.wrap(flash[key]).compact }.flatten
  return '' if messages.blank?

  wrapped = messages.map { |message| (:p, glyphicon(type.glyphicon) + " #{message}") }.join('')
  return alert(type.css_class, content: wrapped)
end

#render_flash_alertsObject



42
43
44
45
46
47
48
49
# File 'app/helpers/kadmin/alert_helper.rb', line 42

def render_flash_alerts
  alerts = AlertHelper::TYPES.map do |type|
    next unless type.flash_keys.any? { |key| flash[key].present? }
    render_flash_alert(type)
  end.compact

  return safe_join(alerts)
end