Module: Noventius::AlertsHelper

Defined in:
app/helpers/noventius/alerts_helper.rb

Constant Summary collapse

ALERT_TYPES =
[:success, :info, :warning, :danger]

Instance Method Summary collapse

Instance Method Details

#flash_alert(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/noventius/alerts_helper.rb', line 7

def flash_alert(options = {})
  flash_messages = []
  flash.each do |type, message|
    # Skip empty messages, e.g. for devise messages set to nothing in a locale file.
    next if message.blank?

    type = transform_type(type)

    next unless ALERT_TYPES.include?(type)

    tag_options = options_for_type(options, type)

    close_button = (:button,           raw('×'),
                               type:              'button',
                               class:             'close',
                               'data-dismiss' =>  'alert')

    Array(message).each do |msg|
      text = (:div, close_button + msg, tag_options)
      flash_messages << text if msg
    end
  end
  flash_messages.join("\n").html_safe
end

#options_for_type(options, type) ⇒ Object



40
41
42
43
44
# File 'app/helpers/noventius/alerts_helper.rb', line 40

def options_for_type(options, type)
  tag_class = options.extract!(:class)[:class]

  { class: "alert fade in alert-#{type} #{tag_class}" }.merge(options)
end

#transform_type(type) ⇒ Object



32
33
34
35
36
37
38
# File 'app/helpers/noventius/alerts_helper.rb', line 32

def transform_type(type)
  case type
  when :notice then :success
  when :alert, :error then :danger
  else type
  end
end