Class: Accountability::Inventory

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
app/models/accountability/inventory.rb

Defined Under Namespace

Classes: InventoryItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(product, available_only: false) ⇒ Inventory

Returns a new instance of Inventory.



22
23
24
25
# File 'app/models/accountability/inventory.rb', line 22

def initialize(product, available_only: false)
  @product = product
  @scope_availability = available_only
end

Instance Attribute Details

#productObject

Returns the value of attribute product.



17
18
19
# File 'app/models/accountability/inventory.rb', line 17

def product
  @product
end

Instance Method Details

#availableObject



38
39
40
41
42
# File 'app/models/accountability/inventory.rb', line 38

def available
  @scope_availability = true

  self
end

#available_source_idsObject



50
51
52
53
54
# File 'app/models/accountability/inventory.rb', line 50

def available_source_ids
  return @available_source_ids if @available_source_ids.present?

  @available_source_ids = Inventory.new(product, available_only: true).source_records.ids
end

#collectionObject



27
28
29
# File 'app/models/accountability/inventory.rb', line 27

def collection
  source_records.map { |record| InventoryItem.new(record: record, product: product, inventory: self) }
end

#order(order_params) ⇒ Object



44
45
46
47
48
# File 'app/models/accountability/inventory.rb', line 44

def order(order_params)
  @order_params = order_params

  self
end

#source_recordsObject



31
32
33
34
35
36
# File 'app/models/accountability/inventory.rb', line 31

def source_records
  records = source_class.where(**source_scope).includes(:price_overrides)
  records = records.public_send(offerable_template.whitelist) if scope_availability?
  records = records.order(@order_params) if @order_params.present?
  records
end