Class: SpreeCmCommissioner::Stock::AvailabilityChecker

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree_cm_commissioner/stock/availability_checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @variant = variant
  @options = options
  @error_message = nil
end

Instance Attribute Details

#error_messageObject (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
  @error_message
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 4

def options
  @options
end

#variantObject (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_klassObject



44
45
46
# File 'app/models/spree_cm_commissioner/stock/availability_checker.rb', line 44

def builder_klass
  ::SpreeCmCommissioner::RedisStock::VariantCachedInventoryItemsBuilder
end

#cached_inventory_itemsObject



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 options[:from_date].blank? || options[:to_date].blank?

    dates = options[:from_date].to_date..options[: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

Returns:

  • (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

Returns:

  • (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