Created
March 23, 2011 17:22
-
-
Save lmmendes/883529 to your computer and use it in GitHub Desktop.
Rails flash messages helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flash_messages(options={}) | |
flash_names = [:success, :warning, :error, :information, :tip] | |
html = '' | |
flash_names.each do |flash_type| | |
html += flash_message_for( flash[flash_type], options ) if flash[flash_type] | |
end | |
raw html | |
end | |
def flash_message_for(message, options={}) | |
options = { | |
:type => :tip, | |
:closeable => true, | |
:timeout => 2500 | |
}.merge( options ); | |
id = 'flash_' + rand(Time.now.to_i).to_s | |
html = "<div id='#{ id }' class='message #{ options[:closeable] ? 'closeable' : '' } #{ options[:type].to_s }'>" | |
html += "<p>#{ message }</p>" | |
html += "<div class='close'>x</div>" if options[:closeable] | |
html += "</div>" | |
if options[:closeable] | |
html += "<script type='text/javascript'>" | |
html += "setTimeout( function(){ $('##{ id }').fadeOut(); }, #{ options[:timeout] } )" | |
html += "</script>" | |
end | |
raw html | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment