Module: Responders::FlashResponder
- Defined in:
- lib/responders/flash_responder.rb
Overview
Responder to automatically set flash messages based on I18n API. It checks for message based on the current action, but also allows defaults to be set, using the following order:
flash.controller_name.action_name.status
flash.actions.action_name.status
So, if you have a CarsController, create action, it will check for:
flash.cars.create.status
flash.actions.create.status
The statuses by default are :notice (when the object can be created, updated or destroyed with success) and :alert (when the object cannot be created or updated).
On I18n, the resource_name given is available as interpolation option, this means you can set:
flash:
actions:
create:
notice: "Hooray! %{resource_name} was successfully created!"
But sometimes, flash messages are not that simple. Going back to cars example, you might want to say the brand of the car when it’s updated. Well, that’s easy also:
flash:
cars:
update:
notice: "Hooray! You just tuned your %{car_brand}!"
Since :car_name is not available for interpolation by default, you have to overwrite ‘flash_interpolation_options` in your controller.
def
{ :car_brand => @car.brand }
end
Then you will finally have:
'Hooray! You just tuned your Aston Martin!'
If your controller is namespaced, for example Admin::CarsController, the messages will be checked in the following order:
flash.admin.cars.create.status
flash.admin.actions.create.status
flash.cars.create.status
flash.actions.create.status
You can also have flash messages with embedded HTML. Just create a scope that ends with _html
as the scopes below:
flash.actions.create.notice_html
flash.cars.create.notice_html
Options
FlashResponder also accepts some options through respond_with API.
-
:flash - When set to false, no flash message is set.
respond_with(@user, :flash => true)
-
:notice - Supply the message to be set if the record has no errors.
-
:alert - Supply the message to be set if the record has errors.
respond_with(@user, :notice => "Hooray! Welcome!", :alert => "Woot! You failed.")
-
:flash_now - Sets the flash message using flash.now. Accepts true, :on_failure or :on_success.
Configure status keys
As said previously, FlashResponder by default use :notice and :alert keys. You can change that by setting the status_keys:
Responders::FlashResponder.flash_keys = [ :success, :failure ]
However, the options :notice and :alert to respond_with are kept :notice and :alert.
Class Attribute Summary collapse
-
.flash_keys ⇒ Object
Returns the value of attribute flash_keys.
-
.namespace_lookup ⇒ Object
Returns the value of attribute namespace_lookup.
Instance Method Summary collapse
Class Attribute Details
.flash_keys ⇒ Object
Returns the value of attribute flash_keys.
89 90 91 |
# File 'lib/responders/flash_responder.rb', line 89 def flash_keys @flash_keys end |
.namespace_lookup ⇒ Object
Returns the value of attribute namespace_lookup.
89 90 91 |
# File 'lib/responders/flash_responder.rb', line 89 def namespace_lookup @namespace_lookup end |
Instance Method Details
#initialize(controller, resources, options = {}) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/responders/flash_responder.rb', line 95 def initialize(controller, resources, = {}) super @flash = .delete(:flash) @notice = .delete(:notice) @alert = .delete(:alert) @flash_now = .delete(:flash_now) { :on_failure } end |
#to_html ⇒ Object
103 104 105 106 |
# File 'lib/responders/flash_responder.rb', line 103 def to_html if super end |
#to_js ⇒ Object
108 109 110 111 |
# File 'lib/responders/flash_responder.rb', line 108 def to_js if defined?(super) ? super : to_format end |