diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-05-21 01:38:48 +0200 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-05-21 01:41:35 +0200 |
commit | 8e7a87d299483fce6af3be89e50deae43055a96f (patch) | |
tree | 3d5b2e2ca99ad1705ce09d8620bbcf0f9106ee22 /actionpack | |
parent | 205cfe2163d9eb6ee801a23f550e960136b5680e (diff) | |
download | rails-8e7a87d299483fce6af3be89e50deae43055a96f.tar.gz rails-8e7a87d299483fce6af3be89e50deae43055a96f.tar.bz2 rails-8e7a87d299483fce6af3be89e50deae43055a96f.zip |
Make ActionController::Flash work with new_base
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/Rakefile | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/base/chained/flash.rb | 74 | ||||
-rw-r--r-- | actionpack/lib/action_controller/new_base.rb | 5 | ||||
-rw-r--r-- | actionpack/lib/action_controller/new_base/base.rb | 3 | ||||
-rw-r--r-- | actionpack/lib/action_controller/new_base/http.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/new_base/session.rb | 11 | ||||
-rw-r--r-- | actionpack/test/controller/flash_test.rb | 49 |
7 files changed, 97 insertions, 49 deletions
diff --git a/actionpack/Rakefile b/actionpack/Rakefile index af14e592c0..2a456ebb05 100644 --- a/actionpack/Rakefile +++ b/actionpack/Rakefile @@ -61,7 +61,7 @@ Rake::TestTask.new(:test_new_base_on_old_tests) do |t| # content_type mime_responds layout t.test_files = %w( addresses_render base benchmark caching capture dispatcher record_identifier - redirect render rescue url_rewriter webservice + redirect render rescue url_rewriter webservice flash ).map { |name| "test/controller/#{name}_test.rb" } end diff --git a/actionpack/lib/action_controller/base/chained/flash.rb b/actionpack/lib/action_controller/base/chained/flash.rb index 56ee9c67e2..6bd482d85a 100644 --- a/actionpack/lib/action_controller/base/chained/flash.rb +++ b/actionpack/lib/action_controller/base/chained/flash.rb @@ -26,9 +26,18 @@ module ActionController #:nodoc: # # See docs on the FlashHash class for more details about the flash. module Flash - def self.included(base) - base.class_eval do - include InstanceMethods + extend ActiveSupport::DependencyModule + + # TODO : Remove the defined? check when new base is the main base + depends_on Session if defined?(ActionController::Http) + + included do + # TODO : Remove the defined? check when new base is the main base + if defined?(ActionController::Http) + include InstanceMethodsForNewBase + else + include InstanceMethodsForBase + alias_method_chain :perform_action, :flash alias_method_chain :reset_session, :flash end @@ -135,29 +144,50 @@ module ActionController #:nodoc: end end - module InstanceMethods #:nodoc: + module InstanceMethodsForBase #:nodoc: protected - def perform_action_with_flash - perform_action_without_flash - remove_instance_variable(:@_flash) if defined? @_flash - end - def reset_session_with_flash - reset_session_without_flash - remove_instance_variable(:@_flash) if defined? @_flash - end + def perform_action_with_flash + perform_action_without_flash + remove_instance_variable(:@_flash) if defined?(@_flash) + end - # Access the contents of the flash. Use <tt>flash["notice"]</tt> to - # read a notice you put there or <tt>flash["notice"] = "hello"</tt> - # to put a new one. - def flash #:doc: - unless defined? @_flash - @_flash = session["flash"] ||= FlashHash.new - @_flash.sweep - end + def reset_session_with_flash + reset_session_without_flash + remove_instance_variable(:@_flash) if defined?(@_flash) + end + end - @_flash - end + module InstanceMethodsForNewBase #:nodoc: + protected + + def reset_session + super + remove_flash_instance_variable + end + + def process_action(method_name) + super + remove_flash_instance_variable + end + + def remove_flash_instance_variable + remove_instance_variable(:@_flash) if defined?(@_flash) + end + end + + protected + + # Access the contents of the flash. Use <tt>flash["notice"]</tt> to + # read a notice you put there or <tt>flash["notice"] = "hello"</tt> + # to put a new one. + def flash #:doc: + unless defined?(@_flash) + @_flash = session["flash"] ||= FlashHash.new + @_flash.sweep + end + + @_flash end end end diff --git a/actionpack/lib/action_controller/new_base.rb b/actionpack/lib/action_controller/new_base.rb index 3fc5d82d01..d2f6514ff9 100644 --- a/actionpack/lib/action_controller/new_base.rb +++ b/actionpack/lib/action_controller/new_base.rb @@ -10,7 +10,8 @@ module ActionController autoload :Rescue, "action_controller/new_base/rescuable" autoload :Testing, "action_controller/new_base/testing" autoload :UrlFor, "action_controller/new_base/url_for" - + autoload :Session, "action_controller/new_base/session" + # Ported modules # require 'action_controller/routing' autoload :Caching, 'action_controller/caching' @@ -23,6 +24,8 @@ module ActionController autoload :TestCase, 'action_controller/testing/test_case' autoload :UrlRewriter, 'action_controller/routing/generation/url_rewriter' autoload :UrlWriter, 'action_controller/routing/generation/url_rewriter' + + autoload :Flash, 'action_controller/base/chained/flash' require 'action_controller/routing' end diff --git a/actionpack/lib/action_controller/new_base/base.rb b/actionpack/lib/action_controller/new_base/base.rb index 3d8d46c9c2..eff7297973 100644 --- a/actionpack/lib/action_controller/new_base/base.rb +++ b/actionpack/lib/action_controller/new_base/base.rb @@ -14,6 +14,9 @@ module ActionController include ActionController::Layouts include ActionController::ConditionalGet + include ActionController::Session + include ActionController::Flash + # Legacy modules include SessionManagement include ActionDispatch::StatusCodes diff --git a/actionpack/lib/action_controller/new_base/http.rb b/actionpack/lib/action_controller/new_base/http.rb index 17466a2d52..2525e221a6 100644 --- a/actionpack/lib/action_controller/new_base/http.rb +++ b/actionpack/lib/action_controller/new_base/http.rb @@ -37,7 +37,7 @@ module ActionController end delegate :headers, :to => "@_response" - + def params @_params ||= @_request.parameters end diff --git a/actionpack/lib/action_controller/new_base/session.rb b/actionpack/lib/action_controller/new_base/session.rb new file mode 100644 index 0000000000..a8715555fb --- /dev/null +++ b/actionpack/lib/action_controller/new_base/session.rb @@ -0,0 +1,11 @@ +module ActionController + module Session + def session + @_request.session + end + + def reset_session + @_request.reset_session + end + end +end diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb index ef60cae0ff..84e27d7779 100644 --- a/actionpack/test/controller/flash_test.rb +++ b/actionpack/test/controller/flash_test.rb @@ -60,6 +60,7 @@ class FlashTest < ActionController::TestCase def std_action @flash_copy = {}.update(flash) + render :nothing => true end def filter_halting_action @@ -79,64 +80,64 @@ class FlashTest < ActionController::TestCase get :set_flash get :use_flash - assert_equal "hello", @controller.template.assigns["flash_copy"]["that"] - assert_equal "hello", @controller.template.assigns["flashy"] + assert_equal "hello", assigns["flash_copy"]["that"] + assert_equal "hello", assigns["flashy"] get :use_flash - assert_nil @controller.template.assigns["flash_copy"]["that"], "On second flash" + assert_nil assigns["flash_copy"]["that"], "On second flash" end def test_keep_flash get :set_flash get :use_flash_and_keep_it - assert_equal "hello", @controller.template.assigns["flash_copy"]["that"] - assert_equal "hello", @controller.template.assigns["flashy"] + assert_equal "hello", assigns["flash_copy"]["that"] + assert_equal "hello", assigns["flashy"] get :use_flash - assert_equal "hello", @controller.template.assigns["flash_copy"]["that"], "On second flash" + assert_equal "hello", assigns["flash_copy"]["that"], "On second flash" get :use_flash - assert_nil @controller.template.assigns["flash_copy"]["that"], "On third flash" + assert_nil assigns["flash_copy"]["that"], "On third flash" end def test_flash_now get :set_flash_now - assert_equal "hello", @controller.template.assigns["flash_copy"]["that"] - assert_equal "bar" , @controller.template.assigns["flash_copy"]["foo"] - assert_equal "hello", @controller.template.assigns["flashy"] + assert_equal "hello", assigns["flash_copy"]["that"] + assert_equal "bar" , assigns["flash_copy"]["foo"] + assert_equal "hello", assigns["flashy"] get :attempt_to_use_flash_now - assert_nil @controller.template.assigns["flash_copy"]["that"] - assert_nil @controller.template.assigns["flash_copy"]["foo"] - assert_nil @controller.template.assigns["flashy"] + assert_nil assigns["flash_copy"]["that"] + assert_nil assigns["flash_copy"]["foo"] + assert_nil assigns["flashy"] end def test_update_flash get :set_flash get :use_flash_and_update_it - assert_equal "hello", @controller.template.assigns["flash_copy"]["that"] - assert_equal "hello again", @controller.template.assigns["flash_copy"]["this"] + assert_equal "hello", assigns["flash_copy"]["that"] + assert_equal "hello again", assigns["flash_copy"]["this"] get :use_flash - assert_nil @controller.template.assigns["flash_copy"]["that"], "On second flash" - assert_equal "hello again", @controller.template.assigns["flash_copy"]["this"], "On second flash" + assert_nil assigns["flash_copy"]["that"], "On second flash" + assert_equal "hello again", assigns["flash_copy"]["this"], "On second flash" end def test_flash_after_reset_session get :use_flash_after_reset_session - assert_equal "hello", @controller.template.assigns["flashy_that"] - assert_equal "good-bye", @controller.template.assigns["flashy_this"] - assert_nil @controller.template.assigns["flashy_that_reset"] + assert_equal "hello", assigns["flashy_that"] + assert_equal "good-bye", assigns["flashy_this"] + assert_nil assigns["flashy_that_reset"] end def test_sweep_after_halted_filter_chain get :std_action - assert_nil @controller.template.assigns["flash_copy"]["foo"] + assert_nil assigns["flash_copy"]["foo"] get :filter_halting_action - assert_equal "bar", @controller.template.assigns["flash_copy"]["foo"] + assert_equal "bar", assigns["flash_copy"]["foo"] get :std_action # follow redirection - assert_equal "bar", @controller.template.assigns["flash_copy"]["foo"] + assert_equal "bar", assigns["flash_copy"]["foo"] get :std_action - assert_nil @controller.template.assigns["flash_copy"]["foo"] + assert_nil assigns["flash_copy"]["foo"] end end |