Class: Comable::Stock

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Liquidable, Ransackable, SkuChoice, SkuItem, Csvable
Defined in:
app/models/comable/stock.rb,
app/models/comable/stock/csvable.rb

Overview

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

Defined Under Namespace

Modules: Csvable

Scope collapse

Instance Method Summary collapse

Methods included from Csvable

#product_id=

Methods included from SkuChoice

#name_with_sku

Methods included from SkuItem

#sku_h?, #sku_v?

Class Method Details

.by_newestObject

returns sorted records by newest.


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

scope :by_newest, -> { reorder(created_at: :desc) }

.stockedObject

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


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

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

.unstockedObject

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


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

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

Instance Method Details

#codeObject


84
85
86
# File 'app/models/comable/stock.rb', line 84

def code
  variant.sku
end

#product=(product) ⇒ Object


88
89
90
91
92
93
94
# File 'app/models/comable/stock.rb', line 88

def product=(product)
  if variant
    variant.product = product
  else
    build_variant(product: product)
  end
end

#set_default_stock_locationObject


96
97
98
# File 'app/models/comable/stock.rb', line 96

def set_default_stock_location
  self.stock_location = Comable::StockLocation.default
end

#sku_h_choice_nameObject


76
77
78
# File 'app/models/comable/stock.rb', line 76

def sku_h_choice_name
  variant.option_values.first.try(:name)
end

#sku_v_choice_nameObject


80
81
82
# File 'app/models/comable/stock.rb', line 80

def sku_v_choice_name
  variant.option_values.second.try(:name)
end

#stocked?(quantity: 1) ⇒ Boolean

在庫の有無を取得する

Examples:

stock.quanaity = 1
stock.stocked? #=> true

Parameters:

  • quantity (Fixnum) (defaults to: 1)

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

Returns:

  • (Boolean)

    在庫があれば true を返す

See Also:


59
60
61
# File 'app/models/comable/stock.rb', line 59

def stocked?(quantity: 1)
  (self.quantity - quantity) >= 0
end

#unstocked?(quantity: 1) ⇒ Boolean

在庫の有無を取得する

Examples:

stock.quanaity = 1
stock.unstocked? #=> false

Parameters:

  • quantity (Fixnum) (defaults to: 1)

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

Returns:

  • (Boolean)

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

See Also:


72
73
74
# File 'app/models/comable/stock.rb', line 72

def unstocked?(quantity: 1)
  !stocked?(quantity: quantity)
end