Class: Comable::Stock

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Decoratable
Defined in:
app/models/comable/stock.rb

Overview

在庫モデル。商品に複数紐付き、品数やSKU(Stock Keeping Unit)情報を保持する。

Scope collapse

Instance Method Summary collapse

Methods included from Decoratable

included

Class Method Details

.activatedObject

有効な在庫インスタンスを返す



17
# File 'app/models/comable/stock.rb', line 17

scope :activated, -> { where.not(product_id_num: nil) }

.soldoutObject

品切れの在庫インスタンスを返す



25
# File 'app/models/comable/stock.rb', line 25

scope :soldout, -> { where('quantity <= ?', 0) }

.unsoldObject

品切れでない在庫インスタンスを返す



21
# File 'app/models/comable/stock.rb', line 21

scope :unsold, -> { where('quantity > ?', 0) }

Instance Method Details

#decrement!(quantity: 1) ⇒ Boolean

在庫減算を行う

Examples:

stock.quantity #=> 10
stock.decrement!(quantity: 1) #=> true
stock.quantity #=> 9

Parameters:

  • quantity (Fixnum) (defaults to: 1)

    減算する在庫数を指定する

Returns:

  • (Boolean)

    レコードの保存に成功すると true を返す



74
75
76
77
78
79
80
# File 'app/models/comable/stock.rb', line 74

def decrement!(quantity: 1)
  with_lock do
    # TODO: カラムマッピングのdecrementメソッドへの対応
    self.quantity -= quantity
    save!
  end
end

#nameObject



34
35
36
37
38
39
# File 'app/models/comable/stock.rb', line 34

def name
  return product.name unless product.sku?
  sku_name = sku_h_choice_name
  sku_name += '/' + sku_v_choice_name if sku_v_choice_name.present?
  product.name + "(#{sku_name})"
end

#soldout?Boolean

在庫の有無を取得する

Examples:

stock.soldout? #=> false

Returns:

  • (Boolean)

    #unsold? の逆。在庫がなければ true を返す

See Also:



61
62
63
# File 'app/models/comable/stock.rb', line 61

def soldout?
  !unsold?
end

#unsold?Boolean

在庫の有無を取得する

Examples:

stock.unsold? #=> true

Returns:

  • (Boolean)

    在庫があれば true を返す

See Also:



48
49
50
51
52
# File 'app/models/comable/stock.rb', line 48

def unsold?
  return false if product_id_num.nil?
  return false if quantity.nil?
  quantity > 0
end