2015-02-06 06:58:16 +01:00
|
|
|
class Cart < ActiveRecord::Base
|
2015-02-07 07:52:32 +01:00
|
|
|
has_many :item_in_carts, -> { order "created_at" }
|
2015-02-26 06:48:34 +01:00
|
|
|
belongs_to :delivery_destination
|
2015-02-08 08:29:24 +01:00
|
|
|
|
2015-02-06 06:58:16 +01:00
|
|
|
def self.find_or_create(anonymous_id, user_id)
|
|
|
|
|
cart = Cart.where(user_id: user_id).where(ordered: false).first
|
|
|
|
|
cart ||= Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
|
|
|
|
safe_user_id = (user_id > 0) ? user_id : nil
|
|
|
|
|
cart ||= Cart.create!(anonymous_id_string: anonymous_id, user_id: safe_user_id, ordered: false )
|
2015-03-03 07:26:18 +01:00
|
|
|
cart.delivery_destination ||= DeliveryDestination.find_or_create(anonymous_id, user_id);
|
2015-02-26 06:48:34 +01:00
|
|
|
return cart
|
2015-02-06 06:58:16 +01:00
|
|
|
end
|
2015-03-03 07:26:18 +01:00
|
|
|
end
|