Sha256: b7a1c9e706e8578b78acf205736375b379e45e0791d850a83d8a40ea948bd69d
Contents?: true
Size: 1.75 KB
Versions: 3
Compression:
Stored size: 1.75 KB
Contents
module Effective class OrderItem < ActiveRecord::Base self.table_name = EffectiveOrders.order_items_table_name.to_s belongs_to :order belongs_to :purchasable, polymorphic: true effective_resource do name :string quantity :integer price :integer tax_exempt :boolean timestamps end validates :purchasable, associated: true, presence: true accepts_nested_attributes_for :purchasable validates :name, presence: true validates :quantity, presence: true, numericality: { greater_than: 0 } validates :price, presence: true validates :tax_exempt, inclusion: { in: [true, false] } scope :purchased, -> { where(order_id: Effective::Order.purchased) } scope :purchased_by, lambda { |user| where(order_id: Effective::Order.purchased_by(user)) } def to_s ((quantity || 0) > 1 ? "#{quantity}x #{name}" : name) || 'order item' end def purchased_download_url purchasable&.purchased_download_url end def subtotal price * quantity end def quantity self[:quantity] || 1 end def tax return 0 if tax_exempt? raise 'parent Effective::Order must have a tax_rate to compute order item tax' unless order.try(:tax_rate).present? (subtotal * order.tax_rate / 100.0).round(0).to_i end def total return subtotal if tax_exempt? raise 'parent Effective::Order must have a tax_rate to compute order item total' unless order.try(:tax_rate).present? subtotal + tax end def price=(value) if value.kind_of?(Integer) super else raise 'expected price to be an Integer representing the number of cents.' end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
effective_orders-5.0.1 | app/models/effective/order_item.rb |
effective_orders-4.6.1 | app/models/effective/order_item.rb |
effective_orders-5.0.0 | app/models/effective/order_item.rb |