From 4589b2419b6c2f6d8b1ea0873999a4d0fa21bdb3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 31 Oct 2011 16:26:11 -0400 Subject: [PATCH] require that all blocks have arity of 2 --- .../lib/action_dispatch/routing/redirection.rb | 15 ++++++++++----- actionpack/test/dispatch/routing_test.rb | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index 804991ad5f..27dbf72519 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -43,13 +43,19 @@ def redirect(*args, &block) path = args.shift path_proc = if path.is_a?(String) - proc { |params| (params.empty? || !path.match(/%\{\w*\}/)) ? path : (path % params) } + proc { |params, request| (params.empty? || !path.match(/%\{\w*\}/)) ? path : (path % params) } elsif options.any? options_proc(options) elsif path.respond_to?(:call) proc { |params, request| path.call(params, request) } elsif block - block + if block.arity < 2 + msg = "redirect blocks with arity of #{block.arity} are deprecated. Your block must take 2 parameters: the environment, and a request object" + ActiveSupport::Deprecation.warn msg + lambda { |params, _| block.call(params) } + else + block + end else raise ArgumentError, "redirection argument not supported" end @@ -85,8 +91,7 @@ def redirection_proc(status, path_proc) lambda do |env| req = Request.new(env) - params = [req.symbolized_path_parameters] - params << req if path_proc.arity > 1 + params = [req.symbolized_path_parameters, req] uri = URI.parse(path_proc.call(*params)) uri.scheme ||= req.scheme @@ -107,4 +112,4 @@ def redirection_proc(status, path_proc) end end -end \ No newline at end of file +end diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index cf22731823..66b2263790 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -79,7 +79,7 @@ def self.call(params, request) match 'sign_in' => "sessions#new" match 'account/modulo/:name', :to => redirect("/%{name}s") - match 'account/proc/:name', :to => redirect {|params| "/#{params[:name].pluralize}" } + match 'account/proc/:name', :to => redirect {|params, req| "/#{params[:name].pluralize}" } match 'account/proc_req' => redirect {|params, req| "/#{req.method}" } match 'account/google' => redirect('http://www.google.com/', :status => 302) -- GitLab