From 9e0a6acf55cf496c298fd5378e4d95ca8e1b3a36 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Tue, 6 Mar 2012 22:03:12 +0100 Subject: [PATCH] Spec for #redirect_back_or_to --- app/controllers/application_controller.rb | 9 +++++++-- .../application_controller_spec.rb | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8cebc48..7526476 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -40,9 +40,14 @@ class ApplicationController < ActionController::Base session.delete(:return_to) end - def redirect_back_or_to(default, options = {}) + def redirect_back_or_to(default, options = nil) path = get_stored_location || default - redirect_to path, options + + if options + redirect_to path, options + else + redirect_to path + end end def forbidden diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 712492a..e428376 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -85,5 +85,25 @@ describe FakeController do assigns[:location_again].should == 'NOWAI!' end end + + describe '#redirect_back_or_to' do + context 'when there is no stored location' do + it 'redirects to given location' do + path = double + @controller.should_receive(:redirect_to).with(path) + @controller.send(:redirect_back_or_to, path) + end + end + + context 'when there is stored location' do + it 'redirects to stored location' do + stored_path = double + path = double + @controller.stub!(:get_stored_location => stored_path) + @controller.should_receive(:redirect_to).with(stored_path) + @controller.send(:redirect_back_or_to, path) + end + end + end end