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) ⇒ Boolean
-
#initialize(variant, options = {}) ⇒ AvailabilityChecker
constructor
A new instance of AvailabilityChecker.
- #variant_available?(quantity = 1) ⇒ Boolean
Constructor Details
#initialize(variant, options = {}) ⇒ AvailabilityChecker
Returns a new instance of AvailabilityChecker.
6 7 8 9 10 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 6 def initialize(variant, = {}) @variant = variant = = 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 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 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
44 45 46 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 44 def builder_klass ::SpreeCmCommissioner::RedisStock::VariantCachedInventoryItemsBuilder end |
#cached_inventory_items ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 29 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) ⇒ Boolean
12 13 14 15 16 17 18 19 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 12 def can_supply?(quantity = 1) return false unless variant.available? return true unless variant.should_track_inventory? return true if variant.backorderable? return true if variant.need_confirmation? variant_available?(quantity) end |
#variant_available?(quantity = 1) ⇒ Boolean
21 22 23 24 25 26 27 |
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 21 def variant_available?(quantity = 1) return false if cached_inventory_items.empty? cached_inventory_items.all? do |cached_inventory_item| cached_inventory_item.active? && cached_inventory_item.quantity_available >= quantity end end |