require that all blocks have arity of 2
This commit is contained in:
parent
3178cc9a80
commit
4589b2419b
@ -43,13 +43,19 @@ def redirect(*args, &block)
|
|||||||
path = args.shift
|
path = args.shift
|
||||||
|
|
||||||
path_proc = if path.is_a?(String)
|
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?
|
elsif options.any?
|
||||||
options_proc(options)
|
options_proc(options)
|
||||||
elsif path.respond_to?(:call)
|
elsif path.respond_to?(:call)
|
||||||
proc { |params, request| path.call(params, request) }
|
proc { |params, request| path.call(params, request) }
|
||||||
elsif block
|
elsif 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
|
block
|
||||||
|
end
|
||||||
else
|
else
|
||||||
raise ArgumentError, "redirection argument not supported"
|
raise ArgumentError, "redirection argument not supported"
|
||||||
end
|
end
|
||||||
@ -85,8 +91,7 @@ def redirection_proc(status, path_proc)
|
|||||||
lambda do |env|
|
lambda do |env|
|
||||||
req = Request.new(env)
|
req = Request.new(env)
|
||||||
|
|
||||||
params = [req.symbolized_path_parameters]
|
params = [req.symbolized_path_parameters, req]
|
||||||
params << req if path_proc.arity > 1
|
|
||||||
|
|
||||||
uri = URI.parse(path_proc.call(*params))
|
uri = URI.parse(path_proc.call(*params))
|
||||||
uri.scheme ||= req.scheme
|
uri.scheme ||= req.scheme
|
||||||
|
@ -79,7 +79,7 @@ def self.call(params, request)
|
|||||||
match 'sign_in' => "sessions#new"
|
match 'sign_in' => "sessions#new"
|
||||||
|
|
||||||
match 'account/modulo/:name', :to => redirect("/%{name}s")
|
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/proc_req' => redirect {|params, req| "/#{req.method}" }
|
||||||
|
|
||||||
match 'account/google' => redirect('http://www.google.com/', :status => 302)
|
match 'account/google' => redirect('http://www.google.com/', :status => 302)
|
||||||
|
Loading…
Reference in New Issue
Block a user