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
-
#alert(type, options = {}, &block) ⇒ Object
Generates HTML for a bootstrap alert message, optionally closable.
- #render_flash_alert(type, &block) ⇒ Object
- #render_flash_alerts ⇒ Object
Instance Method Details
#alert(type, options = {}, &block) ⇒ Object
Generates HTML for a bootstrap alert message, optionally closable.
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, = {}, &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) 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) = type.flash_keys.map { |key| Array.wrap(flash[key]).compact }.flatten return '' if .blank? wrapped = .map { || content_tag(:p, glyphicon(type.glyphicon) + " #{}") }.join('') return alert(type.css_class, content: wrapped) end |
#render_flash_alerts ⇒ Object
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 |