Sha256: 8224d765d6aca31692d050886349b5f90126905ad9899e61bfa712d45363427c
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
module Comable # # 在庫モデル。 # 商品に複数紐付き、品数やSKU(Stock Keeping Unit)情報を保持する。 # class Stock < ActiveRecord::Base include Comable::SkuChoice belongs_to :product, class_name: Comable::Product.name, foreign_key: Comable::Product.table_name.singularize.foreign_key # # @!group Scope # # @!scope class # 有効な在庫インスタンスを返す scope :activated, -> { where.not(product_id_num: nil) } # @!scope class # 品切れでない在庫インスタンスを返す scope :unsold, -> { where('quantity > ?', 0) } # @!scope class # 品切れの在庫インスタンスを返す scope :soldout, -> { where('quantity <= ?', 0) } # # @!endgroup # delegate :name, to: :product delegate :price, to: :product delegate :sku?, to: :product # 在庫の有無を取得する # # @example # stock.unsold? #=> true # # @return [Boolean] 在庫があれば true を返す # @see #soldout? def unsold? return false if product_id_num.nil? return false if quantity.nil? quantity > 0 end # 在庫の有無を取得する # # @example # stock.soldout? #=> false # # @return [Boolean] {#unsold?} の逆。在庫がなければ true を返す # @see #unsold? def soldout? !unsold? end # 在庫減算を行う # # @example # stock.quantity #=> 10 # stock.decrement!(quantity: 1) #=> true # stock.quantity #=> 9 # # @param quantity [Fixnum] 減算する在庫数を指定する # @return [Boolean] レコードの保存に成功すると true を返す def decrement!(quantity: 1) with_lock do # TODO: カラムマッピングのdecrementメソッドへの対応 self.quantity -= quantity save! end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
comable_core-0.2.0 | app/models/comable/stock.rb |