Class: Unidom::Shopping::ShoppingCart

Inherits:
ApplicationRecord show all
Includes:
Common::Concerns::ModelExtension
Defined in:
app/models/unidom/shopping/shopping_cart.rb

Instance Method Summary collapse

Instance Method Details

#add!(shopped, by: nil, unit_price: 0, quantity: 1, at: Time.now) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/unidom/shopping/shopping_cart.rb', line 24

def add!(shopped, by: nil, unit_price: 0, quantity: 1, at: Time.now)

  assert_present! :shopped, shopped

  item = items.shopped_is(shopped).valid_at.alive.first
  if item.present?
    item.attributes = { shopper: by||shopper, unit_price: unit_price, quantity: quantity+item.quantity }
    item.save!
  else
    items.create! shopped: shopped, shopper: by||shopper, unit_price: unit_price, quantity: quantity, opened_at: at
  end
end