rails-4 transferring records of guest_user to current_user after
sign-in/register
I have shopping cart that has a guest user. The guest_user can create a
cart but if he sign-ins or signs-up,the guest_cart is transferred from
guest_user to current_user.
Though the code for doing the transfer of ownership works, it doesn't
transfer the guest_users cart to the signed-in user unless I click the
cart to add an aditional item to cart.
What I want is for the cart to be transferred from guest_user to
current_user immediately he signs in without needing to click an
additional add to cart. Railscasts gave an example here and even I
followed the guide in that screencast, it doesn't still work thesame way:
http://railscasts.com/episodes/393-guest-user-record
The code that loads the cart:
class OrderItemsController < ApplicationController
before_action :load_order, only: [:create]
private
def load_order
@order = Order.find_or_initialize_by(id: session[:order_id], status:
'unsubmitted')
@order.user = current_or_guest_user
if @order.new_record?
@order.save!
session[:order_id] = @order.id
end
end
end
class ApplicationController < ActionController::Base
include DeviseGuest
end
module DeviseGuest
extend ActiveSupport::Concern
included do
helper_method :current_or_guest_user
end
#if user is logged in return current_user else return guest_user
def current_or_guest_user
if current_user
if session[:guest_user_id]
logging_in
guest_user.destroy
session[:guest_user_id] = nil
end
current_user
else
guest_user
end
end
#find guest_user object associated with the current session or create one
def guest_user
@cached_guest_user ||= User.find(session[:guest_user_id] ||=
create_guest_user.id)
rescue ActiveRecord::RecordNotFound # if session[:guest_user_id] invalid
session[:guest_user_id] = nil
guest_user
end
private
#called once when the user logs in, insert any code your application needs
hand of from guest_user to current_user
def logging_in
guest_orders = guest_user.orders.all
guest_orders.each do |order|
#order.user_id = current_user.id
order.update(user_id: current_user.id)
order.save!
end
end
def create_guest_user
u = User.create(:email => "guest_#{Time.now.to_i}#{rand(99)}@example.com")
u.guest = true if u.respond_to? :guest
u.save!(:validate => false)
session[:guest_user_id] = u.id
u
end
end
The logging_in method is used to associate guest_user when logging in but
If the user is signing-up for the first time, I user this:
class UsersController < ApplicationController
def create
@user = User.new(user_params)
if @user.save
current_user.move_guest_cart_to_user(@user) if current_user &&
current_user.guest?
#sign_in @user #immediately signs in the user
#current_or_guest_user if current_user && current_user.guest?
redirect_to @user, {notice: 'User was created'}
else
render 'new'
end
end
end
The user model with a method transfer ownership from guest to user
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :orders
#transfer the items in current cart added by a guest to a user after
the guest signs up
def move_guest_cart_to_user(user)
orders.update_all(user_id: user.id)
end
end
How can I make the guest user's cart to be transferred to the signed_in
user or a newly created without clicking having to click on button_to to
add to cart.
No comments:
Post a Comment