From 89ff9466d79813a672103e1370ff9ca65f8fcc6c Mon Sep 17 00:00:00 2001 From: Michael Raidel Date: Sat, 15 May 2010 17:14:59 +0200 Subject: [PATCH] Flash.now convenience accessors notice and alert (ticket 4369) --- actionpack/lib/action_dispatch/middleware/flash.rb | 57 +++++++++---------- actionpack/test/controller/flash_test.rb | 58 +++++++++++++------- 2 files changed, 65 insertions(+), 50 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb index adde183..2d6db9b 100644 --- a/actionpack/lib/action_dispatch/middleware/flash.rb +++ b/actionpack/lib/action_dispatch/middleware/flash.rb @@ -35,7 +35,31 @@ module ActionDispatch # # See docs on the FlashHash class for more details about the flash. class Flash + module ConvenienceAccessors + # Convenience accessor for flash[:alert] + def alert + self[:alert] + end + + # Convenience accessor for flash[:alert]= + def alert=(message) + self[:alert] = message + end + + # Convenience accessor for flash[:notice] + def notice + self[:notice] + end + + # Convenience accessor for flash[:notice]= + def notice=(message) + self[:notice] = message + end + end + class FlashNow #:nodoc: + include ConvenienceAccessors + def initialize(flash) @flash = flash end @@ -49,19 +73,11 @@ module ActionDispatch def [](k) @flash[k] end - - # Convenience accessor for flash.now[:alert]= - def alert=(message) - self[:alert] = message - end - - # Convenience accessor for flash.now[:notice]= - def notice=(message) - self[:notice] = message - end end class FlashHash < Hash + include ConvenienceAccessors + def initialize #:nodoc: super @used = Set.new @@ -131,26 +147,6 @@ module ActionDispatch (@used - keys).each{ |k| @used.delete(k) } end - # Convenience accessor for flash[:alert] - def alert - self[:alert] - end - - # Convenience accessor for flash[:alert]= - def alert=(message) - self[:alert] = message - end - - # Convenience accessor for flash[:notice] - def notice - self[:notice] - end - - # Convenience accessor for flash[:notice]= - def notice=(message) - self[:notice] = message - end - private # Used internally by the keep and discard methods # use() # marks the entire flash as used @@ -165,6 +161,7 @@ module ActionDispatch end end + def initialize(app) @app = app end diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb index c662ce2..1af9dfa 100644 --- a/actionpack/test/controller/flash_test.rb +++ b/actionpack/test/controller/flash_test.rb @@ -7,6 +7,12 @@ class FlashTest < ActionController::TestCase render :inline => "hello" end + def set_flash_with_convenience_accessors + flash.notice = "foo" + flash.alert = "bar" + render :inline => "hello" + end + def set_flash_now flash.now["that"] = "hello" flash.now["foo"] ||= "bar" @@ -16,6 +22,18 @@ class FlashTest < ActionController::TestCase render :inline => "hello" end + def set_flash_now_with_convenience_accessors + flash.now.notice ||= "foo" + flash.now.notice ||= "err" + flash.now.alert ||= "bar" + flash.now.alert ||= "err" + + @flashy_notice = flash.now[:notice] + @flashy_alert = flash.now[:alert] + @flash_copy = {}.update flash + render :inline => "hello" + end + def attempt_to_use_flash_now @flash_copy = {}.update flash @flashy = flash["that"] @@ -81,16 +99,6 @@ class FlashTest < ActionController::TestCase redirect_to '/somewhere', :notice => "Good luck in the somewheres!" end - def render_with_flash_now_alert - flash.now.alert = "Beware the nowheres now!" - render :inline => "hello" - end - - def render_with_flash_now_notice - flash.now.notice = "Good luck in the somewheres now!" - render :inline => "hello" - end - def redirect_with_other_flashes redirect_to '/wonderland', :flash => { :joyride => "Horses!" } end @@ -109,6 +117,14 @@ class FlashTest < ActionController::TestCase assert_nil assigns["flash_copy"]["that"], "On second flash" end + def test_flash_with_convenience_accessors + get :set_flash_with_convenience_accessors + + get :use_flash + assert_equal "foo", assigns["flash_copy"]["notice"] + assert_equal "bar", assigns["flash_copy"]["alert"] + end + def test_keep_flash get :set_flash @@ -135,6 +151,18 @@ class FlashTest < ActionController::TestCase assert_nil assigns["flashy"] end + def test_flash_now + get :set_flash_now_with_convenience_accessors + assert_equal "foo", assigns["flash_copy"]["notice"] + assert_equal "bar", assigns["flash_copy"]["alert"] + assert_equal "foo", assigns["flashy_notice"] + assert_equal "bar", assigns["flashy_alert"] + + get :attempt_to_use_flash_now + assert_nil assigns["flash_copy"]["notice"] + assert_nil assigns["flash_copy"]["alert"] + end + def test_update_flash get :set_flash get :use_flash_and_update_it @@ -193,16 +221,6 @@ class FlashTest < ActionController::TestCase assert_equal "Good luck in the somewheres!", @controller.send(:flash)[:notice] end - def test_render_with_flash_now_alert - get :render_with_flash_now_alert - assert_equal "Beware the nowheres now!", @controller.send(:flash)[:alert] - end - - def test_render_with_flash_now_notice - get :render_with_flash_now_notice - assert_equal "Good luck in the somewheres now!", @controller.send(:flash)[:notice] - end - def test_redirect_to_with_other_flashes get :redirect_with_other_flashes assert_equal "Horses!", @controller.send(:flash)[:joyride] -- 1.6.3.3