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
|
# content_type mime_responds layout
|
||||||
t.test_files = %w(
|
t.test_files = %w(
|
||||||
addresses_render base benchmark caching capture dispatcher record_identifier
|
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" }
|
).map { |name| "test/controller/#{name}_test.rb" }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -26,9 +26,18 @@ module ActionController #:nodoc:
|
||||||
#
|
#
|
||||||
# See docs on the FlashHash class for more details about the flash.
|
# See docs on the FlashHash class for more details about the flash.
|
||||||
module Flash
|
module Flash
|
||||||
def self.included(base)
|
extend ActiveSupport::DependencyModule
|
||||||
base.class_eval do
|
|
||||||
include InstanceMethods
|
# 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 :perform_action, :flash
|
||||||
alias_method_chain :reset_session, :flash
|
alias_method_chain :reset_session, :flash
|
||||||
end
|
end
|
||||||
|
@ -135,29 +144,50 @@ module ActionController #:nodoc:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module InstanceMethods #:nodoc:
|
module InstanceMethodsForBase #:nodoc:
|
||||||
protected
|
protected
|
||||||
def perform_action_with_flash
|
|
||||||
perform_action_without_flash
|
|
||||||
remove_instance_variable(:@_flash) if defined? @_flash
|
|
||||||
end
|
|
||||||
|
|
||||||
def reset_session_with_flash
|
def perform_action_with_flash
|
||||||
reset_session_without_flash
|
perform_action_without_flash
|
||||||
remove_instance_variable(:@_flash) if defined? @_flash
|
remove_instance_variable(:@_flash) if defined?(@_flash)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Access the contents of the flash. Use <tt>flash["notice"]</tt> to
|
def reset_session_with_flash
|
||||||
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
|
reset_session_without_flash
|
||||||
# to put a new one.
|
remove_instance_variable(:@_flash) if defined?(@_flash)
|
||||||
def flash #:doc:
|
end
|
||||||
unless defined? @_flash
|
end
|
||||||
@_flash = session["flash"] ||= FlashHash.new
|
|
||||||
@_flash.sweep
|
|
||||||
end
|
|
||||||
|
|
||||||
@_flash
|
module InstanceMethodsForNewBase #:nodoc:
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,7 +10,8 @@ module ActionController
|
||||||
autoload :Rescue, "action_controller/new_base/rescuable"
|
autoload :Rescue, "action_controller/new_base/rescuable"
|
||||||
autoload :Testing, "action_controller/new_base/testing"
|
autoload :Testing, "action_controller/new_base/testing"
|
||||||
autoload :UrlFor, "action_controller/new_base/url_for"
|
autoload :UrlFor, "action_controller/new_base/url_for"
|
||||||
|
autoload :Session, "action_controller/new_base/session"
|
||||||
|
|
||||||
# Ported modules
|
# Ported modules
|
||||||
# require 'action_controller/routing'
|
# require 'action_controller/routing'
|
||||||
autoload :Caching, 'action_controller/caching'
|
autoload :Caching, 'action_controller/caching'
|
||||||
|
@ -23,6 +24,8 @@ module ActionController
|
||||||
autoload :TestCase, 'action_controller/testing/test_case'
|
autoload :TestCase, 'action_controller/testing/test_case'
|
||||||
autoload :UrlRewriter, 'action_controller/routing/generation/url_rewriter'
|
autoload :UrlRewriter, 'action_controller/routing/generation/url_rewriter'
|
||||||
autoload :UrlWriter, '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'
|
require 'action_controller/routing'
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,6 +14,9 @@ module ActionController
|
||||||
include ActionController::Layouts
|
include ActionController::Layouts
|
||||||
include ActionController::ConditionalGet
|
include ActionController::ConditionalGet
|
||||||
|
|
||||||
|
include ActionController::Session
|
||||||
|
include ActionController::Flash
|
||||||
|
|
||||||
# Legacy modules
|
# Legacy modules
|
||||||
include SessionManagement
|
include SessionManagement
|
||||||
include ActionDispatch::StatusCodes
|
include ActionDispatch::StatusCodes
|
||||||
|
|
|
@ -37,7 +37,7 @@ module ActionController
|
||||||
end
|
end
|
||||||
|
|
||||||
delegate :headers, :to => "@_response"
|
delegate :headers, :to => "@_response"
|
||||||
|
|
||||||
def params
|
def params
|
||||||
@_params ||= @_request.parameters
|
@_params ||= @_request.parameters
|
||||||
end
|
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
|
def std_action
|
||||||
@flash_copy = {}.update(flash)
|
@flash_copy = {}.update(flash)
|
||||||
|
render :nothing => true
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_halting_action
|
def filter_halting_action
|
||||||
|
@ -79,64 +80,64 @@ class FlashTest < ActionController::TestCase
|
||||||
get :set_flash
|
get :set_flash
|
||||||
|
|
||||||
get :use_flash
|
get :use_flash
|
||||||
assert_equal "hello", @controller.template.assigns["flash_copy"]["that"]
|
assert_equal "hello", assigns["flash_copy"]["that"]
|
||||||
assert_equal "hello", @controller.template.assigns["flashy"]
|
assert_equal "hello", assigns["flashy"]
|
||||||
|
|
||||||
get :use_flash
|
get :use_flash
|
||||||
assert_nil @controller.template.assigns["flash_copy"]["that"], "On second flash"
|
assert_nil assigns["flash_copy"]["that"], "On second flash"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_keep_flash
|
def test_keep_flash
|
||||||
get :set_flash
|
get :set_flash
|
||||||
|
|
||||||
get :use_flash_and_keep_it
|
get :use_flash_and_keep_it
|
||||||
assert_equal "hello", @controller.template.assigns["flash_copy"]["that"]
|
assert_equal "hello", assigns["flash_copy"]["that"]
|
||||||
assert_equal "hello", @controller.template.assigns["flashy"]
|
assert_equal "hello", assigns["flashy"]
|
||||||
|
|
||||||
get :use_flash
|
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
|
get :use_flash
|
||||||
assert_nil @controller.template.assigns["flash_copy"]["that"], "On third flash"
|
assert_nil assigns["flash_copy"]["that"], "On third flash"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_flash_now
|
def test_flash_now
|
||||||
get :set_flash_now
|
get :set_flash_now
|
||||||
assert_equal "hello", @controller.template.assigns["flash_copy"]["that"]
|
assert_equal "hello", assigns["flash_copy"]["that"]
|
||||||
assert_equal "bar" , @controller.template.assigns["flash_copy"]["foo"]
|
assert_equal "bar" , assigns["flash_copy"]["foo"]
|
||||||
assert_equal "hello", @controller.template.assigns["flashy"]
|
assert_equal "hello", assigns["flashy"]
|
||||||
|
|
||||||
get :attempt_to_use_flash_now
|
get :attempt_to_use_flash_now
|
||||||
assert_nil @controller.template.assigns["flash_copy"]["that"]
|
assert_nil assigns["flash_copy"]["that"]
|
||||||
assert_nil @controller.template.assigns["flash_copy"]["foo"]
|
assert_nil assigns["flash_copy"]["foo"]
|
||||||
assert_nil @controller.template.assigns["flashy"]
|
assert_nil assigns["flashy"]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_update_flash
|
def test_update_flash
|
||||||
get :set_flash
|
get :set_flash
|
||||||
get :use_flash_and_update_it
|
get :use_flash_and_update_it
|
||||||
assert_equal "hello", @controller.template.assigns["flash_copy"]["that"]
|
assert_equal "hello", assigns["flash_copy"]["that"]
|
||||||
assert_equal "hello again", @controller.template.assigns["flash_copy"]["this"]
|
assert_equal "hello again", assigns["flash_copy"]["this"]
|
||||||
get :use_flash
|
get :use_flash
|
||||||
assert_nil @controller.template.assigns["flash_copy"]["that"], "On second flash"
|
assert_nil assigns["flash_copy"]["that"], "On second flash"
|
||||||
assert_equal "hello again", @controller.template.assigns["flash_copy"]["this"], "On second flash"
|
assert_equal "hello again", assigns["flash_copy"]["this"], "On second flash"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_flash_after_reset_session
|
def test_flash_after_reset_session
|
||||||
get :use_flash_after_reset_session
|
get :use_flash_after_reset_session
|
||||||
assert_equal "hello", @controller.template.assigns["flashy_that"]
|
assert_equal "hello", assigns["flashy_that"]
|
||||||
assert_equal "good-bye", @controller.template.assigns["flashy_this"]
|
assert_equal "good-bye", assigns["flashy_this"]
|
||||||
assert_nil @controller.template.assigns["flashy_that_reset"]
|
assert_nil assigns["flashy_that_reset"]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sweep_after_halted_filter_chain
|
def test_sweep_after_halted_filter_chain
|
||||||
get :std_action
|
get :std_action
|
||||||
assert_nil @controller.template.assigns["flash_copy"]["foo"]
|
assert_nil assigns["flash_copy"]["foo"]
|
||||||
get :filter_halting_action
|
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
|
get :std_action # follow redirection
|
||||||
assert_equal "bar", @controller.template.assigns["flash_copy"]["foo"]
|
assert_equal "bar", assigns["flash_copy"]["foo"]
|
||||||
get :std_action
|
get :std_action
|
||||||
assert_nil @controller.template.assigns["flash_copy"]["foo"]
|
assert_nil assigns["flash_copy"]["foo"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue