Don't override devise's after_sign_in_path_for, use specific before filters for user sessions and registrations controllers instead

This commit is contained in:
Rob Harrington
2015-04-16 16:56:27 +10:00
parent 0c155e6e3a
commit 5940ff2b2c
5 changed files with 61 additions and 14 deletions

View File

@@ -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

View File

@@ -1,4 +1,6 @@
Spree::UserSessionsController.class_eval do
before_filter :set_checkout_redirect, only: :create
def create
authenticate_spree_user!

View File

@@ -1,4 +1,5 @@
class UserRegistrationsController < Spree::UserRegistrationsController
before_filter :set_checkout_redirect, only: :create
# POST /resource/sign_up
def create

View File

@@ -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

View File

@@ -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