Sha256: 32ea44bf4e8fb58219de15e114d3dff2f109cf129d0c173357448af54f4a37ac

Contents?: true

Size: 1.77 KB

Versions: 40

Compression:

Stored size: 1.77 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

    delegate :purchased_download_url, to: :purchasable
    delegate :purchased?, :declined?, to: :order

    # 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 :sold, -> { joins(:order).where(orders: { state: EffectiveOrders::PURCHASED }) }
    scope :sold_by, lambda { |user| sold().where(seller_id: user.id) }

    def to_s
      (quantity || 0) > 1 ? "#{quantity}x #{name}" : name
    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

40 entries across 40 versions & 1 rubygems

Version Path
effective_orders-4.4.0 app/models/effective/order_item.rb
effective_orders-4.3.2 app/models/effective/order_item.rb
effective_orders-4.3.1 app/models/effective/order_item.rb
effective_orders-4.3.0 app/models/effective/order_item.rb
effective_orders-4.2.7 app/models/effective/order_item.rb
effective_orders-4.2.6 app/models/effective/order_item.rb
effective_orders-4.2.5 app/models/effective/order_item.rb
effective_orders-4.2.4 app/models/effective/order_item.rb
effective_orders-4.2.3 app/models/effective/order_item.rb
effective_orders-4.2.2 app/models/effective/order_item.rb
effective_orders-4.2.1 app/models/effective/order_item.rb
effective_orders-4.2.0 app/models/effective/order_item.rb
effective_orders-4.1.5 app/models/effective/order_item.rb
effective_orders-4.1.4 app/models/effective/order_item.rb
effective_orders-4.1.3 app/models/effective/order_item.rb
effective_orders-4.1.2 app/models/effective/order_item.rb
effective_orders-4.1.1 app/models/effective/order_item.rb
effective_orders-4.1.0 app/models/effective/order_item.rb
effective_orders-4.0.6 app/models/effective/order_item.rb
effective_orders-4.0.5 app/models/effective/order_item.rb