Module: Facades::Helpers::Notifications

Included in:
Facades::Helpers
Defined in:
lib/facades/helpers/notifications.rb

Instance Method Summary collapse

Instance Method Details

#flash_messages(attrs = {}) ⇒ Object

Convenience method for outputting all current flash messages. This allows you to avoid using things like “if flash” and so on, which is far from DRY

Examples:

Output any available flash messages, appending a “closer” span

In your controller:
  flash[:success] = "You did something awesome"
In your view or layout
<%= flash_messages :close => "<span>X</span>" %> #=> <div class="flash_message flash_message_success success">You did something awesome <span>X</span></div>

Output a message without a “close”

<%= flash_messages :close => false %> #=> <div class="flash_message flash_message_success success">You did something awesome</div>

Parameters:

  • attrs (Hash) (defaults to: {})

    Options to modify the html attributes and settings

Options Hash (attrs):

  • closer (Symbol)

    Specify HTML to be appended to the message in case you want a “close” button/link. Defaults to “<span>X</span>”

  • wrapper (Symbol)

    Specify the HTML element which wraps each message. Defaults to :div



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/facades/helpers/notifications.rb', line 24

def flash_messages(attrs = {})

  return if flash.nil? or flash.empty?

  wrapper = attrs.delete(:wrapper) || :div
  closer  = attrs.delete(:closer)
  unless closer === false
    closer ||= "<span class='close'>&times;</span>"
  end
  klasses = (attrs.delete(:class) || "").split(" ")
  klasses << "flash-message"  
  content = ""    

  flash.each do |key, value|
    next if value === true or value.to_s == 'true' # fix awkward devise message
    klasses << "notification"
    klasses << "flash-message-#{key.to_s.underscore}"
    msg_attrs = attrs.merge(:class => [key.to_s, klasses].flatten.join(' '))
    content.concat (wrapper, "#{value} #{closer}".html_safe, msg_attrs).html_safe
  end    

  content.html_safe    

end