Sha256: 126d54289ca4c78b9392898c1150f6b60142c6d092a43be7bc9e0012520d9c36

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 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
  
  before_validation         :adjust_quantity, :copy_price
  
  validates_presence_of     :item, :item_price
  
  validates_uniqueness_of   :item_id, :scope => [ :order_id, :item_type ]
  
  validates_numericality_of :item_price, :greater_than => 0.00,    :allow_nil => true,     :precisions => 2
  
  def price
    (item_price.to_f * self.quantity).to_f
  end
  
  def weight
    warn 'Not yet fully implemented'
    (item.weight.to_f * self.quantity.to_f).to_f
  end
  
  # Overloads the base to_json to return what we want
  def to_json(*attrs); super self.class.params; end
  
  class << self
    
    # Returns attributes attached to the product
    def attrs
      [ :id, :quantity ]
    end
    
    # Returns methods with usefuly information
    def methds
      [ :price, :weight ]
    end
    
    # Returns a custom hash of attributes on the product
    def params
      { :only  => attrs, :methods => methds }
    end
    
  end
  
protected
  
  def adjust_quantity
    self.quantity = [1,self.quantity].max
  end
  
  def copy_price
    self.item_price = item.price unless item_price.present?
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
radiant-shop-extension-0.90.4 app/models/shop_line_item.rb
radiant-shop-extension-0.90.2 app/models/shop_line_item.rb
radiant-shop-extension-0.90.1 app/models/shop_line_item.rb
radiant-shop-extension-0.90.0 app/models/shop_line_item.rb