Browse files

Added a block option to should_redirect_to to replace the eval'd string

  • Loading branch information...
1 parent 3a3c576 commit a8f0d9491a6e832766874feaba2ff26a57e29017 @jferris jferris committed Feb 7, 2009
Showing with 32 additions and 7 deletions.
  1. +14 −7 lib/shoulda/controller/macros.rb
  2. +18 −0 test/functional/posts_controller_test.rb
View
21 lib/shoulda/controller/macros.rb
@@ -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
View
18 test/functional/posts_controller_test.rb
@@ -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.