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 collapse

TYPES =
Type.new(%w[danger alert], 'danger', 'exclamation-sign'),
  Type.new(['success'], 'success', 'ok-sign'),
  Type.new(%w[notice info], 'info', 'info-sign'),
  Type.new(%w[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.

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

Parameters:

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

  • (defaults to: {})

    options to customize the alert

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



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

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) if block_content.present?
    content
  end
end

#render_flash_alert(type) ⇒ Object



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

def render_flash_alert(type)
  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}".html_safe) }.join('')
  return alert(type.css_class, content: wrapped)
end

#render_flash_alertsObject



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

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