From 89ed9fbd1917e431e489dc856042d996d0f088c5 Mon Sep 17 00:00:00 2001 From: Florent Piteau Date: Wed, 20 Apr 2011 02:10:29 +0800 Subject: [PATCH] Don't reuse a closed flash when using now --- actionpack/lib/action_dispatch/middleware/flash.rb | 6 +++++- actionpack/test/controller/flash_test.rb | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb index 735c72d34a..c7f7d4d4f0 100644 --- a/actionpack/lib/action_dispatch/middleware/flash.rb +++ b/actionpack/lib/action_dispatch/middleware/flash.rb @@ -70,6 +70,10 @@ module ActionDispatch def close!(new_flash) @flash = new_flash end + + def closed? + @flash.closed? + end end class FlashHash @@ -146,7 +150,7 @@ module ActionDispatch # # Entries set via now are accessed the same way as standard entries: flash['my-key']. def now - @now ||= FlashNow.new(self) + @now = (!@now || @now.closed?) ? FlashNow.new(self) : @now end attr_reader :closed diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb index 7b5bf8b21a..e19612eace 100644 --- a/actionpack/test/controller/flash_test.rb +++ b/actionpack/test/controller/flash_test.rb @@ -272,6 +272,14 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest end end + def test_setting_flash_now_does_not_raise_in_following_requests + with_test_route_set do + env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new } + get '/set_flash_now', nil, env + get '/set_flash_now', nil, env + end + end + def test_setting_flash_raises_after_stream_back_to_client_even_with_an_empty_flash with_test_route_set do env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new }