diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-08-07 14:26:21 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-08-07 15:37:31 -0700 |
commit | 83b767cef90abfc4c2ee9f4b451b0215501fae9a (patch) | |
tree | 1655677c718b8f33665c106dd631c42f798d2926 /actionpack | |
parent | 435b224e7ec42194d974a90b89a4562ee876aafc (diff) | |
download | rails-83b767cef90abfc4c2ee9f4b451b0215501fae9a.tar.gz rails-83b767cef90abfc4c2ee9f4b451b0215501fae9a.tar.bz2 rails-83b767cef90abfc4c2ee9f4b451b0215501fae9a.zip |
Using strings or symbols for middleware class names is deprecated.
Convert things like this:
middleware.use "Foo::Bar"
to this:
middleware.use Foo::Bar
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG.md | 9 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal.rb | 3 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/stack.rb | 43 | ||||
-rw-r--r-- | actionpack/test/abstract_unit.rb | 14 | ||||
-rw-r--r-- | actionpack/test/dispatch/middleware_stack/middleware_test.rb | 6 | ||||
-rw-r--r-- | actionpack/test/dispatch/middleware_stack_test.rb | 36 |
6 files changed, 61 insertions, 50 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 0e478abc97..8eea4ccd41 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,12 @@ +* Using strings or symbols for middleware class names is deprecated. Convert + things like this: + + middleware.use "Foo::Bar" + + to this: + + middleware.use Foo::Bar + * ActionController::TestSession now accepts a default value as well as a block for generating a default value based off the key provided. diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb index 4dfd351b95..dab67452ee 100644 --- a/actionpack/lib/action_controller/metal.rb +++ b/actionpack/lib/action_controller/metal.rb @@ -53,8 +53,7 @@ module ActionController list = except end - - Middleware.new(klass, args, list, strategy, block) + Middleware.new(get_class(klass), args, list, strategy, block) end end diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index b39a502d77..62e87a6f29 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -4,25 +4,15 @@ require "active_support/dependencies" module ActionDispatch class MiddlewareStack class Middleware - attr_reader :args, :block, :name, :classcache + attr_reader :args, :block, :klass - def initialize(klass_or_name, args, block) - @klass = nil - - if klass_or_name.respond_to?(:name) - @klass = klass_or_name - @name = @klass.name - else - @name = klass_or_name.to_s - end - - @classcache = ActiveSupport::Dependencies::Reference - @args, @block = args, block + def initialize(klass, args, block) + @klass = klass + @args = args + @block = block end - def klass - @klass || classcache[@name] - end + def name; klass.name; end def ==(middleware) case middleware @@ -31,7 +21,7 @@ module ActionDispatch when Class klass == middleware else - normalize(@name) == normalize(middleware) + normalize(name) == normalize(middleware) end end @@ -123,8 +113,25 @@ module ActionDispatch private + def get_class(klass) + if klass.is_a?(String) || klass.is_a?(Symbol) + classcache = ActiveSupport::Dependencies::Reference + converted_klass = classcache[klass.to_s] + ActiveSupport::Deprecation.warn <<-eowarn +Passing strings or symbols to the middleware builder is deprecated, please change +them to actual class references. For example: + + "#{klass}" => #{converted_klass} + + eowarn + converted_klass + else + klass + end + end + def build_middleware(klass, args, block) - Middleware.new(klass, args, block) + Middleware.new(get_class(klass), args, block) end end end diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index cc610b6d75..771df5a0b2 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -123,13 +123,13 @@ class ActionDispatch::IntegrationTest < ActiveSupport::TestCase def self.build_app(routes = nil) RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware| - middleware.use "ActionDispatch::ShowExceptions", ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public") - middleware.use "ActionDispatch::DebugExceptions" - middleware.use "ActionDispatch::Callbacks" - middleware.use "ActionDispatch::ParamsParser" - middleware.use "ActionDispatch::Cookies" - middleware.use "ActionDispatch::Flash" - middleware.use "Rack::Head" + middleware.use ActionDispatch::ShowExceptions, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public") + middleware.use ActionDispatch::DebugExceptions + middleware.use ActionDispatch::Callbacks + middleware.use ActionDispatch::ParamsParser + middleware.use ActionDispatch::Cookies + middleware.use ActionDispatch::Flash + middleware.use Rack::Head yield(middleware) if block_given? end end diff --git a/actionpack/test/dispatch/middleware_stack/middleware_test.rb b/actionpack/test/dispatch/middleware_stack/middleware_test.rb index 6366d37aa4..28f9fa317b 100644 --- a/actionpack/test/dispatch/middleware_stack/middleware_test.rb +++ b/actionpack/test/dispatch/middleware_stack/middleware_test.rb @@ -33,12 +33,6 @@ module ActionDispatch @stack = ActionDispatch::MiddlewareStack.new end - def test_string_class - stack = ActionDispatch::MiddlewareStack.new - stack.use Omg.name - assert_equal Omg, stack.first.klass - end - def test_double_equal_works_with_classes k = Class.new stack.use k diff --git a/actionpack/test/dispatch/middleware_stack_test.rb b/actionpack/test/dispatch/middleware_stack_test.rb index 948a690979..354e95cf30 100644 --- a/actionpack/test/dispatch/middleware_stack_test.rb +++ b/actionpack/test/dispatch/middleware_stack_test.rb @@ -25,17 +25,21 @@ class MiddlewareStackTest < ActiveSupport::TestCase end test "use should push middleware as a string onto the stack" do - assert_difference "@stack.size" do - @stack.use "MiddlewareStackTest::BazMiddleware" + assert_deprecated do + assert_difference "@stack.size" do + @stack.use "MiddlewareStackTest::BazMiddleware" + end + assert_equal BazMiddleware, @stack.last.klass end - assert_equal BazMiddleware, @stack.last.klass end test "use should push middleware as a symbol onto the stack" do - assert_difference "@stack.size" do - @stack.use :"MiddlewareStackTest::BazMiddleware" + assert_deprecated do + assert_difference "@stack.size" do + @stack.use :"MiddlewareStackTest::BazMiddleware" + end + assert_equal BazMiddleware, @stack.last.klass end - assert_equal BazMiddleware, @stack.last.klass end test "use should push middleware class with arguments onto the stack" do @@ -88,8 +92,10 @@ class MiddlewareStackTest < ActiveSupport::TestCase end test "unshift adds a new middleware at the beginning of the stack" do - @stack.unshift :"MiddlewareStackTest::BazMiddleware" - assert_equal BazMiddleware, @stack.first.klass + assert_deprecated do + @stack.unshift :"MiddlewareStackTest::BazMiddleware" + assert_equal BazMiddleware, @stack.first.klass + end end test "raise an error on invalid index" do @@ -103,15 +109,11 @@ class MiddlewareStackTest < ActiveSupport::TestCase end test "lazy evaluates middleware class" do - assert_difference "@stack.size" do - @stack.use "MiddlewareStackTest::BazMiddleware" + assert_deprecated do + assert_difference "@stack.size" do + @stack.use "MiddlewareStackTest::BazMiddleware" + end + assert_equal BazMiddleware, @stack.last.klass end - assert_equal BazMiddleware, @stack.last.klass - end - - test "lazy compares so unloaded constants are not loaded" do - @stack.use "UnknownMiddleware" - @stack.use :"MiddlewareStackTest::BazMiddleware" - assert @stack.include?("::MiddlewareStackTest::BazMiddleware") end end |