Class: Accountability::Inventory::InventoryItem

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record:, product:, inventory:) ⇒ InventoryItem

Returns a new instance of InventoryItem.



61
62
63
64
65
66
67
68
69
# File 'app/models/accountability/inventory.rb', line 61

def initialize(record:, product:, inventory:)
  @record = record
  @product = product
  @inventory = inventory

  # Expose source record through offerable's name
  record_alias = product.offerable_category.to_sym
  alias :"#{record_alias}" record
end

Instance Attribute Details

#inventoryObject

Returns the value of attribute inventory.



59
60
61
# File 'app/models/accountability/inventory.rb', line 59

def inventory
  @inventory
end

#productObject

Returns the value of attribute product.



59
60
61
# File 'app/models/accountability/inventory.rb', line 59

def product
  @product
end

#recordObject

Returns the value of attribute record.



59
60
61
# File 'app/models/accountability/inventory.rb', line 59

def record
  @record
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'app/models/accountability/inventory.rb', line 77

def available?
  return @available unless @available.nil?

  @available = inventory.available_source_ids.include? record.id
end

#priceObject



71
72
73
74
75
# File 'app/models/accountability/inventory.rb', line 71

def price
  # Iterating pre-loaded content is faster with Ruby than an N+1 in SQL
  price_override = record.price_overrides.find { |override| override.product_id == product.id }
  price_override&.price || product.price
end

#unavailable?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/accountability/inventory.rb', line 83

def unavailable?
  !available?
end