Sha256: 7e87dabdd5e9da9c82ffe5ef44fdc7ee652e9de2d50e082e32c2be3c1a799eec

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

class LineItem < ActiveRecord::Base
  before_validation :adjust_quantity
  belongs_to :order
  belongs_to :variant
  
  has_one :product, :through => :variant

  validates_presence_of :variant
  validates_numericality_of :quantity, :only_integer => true, :message => "must be an integer"
  validates_numericality_of :price

  attr_accessible :quantity
  
  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
  
  def increment_quantity
    self.quantity += 1
  end

  def decrement_quantity
    self.quantity -= 1
  end
  
  def total
    self.price * self.quantity  
  end
  alias amount total
  
  def adjust_quantity    
    self.quantity = 0 if self.quantity.nil? || self.quantity < 0
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
spree-enriquez-0.9.4 app/models/line_item.rb
spree-0.9.4 app/models/line_item.rb
spree-0.9.3 app/models/line_item.rb
spree-0.9.2 app/models/line_item.rb
spree-0.9.1 app/models/line_item.rb
spree-0.9.0 app/models/line_item.rb