Sha256: 453776470d00ed862e5ad5e8a46fa06ab3c34f08c8b46ed146ed69384ce751c2

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

##
# Shopping Cart 是购物车。

module Unidom
  module Shopping

    class ShoppingCart < 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    }

      ##
      # 将商品 shopped 放入购物车。购物者为 by 。单价为 unit_price ,缺省值为 0 。数量为 quantity ,缺省值是 1 。时间为 at ,缺省为当前时间。如:
      # shopping_cart.add! coca_cola, by: current_person, unit_price: 3.50, quantity: 2
      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

    end unless Unidom::Common::Neglection.namespace_neglected? 'Unidom::Shopping::ShoppingCart'

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unidom-shopping-2.0.1 app/models/unidom/shopping/shopping_cart.rb