Sha256: 307ee00fa012669557cebcc88ab98567d5a67c3e3f1598b4c54fd46a4c69f2b5

Contents?: true

Size: 902 Bytes

Versions: 6

Compression:

Stored size: 902 Bytes

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
  
  def validate
    unless quantity && quantity >= 0
      errors.add(:quantity, "must be a positive value")
    end
    unless quantity <= variant.on_hand || Spree::Config[:allow_backorders]
      errors.add(:quantity, " for #{variant.product.name} 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
  
  def adjust_quantity    
    self.quantity = 0 if self.quantity < 0
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spree-0.8.4 app/models/line_item.rb
spree-0.8.5 app/models/line_item.rb
spree-0.8.0 app/models/line_item.rb
spree-0.8.1 app/models/line_item.rb
spree-0.8.2 app/models/line_item.rb
spree-0.8.3 app/models/line_item.rb