fixed problem with multiple carts and a whole load of problems with crazy delivery destination logic / if I didn't write it i would have thought it was written by a drug abuser during high

This commit is contained in:
Senad Uka
2015-03-14 08:17:06 +01:00
parent e7793f0884
commit 8815267a79
12 changed files with 195 additions and 25 deletions

View File

@@ -7,7 +7,14 @@ class Cart < ActiveRecord::Base
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 )
cart.delivery_destination ||= DeliveryDestination.find_or_create(anonymous_id, user_id);
cart.delivery_destination ||= DeliveryDestination.create_from_last(anonymous_id, safe_user_id);
cart.save!
return cart
end
def self.just_find(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
return cart
end
end

View File

@@ -2,10 +2,13 @@ class DeliveryDestination < ActiveRecord::Base
has_one :cart
belongs_to :user
def self.find_or_create(anonymous_id, user_id)
def self.create_from_last(anonymous_id, user_id)
dd = DeliveryDestination.where(user_id: user_id).order("id desc").first
dd ||= DeliveryDestination.where(anonymous_id_string: anonymous_id).order("id desc").first
dd ||= DeliveryDestination.create!({user_id: user_id, anonymous_id_string: anonymous_id })
return dd
dd ||= DeliveryDestination.new({user_id: user_id, anonymous_id_string: anonymous_id })
attributes = dd.as_json
attributes.delete("id")
result = DeliveryDestination.create!(attributes)
return result
end
end