From 5940ff2b2cf10c1bdd14eabe8723b438dd508ff4 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 16 Apr 2015 16:56:27 +1000 Subject: [PATCH] Don't override devise's after_sign_in_path_for, use specific before filters for user sessions and registrations controllers instead --- app/controllers/application_controller.rb | 7 ++-- .../user_sessions_controller_decorator.rb | 2 ++ .../user_registrations_controller.rb | 1 + .../spree/user_sessions_controller_spec.rb | 31 +++++++++++++++++ .../user_registrations_controller_spec.rb | 34 ++++++++++++++----- 5 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 spec/controllers/spree/user_sessions_controller_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e96a0f316e..53763ad274 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,15 +8,12 @@ class ApplicationController < ActionController::Base super(options, response_status) end - def after_sign_in_path_for(resource) + def set_checkout_redirect if request.referer and referer_path = URI(request.referer).path - [main_app.checkout_path].include?(referer_path) ? referer_path : root_path - else - root_path + session["spree_user_return_to"] = [main_app.checkout_path].include?(referer_path) ? referer_path : root_path end end - private def require_distributor_chosen diff --git a/app/controllers/spree/user_sessions_controller_decorator.rb b/app/controllers/spree/user_sessions_controller_decorator.rb index c0d5d72381..72dc8b6d14 100644 --- a/app/controllers/spree/user_sessions_controller_decorator.rb +++ b/app/controllers/spree/user_sessions_controller_decorator.rb @@ -1,4 +1,6 @@ Spree::UserSessionsController.class_eval do + before_filter :set_checkout_redirect, only: :create + def create authenticate_spree_user! diff --git a/app/controllers/user_registrations_controller.rb b/app/controllers/user_registrations_controller.rb index a6a0074553..5728de6672 100644 --- a/app/controllers/user_registrations_controller.rb +++ b/app/controllers/user_registrations_controller.rb @@ -1,4 +1,5 @@ class UserRegistrationsController < Spree::UserRegistrationsController + before_filter :set_checkout_redirect, only: :create # POST /resource/sign_up def create diff --git a/spec/controllers/spree/user_sessions_controller_spec.rb b/spec/controllers/spree/user_sessions_controller_spec.rb new file mode 100644 index 0000000000..b4235db82b --- /dev/null +++ b/spec/controllers/spree/user_sessions_controller_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe Spree::UserSessionsController do + include AuthenticationWorkflow + + let(:user) { create_enterprise_user } + + before do + @request.env["devise.mapping"] = Devise.mappings[:spree_user] + end + + describe "create" do + context "succeed" do + context "when referer is not '/checkout'" do + it "redirects to root" do + spree_post :create, spree_user: {email: user.email, password: user.password }, :use_route => :spree + response.should redirect_to root_path + end + end + + context "when referer is '/checkout'" do + before { @request.env['HTTP_REFERER'] = 'http://test.com/checkout' } + + it "redirects to checkout" do + spree_post :create, spree_user: { email: user.email, password: user.password }, :use_route => :spree + response.should redirect_to checkout_path + end + end + end + end +end diff --git a/spec/controllers/user_registrations_controller_spec.rb b/spec/controllers/user_registrations_controller_spec.rb index ed47f0c059..baa4cb73c7 100644 --- a/spec/controllers/user_registrations_controller_spec.rb +++ b/spec/controllers/user_registrations_controller_spec.rb @@ -6,7 +6,7 @@ describe UserRegistrationsController do before do @request.env["devise.mapping"] = Devise.mappings[:spree_user] end - + describe "via ajax" do render_views it "returns errors when registration fails" do @@ -25,15 +25,31 @@ describe UserRegistrationsController do end end - it "renders new when registration fails" do - spree_post :create, spree_user: {} - response.status.should == 200 - response.should render_template "spree/user_registrations/new" + context "when registration fails" do + it "renders new" do + spree_post :create, spree_user: {} + response.status.should == 200 + response.should render_template "spree/user_registrations/new" + end end - it "redirects when registration succeeds" do - spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree - response.should be_redirect - assigns[:user].email.should == "test@test.com" + context "when registration succeeds" do + context "when referer is not '/checkout'" do + it "redirects to root" do + spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree + response.should redirect_to root_path + assigns[:user].email.should == "test@test.com" + end + end + + context "when referer is '/checkout'" do + before { @request.env['HTTP_REFERER'] = 'http://test.com/checkout' } + + it "redirects to checkout" do + spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree + response.should redirect_to checkout_path + assigns[:user].email.should == "test@test.com" + end + end end end