Merge branch 'master' of https://github.com/senaduka/ribica
This commit is contained in:
@@ -27,20 +27,20 @@ post '/cart/item', &update_cart_item
|
||||
|
||||
|
||||
# gets list of items in cart without count
|
||||
get '/cart/item/display' do
|
||||
get '/cart/item/display' do
|
||||
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
||||
item_ids = cart.item_in_carts.map do |x|
|
||||
x.item_id
|
||||
x.item_id
|
||||
end
|
||||
items = []
|
||||
items = Item.find(item_ids) if cart.item_in_carts.length > 0
|
||||
items = Item.find(item_ids) if cart.item_in_carts.length > 0
|
||||
prepare_items_for_mass_display(items)
|
||||
end
|
||||
|
||||
get '/cart/delivery_destination' do
|
||||
get '/cart/delivery_destination' do
|
||||
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
||||
cart.delivery_destination.to_json(:except => [:created_at, :email_verification_code, :phone_verification_code])
|
||||
end
|
||||
end
|
||||
|
||||
update_delivery_destination = ->() {
|
||||
cart = Cart.just_find(anonymous_id, logged_in_user_id)
|
||||
@@ -54,7 +54,7 @@ put '/cart/delivery_destination', &update_delivery_destination
|
||||
post '/cart/delivery_destination', &update_delivery_destination
|
||||
|
||||
|
||||
post '/cart/confirmation' do
|
||||
post '/cart/confirmation' do
|
||||
anonymous = anonymous_id
|
||||
cart = Cart.just_find(anonymous, logged_in_user_id)
|
||||
if cart.item_in_carts.length > 0
|
||||
@@ -65,4 +65,4 @@ post '/cart/confirmation' do
|
||||
# in order for next call of Cart#just_find to be ready
|
||||
Cart.find_or_create(anonymous, logged_in_user_id)
|
||||
"OK".to_json
|
||||
end
|
||||
end
|
||||
|
||||
@@ -23,11 +23,11 @@ ActiveRecord::Schema.define(version: 20150319104317) do
|
||||
t.datetime "updated_at", null: false
|
||||
t.string "anonymous_id_string"
|
||||
t.integer "delivery_destination_id"
|
||||
t.boolean "confirmed", default: false
|
||||
t.boolean "packed", default: false
|
||||
t.boolean "canceled_on_check", default: false
|
||||
t.boolean "canceled_on_delivery", default: false
|
||||
t.boolean "delivered", default: false
|
||||
t.boolean "confirmed"
|
||||
t.boolean "packed"
|
||||
t.boolean "canceled_on_check"
|
||||
t.boolean "canceled_on_delivery"
|
||||
t.boolean "delivered"
|
||||
t.text "internal_note"
|
||||
end
|
||||
|
||||
@@ -101,6 +101,7 @@ ActiveRecord::Schema.define(version: 20150319104317) do
|
||||
t.datetime "updated_at", null: false
|
||||
t.string "tags"
|
||||
t.json "traits"
|
||||
t.integer "supplier_id"
|
||||
t.decimal "weight", precision: 5, scale: 3
|
||||
t.integer "special_offer_id"
|
||||
t.integer "supplier_id"
|
||||
|
||||
@@ -1,14 +1,50 @@
|
||||
class Cart < ActiveRecord::Base
|
||||
has_many :item_in_carts, -> { order "created_at" }
|
||||
belongs_to :delivery_destination
|
||||
|
||||
|
||||
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 )
|
||||
cart.delivery_destination ||= DeliveryDestination.create_from_last(anonymous_id, safe_user_id);
|
||||
cart.save!
|
||||
cart = nil
|
||||
ActiveRecord::Base.transaction do
|
||||
|
||||
logged_in_user_cart = Cart.where(user_id: user_id).where(ordered: false).first
|
||||
anonymous_user_cart = Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
||||
|
||||
safe_user_id = (user_id > 0) ? user_id : nil
|
||||
if safe_user_id.nil?
|
||||
cart = anonymous_user_cart || Cart.create!(anonymous_id_string: anonymous_id, user_id: safe_user_id, ordered: false )
|
||||
else
|
||||
cart = logged_in_user_cart || Cart.create!(anonymous_id_string: nil, user_id: safe_user_id, ordered: false )
|
||||
logged_in_user_cart = cart
|
||||
end
|
||||
|
||||
# add the contents of anonymous cart
|
||||
# to the logged in one on login
|
||||
if logged_in_user_cart and anonymous_user_cart
|
||||
logged_in_items = logged_in_user_cart.item_in_carts
|
||||
anonymous_user_cart.item_in_carts.each do |a_item|
|
||||
item_exists = false
|
||||
|
||||
logged_in_items.each do |l_item|
|
||||
if a_item.item_id == l_item.item_id
|
||||
l_item.count += a_item.count
|
||||
l_item.save!
|
||||
a_item.destroy
|
||||
item_exists = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
unless item_exists
|
||||
a_item.cart_id = logged_in_user_cart.id
|
||||
a_item.save!
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
cart.delivery_destination ||= DeliveryDestination.create_from_last(anonymous_id, safe_user_id);
|
||||
cart.save!
|
||||
|
||||
end
|
||||
return cart
|
||||
end
|
||||
|
||||
@@ -17,4 +53,4 @@ class Cart < ActiveRecord::Base
|
||||
cart ||= Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
|
||||
return cart
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user