From a8f0d9491a6e832766874feaba2ff26a57e29017 Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Sat, 7 Feb 2009 18:31:03 -0500 Subject: [PATCH] Added a block option to should_redirect_to to replace the eval'd string --- lib/shoulda/controller/macros.rb | 21 ++++++++++++++------- test/functional/posts_controller_test.rb | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/shoulda/controller/macros.rb b/lib/shoulda/controller/macros.rb index 0bb3747..cfc06d0 100644 --- a/lib/shoulda/controller/macros.rb +++ b/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 diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index e2ba3e2..4c93972 100644 --- a/test/functional/posts_controller_test.rb +++ b/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