Sha256: a6e4de22725d290047bfb959eb4be7ce996d47c6d8de6e771ba97b27b839cc7d

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

class ShopLineItem < ActiveRecord::Base
  
  belongs_to  :order,       :class_name   => 'ShopOrder'
  has_one     :customer,    :class_name   => 'ShopCustomer', :through => :order, :source => :customer
  belongs_to  :item,        :polymorphic  => true
  
  has_many :discountables, :class_name => 'ShopDiscountable', :foreign_key  => :discounted_id
  has_many   :discounts,   :class_name => 'ShopDiscount',     :through      => :discountables
  
  before_validation       :adjust_quantity
  validates_uniqueness_of :item_id, :scope => [ :order_id, :item_type ]
  validates_presence_of   :item
  
  def price
    price = value
    price -= discounted
    
    # We never want to return a negative cost
    [0.00,price.to_f].max
  end
  
  def value
    price = (item.price * self.quantity)
  end
  
  def discounted
    (item.price * self.quantity * discount)
  end
  
  def weight
    (item.weight.to_f * self.quantity.to_f).to_f
  end
  
  def discount
    discount = BigDecimal.new('0.00')
    self.discounts.map { |d| discount += d.amount }
    
    # Convert to a percentage
    discount * 0.01
  end
  
  class << self
    
    def params
      [ :id, :quantity ]
    end
    
  end
  
private
  
  def adjust_quantity
    self.quantity = [1,self.quantity].max
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
radiant-shop-extension-0.11.6 app/models/shop_line_item.rb