Sha256: 5e60b765095b2a13cac28c9d7ff4e65b4be6d30898d75cc0015d0f319bd31e6d

Contents?: true

Size: 1.75 KB

Versions: 23

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, class_name: 'Effective::Order'
    belongs_to :purchasable, polymorphic: true

    # Attributes
    # name                  :string
    # quantity              :integer
    # price                 :integer, default: 0
    # tax_exempt            :boolean
    # timestamps

    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
    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

23 entries across 23 versions & 1 rubygems

Version Path
effective_orders-4.6.3 app/models/effective/order_item.rb
effective_orders-4.6.2 app/models/effective/order_item.rb
effective_orders-4.6.0 app/models/effective/order_item.rb
effective_orders-4.5.12 app/models/effective/order_item.rb
effective_orders-4.5.11 app/models/effective/order_item.rb
effective_orders-4.5.10 app/models/effective/order_item.rb
effective_orders-4.5.9 app/models/effective/order_item.rb
effective_orders-4.5.8 app/models/effective/order_item.rb
effective_orders-4.5.7 app/models/effective/order_item.rb
effective_orders-4.5.6 app/models/effective/order_item.rb
effective_orders-4.5.5 app/models/effective/order_item.rb
effective_orders-4.5.4 app/models/effective/order_item.rb
effective_orders-4.5.3 app/models/effective/order_item.rb
effective_orders-4.5.2 app/models/effective/order_item.rb
effective_orders-4.5.1 app/models/effective/order_item.rb
effective_orders-4.5.0 app/models/effective/order_item.rb
effective_orders-4.4.10 app/models/effective/order_item.rb
effective_orders-4.4.9 app/models/effective/order_item.rb
effective_orders-4.4.8 app/models/effective/order_item.rb
effective_orders-4.4.7 app/models/effective/order_item.rb