Module: FlashHelper

Defined in:
lib/flash_notifier/flash_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_bootstrap_flash(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/flash_notifier/flash_helper.rb', line 12

def build_bootstrap_flash *args
  options = extract_args args
  dismissible = options.has_key?(:dismiss) && options[:dismiss]
  timeout = options.has_key?(:timeout) ? options[:timeout] : 5000
  alert_klasses = {
      success: 'alert-success',
      error: 'alert-danger',
      alert: 'alert-warning',
      notice: 'alert-info'
  }
  html = ''
  flash.each do |type, message|
    html +=  :div, class: "alert #{alert_klasses[type.to_sym] || type.to_s} alert-dismissible fade in show #{options[:wrapper_class]}", role: 'alert', data: {flash_notifier: 'bootstrap'} do
      inner_html = link_to 'x', '#', class: 'close', data: {dismiss: 'alert'}, aria_label: 'close', title: 'close'
      inner_html +=  :div, message, class: 'text'
      inner_html.html_safe
    end
  end
  dismiss_alert_script = javascript_tag "    setTimeout(function(){\n      $.each($(\"[data-flash-notifier='bootstrap']\"), function(index, alert){\n        $(alert).alert('close');\n      })\n    }, \#{timeout});\n  JS\n  html += dismiss_alert_script if dismissible\n  html.html_safe\nend\n"

#build_flash(*args) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/flash_notifier/flash_helper.rb', line 2

def build_flash *args
  options = extract_args args
  html = append_options_to_dom options
  flash.each do |type, message|
    flash_type = (%w(notice success).include?(type)) ? 'success' : (( %w(alert error).include?(type)) ? 'error' : (%w(warning info).include?(type) ? type : 'info' ))
    html += (:div, '' , data: { flash_notifier: '', flash_type: flash_type, flash_msg: escape_javascript(message) })
  end
  html.html_safe
end