Class: LineItem

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/line_item.rb

Instance Method Summary collapse

Instance Method Details

#adjust_quantityObject



39
40
41
# File 'app/models/line_item.rb', line 39

def adjust_quantity    
  self.quantity = 0 if self.quantity.nil? || self.quantity < 0
end

#decrement_quantityObject



30
31
32
# File 'app/models/line_item.rb', line 30

def decrement_quantity
  self.quantity -= 1
end

#increment_quantityObject



26
27
28
# File 'app/models/line_item.rb', line 26

def increment_quantity
  self.quantity += 1
end

#totalObject Also known as: amount



34
35
36
# File 'app/models/line_item.rb', line 34

def total
  self.price * self.quantity  
end

#validateObject



14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/line_item.rb', line 14

def validate
  unless quantity && quantity >= 0
    errors.add(:quantity, "must be a non-negative value")
  end
  # avoid reload of order.inventory_units by using direct lookup
  unless Spree::Config[:allow_backorders]                               ||
         order   && InventoryUnit.order_id_equals(order).first.present? || 
         variant && quantity <= variant.on_hand                         
    errors.add(:quantity, " is too large-- stock on hand cannot cover requested quantity!")
  end
end