From 0704df5686a2ff8ebfbf1c1cb867d745a9702014 Mon Sep 17 00:00:00 2001 From: Michael Hartl Date: Thu, 26 Aug 2010 16:41:34 -0700 Subject: [PATCH] Sign out --- app/controllers/sessions_controller.rb | 2 ++ app/helpers/sessions_helper.rb | 5 +++++ spec/controllers/sessions_controller_spec.rb | 9 +++++++++ spec/spec_helper.rb | 4 ++++ 4 files changed, 20 insertions(+) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 5569ec3..340a091 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -18,5 +18,7 @@ def create end def destroy + sign_out + redirect_to root_path end end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index b1bcce2..9710ac4 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -17,6 +17,11 @@ def signed_in? !current_user.nil? end + def sign_out + cookies.delete(:remember_token) + self.current_user = nil + end + private def user_from_remember_token diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 68a7a44..f2165e8 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -58,4 +58,13 @@ end end end + + describe "DELETE 'destroy'" do + it "should sign a user out" do + test_sign_in(Factory(:user)) + delete :destroy + controller.should_not be_signed_in + response.should redirect_to(root_path) + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 447111a..698c0d1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -36,6 +36,10 @@ # Emulate initializer set_clear_dependencies_hook in # railties/lib/rails/application/bootstrap.rb ActiveSupport::Dependencies.clear + + def test_sign_in(user) + controller.current_user = user + end end end