Class: SpreeCmCommissioner::Stock::AvailabilityChecker
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::Stock::AvailabilityChecker
- Defined in:
- app/models/spree_cm_commissioner/stock/availability_checker.rb
Instance Attribute Summary collapse
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#variant ⇒ Object
readonly
Returns the value of attribute variant.
Instance Method Summary collapse
- #builder_klass ⇒ Object
- #cached_inventory_items ⇒ Object
-
#can_supply?(quantity = 1, include_locked: false) ⇒ Boolean
Can
quantitybe supplied? By default from THIS line's pool — which is what add-to-cart gates on, so it must never quietly include stock the caller cannot buy. -
#initialize(variant, options = {}) ⇒ AvailabilityChecker
constructor
options checks the operator's withheld allotment instead of public stock — set for a line item whose LineItem#locked_stock? is true.
-
#quantity_in_pool(item, include_locked: false) ⇒ Object
The cache row carries both pools, so choosing between them is a read, not another Redis trip.
Constructor Details
#initialize(variant, options = {}) ⇒ AvailabilityChecker
options checks the operator's withheld allotment instead of public stock — set for a line item whose LineItem#locked_stock? is true.
8 9 10 11 12 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 8 def initialize(variant, = {}) @variant = variant @options = @error_message = nil end |
Instance Attribute Details
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
4 5 6 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 4 def @error_message end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 4 def @options end |
#variant ⇒ Object (readonly)
Returns the value of attribute variant.
4 5 6 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 4 def variant @variant end |
Instance Method Details
#builder_klass ⇒ Object
52 53 54 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 52 def builder_klass ::SpreeCmCommissioner::RedisStock::VariantCachedInventoryItemsBuilder end |
#cached_inventory_items ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 37 def cached_inventory_items return @cached_inventory_items if defined?(@cached_inventory_items) if variant.permanent_stock? return [] if [:from_date].blank? || [:to_date].blank? dates = [:from_date].to_date..[:to_date].to_date @cached_inventory_items = builder_klass.new(variant_id: variant.id, dates: dates).call @cached_inventory_items = [] if @cached_inventory_items.size != dates.count @cached_inventory_items else @cached_inventory_items = builder_klass.new(variant_id: variant.id).call end end |
#can_supply?(quantity = 1, include_locked: false) ⇒ Boolean
Can quantity be supplied? By default from THIS line's pool — which is what add-to-cart gates
on, so it must never quietly include stock the caller cannot buy.
include_locked: true adds the locked pool to the total instead, for callers asking "is this worth showing at all" rather than "may this order have it". See Variant#in_stock?.
19 20 21 22 23 24 25 26 27 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 19 def can_supply?(quantity = 1, include_locked: false) return false unless variant.available? return true unless variant.should_track_inventory? return true if variant.backorderable? return true if variant.need_confirmation? return false if cached_inventory_items.empty? cached_inventory_items.all? { |item| item.active? && quantity_in_pool(item, include_locked: include_locked) >= quantity } end |
#quantity_in_pool(item, include_locked: false) ⇒ Object
The cache row carries both pools, so choosing between them is a read, not another Redis trip.
30 31 32 33 34 35 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 30 def quantity_in_pool(item, include_locked: false) return item.quantity_available + item.quantity_locked if include_locked return item.quantity_locked if [:locked] item.quantity_available end |