gbdev / pretty_flash forked from rpheath/pretty_flash

Rails plugin/gem: reusable (and pretty) flash messages.

This URL has Read+Write access

Darren Johnson (author)
Wed Mar 25 13:58:24 -0700 2009
commit  04064d82fea24654af4d10ed5e8efa47ee16044b
tree    a3d5fdb9a1b047003cb3e94168d3a581d29ba9ca
parent  aa213fb990ec0d7d528ff94b7617c6dd936f521c
name age message
file .gitignore Loading commit data...
file CHANGELOG
file MIT-LICENSE Tue Dec 30 09:12:25 -0800 2008 initial commit [rpheath]
file README.textile
file Rakefile Tue Dec 30 09:12:25 -0800 2008 initial commit [rpheath]
file TODO
file init.rb
file install.rb
directory lib/
file pretty_flash.gemspec
directory public/
directory test/ Tue Dec 30 09:12:25 -0800 2008 initial commit [rpheath]
file uninstall.rb Tue Dec 30 09:12:25 -0800 2008 initial commit [rpheath]
README.textile

PrettyFlash

Rails plugin/gem that provides “pretty” css-driven flash messages and additional helpers to pull all errors from ActiveRecord objects and as well as errors on associated models of an ActiveRecord object.

In the controller…

This plugin/gem provides convenience methods for setting flash messages. Use this instead of the typical flash[:notice] = "Some String"


flash_success :message => "Hooray! You did something good."
flash_notice :message => "Hey! This is for tips and general information."
flash_warning :message => "Ummm, You're not allowed to do that yet."
flash_error :message => 'ID does not exist'

You can specify whether to use flash() or flash.now() via the options hash. The default is to use flash.now(). This is needed when setting flash and then redirecting. Also, any AJAX request defaults to flash.now()


flash_error :message => 'Blah', :now => false
redirect_to :list

You can also load validation errors from ActiveRecord Objects and any specified associated models


flash_error :object => @user, :associations => [:roles, :permissions]

The complete list of hash options for flash_success, flash_notice, flash_warning, flash_error is as follows:
:message – the flash message to be added
:now – Whether to use flash or flash.now
:delimiter – separator between messages
:clear_flash – delete flash for the type of flash called (:notice, :error, etc)
:object – an ActiveRecord object with @errors
:associations – model associations of the AR Object that should be checked for errors

In the view…

Then all you have to do to display them is:

<%= display_flash_messages %>

Easy enough?

There are also methods to check if any error, notice, warning or success messages exist.


flash_has_successes?
flash_has_notices?
flash_has_warnings?
flash_has_errors?

Get Started

Install the Gem

sudo gem install gbdev-pretty_flash

Add the gem to your environment.rb file

config.gem 'gbdev-pretty_flash', :lib => 'pretty_flash', :version => '0.1.0', :source => 'http://gems.github.com'

There’s a public folder with this plugin/gem that contains the images, CSS, and javascript (optional). The first time you run your application with the added config.gem line, the images, stylesheet, and javascript will be copied to your public folder.

On subsequent application starts, the plugin/gem checks for the presence of public/images/flash-error.png. If this file is present, nothing will be copied to your public directory.

WARNING: this will overwrite any of the files that have the same name. So double check if you don’t want that to happen.

Also, the pretty_flash.js file located in the public/javascripts folder is only used to fade the flash messages after so many seconds (defaults to 2.5 seconds). If you want that capability, just copy the relevant section of code (prototype or jquery) to application.js and you’re all set.

Credits

Original plugin by Ryan Heath (http://github.com/rpheath/pretty_flash).
Converted to a gem and extensively modified by Great Basin Solutions, LLC.

Licensing

Copyright © 2008 Ryan Heath, released under the MIT license