Flushing Flash

To provide helper methods for handling rails flash messages.

Installation

Rails 3.0

Put the following into the Gemfile and run ‘bundle install`

gem 'flushing_flash'

Usage

1. To push a flash message

push_flash(message_type, *args)

examples:

1.1. push_flash(:success)
  { message_type: :success, content: I18n.t("flashes.#{controller_name}.#{action_name}.success", {}) }

1.2. push_flash(:failure, "User is not authenticated!") # note that the 2nd param is a **String**!
  { message_type: :failure, content: "User is not authenticated!" }

1.3. push_flash(:failure, :"users.authenticate") # note that the 2nd param is a **Symbol**!
  { message_type: :failure, content: I18n.t("flashes.users.authenticate.failure", {}) }

1.4. push_flash(:success, name: "Peter")
  { message_type: :success, content: I18n.t("flashes.#{controller_name}.#{action_name}.success", name: "Peter") }

1.5. push_flash(:failure, :"users.authenticate", name: "Peter")
  { message_type: :success, content: I18nt("flashes.users.authenticate.failure", name: "Peter") }

1.6. push_flash(:success, name: "Peter", target: :signin_form)
  the target will not be passed to the I18n.t! Instead, it is a keyword for identifying the target receving the message.
  see the (2) part.

2. To pull flash messages from a target

pull_flash(target=:default)

examples:

2.1. pull_flash # pull from :default target
  [{message_type: ..., content: ...}, { message_type: ..., content: ...}, ...]

2.2. pull_flash(:signin_form)
  [{message_type: ..., content: ...}, { message_type: ..., content: ...}, ...]

3. To check if a target has flash messages or not

has_flash?(target=:default)

examples:

3.1. has_flash?
  true / false

3.2. has_flash?(:signin_form)
  true / false

4. To return currently hard-coded view for the messages in the target

*** TODO: provide a better way to customize the html output ***

flush_flash(target=:default)

examples:

4.1. flush_flash
  <div class="alert-message #{message_type} fade in">
    <a href="#" class="close">x</a>
    <p>#{content.html_safe}</p>
  </div>

4.2. flush_flash(:signin_form)
  <div class="alert-message #{message_type} fade in">
    <a href="#" class="close">x</a>
    <p>#{content.html_safe}</p>
  </div>

5. To use self-defined partial for rendering the flash messages

flush_flash(target=:default, options={})

examples:

5.1 flush_flash(using: "shared/flash_messages")
  HTML output for the partial app/views/shared/_flash_messages.html.erb

Change Logs

0.2.3

  1. fixed error if passing ‘:using` to pull `flush_flash` method. Now you could make use of `:using` option to use your own partial for rendering the flash messages.