Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call block to #redirect_to in controller context #33735

Merged
merged 2 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/metal/redirecting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _compute_redirect_to_location(request, options) #:nodoc:
when String
request.protocol + request.host_with_port + options
when Proc
_compute_redirect_to_location request, options.call
_compute_redirect_to_location request, instance_eval(&options)
else
url_for(options)
end.delete("\0\r\n")
Expand Down
15 changes: 15 additions & 0 deletions actionpack/test/controller/redirect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ def redirect_to_with_block_and_options
redirect_to proc { { action: "hello_world" } }
end

def redirect_to_out_of_scope_block
redirect_to self.class.class_eval(<<~END)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe stead this class_eval we could define this proc as a constant in Workshop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally wrote it as a constant (in the RedirectController class), but I thought it was easier to understand the test, especially with the raise, if the proc wasn't ~100 lines away.

I'll add that change, though. I can see how the significance of the class_eval might not be immediately apparent.

Proc.new do
raise "Not executed in controller's context" unless RedirectController === self
request.original_url
end
END
end

def redirect_with_header_break
redirect_to "/lol\r\nwat"
end
Expand Down Expand Up @@ -326,6 +335,12 @@ def test_redirect_to_with_block_and_assigns
assert_redirected_to "http://www.rubyonrails.org/"
end

def test_redirect_to_out_of_scope_block
get :redirect_to_out_of_scope_block
assert_response :redirect
assert_redirected_to "http://test.host/redirect/redirect_to_out_of_scope_block"
end

def test_redirect_to_with_block_and_accepted_options
with_routing do |set|
set.draw do
Expand Down