Sha256: b0cbc33033a52fdec9963a8528be281bd3150c597dbf3c2ec5de43a5ba16edba

Contents?: true

Size: 895 Bytes

Versions: 3

Compression:

Stored size: 895 Bytes

Contents

# Shopping Cart 是购物车。

class Unidom::Shopping::ShoppingCart < Unidom::Shopping::ApplicationRecord

  self.table_name = 'unidom_shopping_carts'

  include Unidom::Common::Concerns::ModelExtension

  belongs_to :shopper, polymorphic: true
  belongs_to :shop,    polymorphic: true

  has_many :items, class_name: 'Unidom::Shopping::ShoppingItem'

  scope :shopped_by, ->(shopper) { where shopper: shopper }
  scope :shop_is,    ->(shop)    { where shop:    shop    }

  def add!(shopped, by: nil, unit_price: 0, quantity: 1, at: Time.now)
    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

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
unidom-shopping-1.6.3 app/models/unidom/shopping/shopping_cart.rb
unidom-shopping-1.6.2 app/models/unidom/shopping/shopping_cart.rb
unidom-shopping-1.6.1 app/models/unidom/shopping/shopping_cart.rb