module Stall::CartHelper

Public Instance Methods

current_cart() click to toggle source
# File lib/stall/cart_helper.rb, line 17
def current_cart
  RequestStore.store[cart_key] ||= load_current_cart
end
current_customer() click to toggle source
# File lib/stall/cart_helper.rb, line 21
def current_customer
  @current_customer ||= if user_signed_in?
    if (customer = current_user.customer)
      customer
    else
      current_user.create_customer(email: current_user.email)
    end
  end
end

Protected Instance Methods

cart_class() click to toggle source
# File lib/stall/cart_helper.rb, line 93
def cart_class
  Cart
end
cart_key(identifier = current_cart_key, namespace: nil) click to toggle source
# File lib/stall/cart_helper.rb, line 89
def cart_key(identifier = current_cart_key, namespace: nil)
  ['stall', 'cart', namespace, identifier.to_s].compact.join('.')
end
current_cart_key() click to toggle source
# File lib/stall/cart_helper.rb, line 33
def current_cart_key
  params[:cart_key].try(:to_sym) || :default
end
find_cart(identifier, ensure_active_cart = true) click to toggle source
# File lib/stall/cart_helper.rb, line 70
def find_cart(identifier, ensure_active_cart = true)
  if (cart_token = cookies.encrypted[cart_key(identifier)])
    if (current_cart = ProductList.find_by_token(cart_token)) &&
       (!ensure_active_cart || current_cart.active?)
    then
      return current_cart
    else
      # Remove any cart that can't be fetched, either because it's already
      # paid, or because it was cleaned out
      remove_cart_from_cookies(identifier)
      nil
    end
  end
end
load_current_cart(identifier = current_cart_key) click to toggle source
# File lib/stall/cart_helper.rb, line 37
def load_current_cart(identifier = current_cart_key)
  if (cart = find_cart(identifier))
    return prepare_cart(cart)
  end

  # If no token was stored or the token does not exist anymore, create a
  # new cart and store the new token
  #
  prepare_cart(cart_class.new(identifier: identifier))
end
prepare_cart(cart) click to toggle source
# File lib/stall/cart_helper.rb, line 48
def prepare_cart(cart)
  cart.tap do
    cart.customer = current_customer if current_customer
    # Keep track of potential customer locale switching to allow e-mailing
    # him in his last used locale
    #
    # Only applicable if the locale is an available locale to avoid strange
    # issues with some URL locale management systems that could set the
    # `assets` prefix as a locale, for instance.
    if cart.customer && I18n.locale.in?(I18n.available_locales)
      cart.customer.locale = I18n.locale
    end

    Stall.config.service_for(:product_list_staleness_handling).new(cart).call

    # Only update locale change for existing carts. New carts don't need
    # to be saved, avoiding each robot or simple visitors to create a
    # cart on large shops.
    cart.save unless cart.new_record?
  end
end
remove_cart_from_cookies(identifier = current_cart_key) click to toggle source
# File lib/stall/cart_helper.rb, line 85
def remove_cart_from_cookies(identifier = current_cart_key)
  cookies.delete(cart_key(identifier))
end
store_cart_to_cookies() click to toggle source
# File lib/stall/cart_helper.rb, line 97
def store_cart_to_cookies
  if current_cart.persisted? && current_cart.active?
    store_cart_cookie_for(current_cart.identifier, current_cart)
  end
end