Permalink
Browse files

Flash message can be set to now. Just give :flash_now as an option.

  • Loading branch information...
1 parent c99bceb commit f1f2abfa52d6dbbffcba31544ba1579665230696 @josevalim josevalim committed Mar 13, 2010
Showing with 22 additions and 7 deletions.
  1. +14 −6 lib/responders/flash_responder.rb
  2. +8 −1 test/flash_responder_test.rb
View
20 lib/responders/flash_responder.rb
@@ -80,9 +80,10 @@ module FlashResponder
def initialize(controller, resources, options={})
super
- @flash = options.delete(:flash)
- @notice = options.delete(:notice)
- @alert = options.delete(:alert)
+ @flash = options.delete(:flash)
+ @notice = options.delete(:notice)
+ @alert = options.delete(:alert)
+ @flash_now = options.delete(:flash_now)
end
def to_html
@@ -94,18 +95,25 @@ def to_html
def set_flash_message!
if has_errors?
- controller.flash[:alert] ||= @alert if @alert
+ set_flash(:alert, @alert)
status = Responders::FlashResponder.flash_keys.last
else
- controller.flash[:notice] ||= @notice if @notice
+ set_flash(:notice, @notice)
status = Responders::FlashResponder.flash_keys.first
end
return if controller.flash[status].present?
options = mount_i18n_options(status)
message = ::I18n.t options[:default].shift, options
- controller.flash[status] = message unless message.blank?
+ set_flash(status, message)
+ end
+
+ def set_flash(key, value)
+ return if value.blank?
+ flash = controller.flash
+ flash = flash.now if @flash_now
+ flash[key] ||= value
end
def set_flash_message? #:nodoc:
View
9 test/flash_responder_test.rb
@@ -18,7 +18,7 @@ class AddressesController < ApplicationController
self.responder = FlashResponder
def action
- options = params.slice(:flash)
+ options = params.slice(:flash, :flash_now)
flash[:success] = "Flash is set" if params[:set_flash]
respond_with(@resource, options)
end
@@ -107,6 +107,13 @@ def test_sets_flash_message_even_if_block_is_given
assert_equal "Resource with block created with success", flash[:success]
end
+ def test_sets_flash_message_can_be_set_to_now
+ @now = {}
+ @controller.flash.expects(:now).returns(@now)
+ post :create, :flash_now => true
+ assert_equal "Resource created with success", @now[:success]
+ end
+
def test_sets_message_based_on_notice_key
Responders::FlashResponder.flash_keys = [ :notice, :alert ]
post :another

0 comments on commit f1f2abf

Please sign in to comment.