Class: Workarea::Inventory::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/models/workarea/inventory/collection.rb

Overview

This class represents a collection of Skus. It is more performant because it does a single query to grab the Skus instead of N+1.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(skus, records = nil) ⇒ Collection

Returns a new instance of Collection.



12
13
14
15
# File 'app/models/workarea/inventory/collection.rb', line 12

def initialize(skus, records = nil)
  @skus = skus
  @records = records
end

Instance Attribute Details

#skusObject (readonly)

Returns the value of attribute skus.



9
10
11
# File 'app/models/workarea/inventory/collection.rb', line 9

def skus
  @skus
end

Class Method Details

.statusesObject



17
18
19
20
21
# File 'app/models/workarea/inventory/collection.rb', line 17

def self.statuses
  Workarea.config.inventory_status_calculators.map do |klass|
    klass.demodulize.underscore
  end
end

Instance Method Details

#available_to_sellObject

The total number available to sell

return [Integer]



33
34
35
# File 'app/models/workarea/inventory/collection.rb', line 33

def available_to_sell
  records.map(&:available_to_sell).sum
end

#for_sku(sku) ⇒ Sku

Grab the specific Sku for a SKU. Returns a new, unpersisted Sku if one does not exist.

Parameters:

Returns:



51
52
53
# File 'app/models/workarea/inventory/collection.rb', line 51

def for_sku(sku)
  records.detect { |r| r.id == sku }
end

#policiesObject



55
56
57
# File 'app/models/workarea/inventory/collection.rb', line 55

def policies
  records.map(&:policy)
end

#purchasable?(quantity = 1) ⇒ Boolean

If the product has any puchasable inventory

return [Boolean]

Returns:

  • (Boolean)


41
42
43
# File 'app/models/workarea/inventory/collection.rb', line 41

def purchasable?(quantity = 1)
  quantity <= available_to_sell
end

#recordsObject



69
70
71
72
73
74
75
76
77
# File 'app/models/workarea/inventory/collection.rb', line 69

def records
  @records ||=
    begin
      existing = Sku.in(id: skus).to_a
      missing_skus = skus - existing.map(&:id)

      existing + missing_skus.map { |sku| Sku.new(id: sku ) }
    end
end

#statusSymbol

Get the status of this collection of inventory skus.

Returns:

  • (Symbol)


63
64
65
66
# File 'app/models/workarea/inventory/collection.rb', line 63

def status
  calculators = Workarea.config.inventory_status_calculators.map(&:constantize)
  StatusCalculator.new(calculators, self).result
end