mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make ActionController::Flash work with new_base
This commit is contained in:
parent
205cfe2163
commit
8e7a87d299
7 changed files with 97 additions and 49 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -37,7 +37,7 @@ module ActionController
|
|||
end
|
||||
|
||||
delegate :headers, :to => "@_response"
|
||||
|
||||
|
||||
def params
|
||||
@_params ||= @_request.parameters
|
||||
end
|
||||
|
|
11
actionpack/lib/action_controller/new_base/session.rb
Normal file
11
actionpack/lib/action_controller/new_base/session.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
module ActionController
|
||||
module Session
|
||||
def session
|
||||
@_request.session
|
||||
end
|
||||
|
||||
def reset_session
|
||||
@_request.reset_session
|
||||
end
|
||||
end
|
||||
end
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue