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
-
#alert(type, options = {}, &block) ⇒ Object
Generates HTML for a bootstrap alert message, optionally closable.
- #render_flash_alert(type) ⇒ Object
- #render_flash_alerts ⇒ Object
Instance Method Details
#alert(type, options = {}, &block) ⇒ Object
Generates HTML for a bootstrap alert message, optionally closable.
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, = {}, &block) dismissible = .fetch(:dismissible, true) text_content = .fetch(:content, '') css_classes = %W[alert alert-#{type}] css_classes << 'alert-dismissible' if .fetch(:dismissible, true) block_content = capture(&block) if block_given? return content_tag(:div, '', class: css_classes.join(' '), role: 'alert') do content = text_content.html_safe if dismissible = (type: 'button', class: 'close', 'data-dismiss': 'alert') do content_tag(:span, '×', {}, false) end content.concat() 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) = type.flash_keys.map { |key| Array.wrap(flash[key]).compact }.flatten return '' if .blank? wrapped = .map { || content_tag(:p, glyphicon(type.glyphicon) + " #{}".html_safe) }.join('') return alert(type.css_class, content: wrapped) end |
#render_flash_alerts ⇒ Object
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 |