Skip to content

Commit

Permalink
Added a block option to should_redirect_to to replace the eval'd string
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Feb 9, 2009
1 parent 3a3c576 commit a8f0d94
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/shoulda/controller/macros.rb
Expand Up @@ -217,14 +217,21 @@ def should_render_without_layout
# set by the controller are available to the evaled string.
# Example:
#
# should_redirect_to '"/"'
# should_redirect_to "user_url(@user)"
# should_redirect_to "users_url"
def should_redirect_to(url)
should "redirect to #{url.inspect}" do
instantiate_variables_from_assigns do
assert_redirected_to eval(url, self.send(:binding), __FILE__, __LINE__)
# should_redirect_to("the user's profile") { user_url(@user) }
def should_redirect_to(description, &block)
unless block
warn "[DEPRECATION] should_redirect_to without a block is " <<
"deprecated. Use should_redirect_to('somewhere') { } instead."
end
should "redirect to #{description}" do
if block
url = instance_eval(&block)
else
instantiate_variables_from_assigns do
url = eval(description, self.send(:binding), __FILE__, __LINE__)
end
end
assert_redirected_to url
end
end

Expand Down
18 changes: 18 additions & 0 deletions test/functional/posts_controller_test.rb
Expand Up @@ -90,6 +90,24 @@ def setup
setup { get :new, :user_id => users(:first) }
should_render_without_layout
end

context "on POST to #create" do
setup do
post :create, :user_id => users(:first),
:post => { :title => "first post",
:body => 'blah blah blah' }
end

should_redirect_to 'user_post_url(@post.user, @post)'
should_redirect_to('the created post') { user_post_url(users(:first),
assigns(:post)) }
should_fail do
should_redirect_to 'user_posts_url(@post.user)'
end
should_fail do
should_redirect_to('elsewhere') { user_posts_url(users(:first)) }
end
end
end

end

0 comments on commit a8f0d94

Please sign in to comment.