Class: LineItem
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- LineItem
- Defined in:
- app/models/line_item.rb
Instance Method Summary collapse
- #adjust_quantity ⇒ Object
- #amount ⇒ Object (also: #total)
- #copy_price ⇒ Object
- #decrement_quantity ⇒ Object
-
#increment_quantity ⇒ Object
def meta_validation_of_quantities unless quantity && quantity >= 0 errors.add(:quantity, I18n.t(“validation.must_be_non_negative”)) end # avoid reload of order.inventory_units by using direct lookup unless !Spree::Config || Spree::Config || order && InventoryUnit.order_id_equals(order).first.present? || variant && quantity <= variant.on_hand errors.add(:quantity, I18n.t(“validation.is_too_large”) + “ (#selfself.variantself.variant.name)”) end.
Instance Method Details
#adjust_quantity ⇒ Object
57 58 59 |
# File 'app/models/line_item.rb', line 57 def adjust_quantity self.quantity = 0 if self.quantity.nil? || self.quantity < 0 end |
#amount ⇒ Object Also known as: total
52 53 54 |
# File 'app/models/line_item.rb', line 52 def amount self.price * self.quantity end |
#copy_price ⇒ Object
23 24 25 |
# File 'app/models/line_item.rb', line 23 def copy_price self.price = variant.price if variant && self.price.nil? end |
#decrement_quantity ⇒ Object
48 49 50 |
# File 'app/models/line_item.rb', line 48 def decrement_quantity self.quantity -= 1 end |
#increment_quantity ⇒ Object
def meta_validation_of_quantities
unless quantity && quantity >= 0
errors.add(:quantity, I18n.t("validation.must_be_non_negative"))
end
# avoid reload of order.inventory_units by using direct lookup
unless !Spree::Config[:track_inventory_levels] ||
Spree::Config[:allow_backorders] ||
order && InventoryUnit.order_id_equals(order).first.present? ||
variant && quantity <= variant.on_hand
errors.add(:quantity, I18n.t("validation.is_too_large") + " (#{self.variant.name})")
end
if shipped_count = order.shipped_units.nil? ? nil : order.shipped_units[variant]
errors.add(:quantity, I18n.t("validation.cannot_be_less_than_shipped_units") ) if quantity < shipped_count
end
end
44 45 46 |
# File 'app/models/line_item.rb', line 44 def increment_quantity self.quantity += 1 end |