Sha256: 4f8f434a10fb114a2653757fad4fbdd787c7a6d7eb0a432575e63bf0c8047477

Contents?: true

Size: 1019 Bytes

Versions: 5

Compression:

Stored size: 1019 Bytes

Contents

module Effective
  class CartItem < ActiveRecord::Base
    self.table_name = EffectiveOrders.cart_items_table_name.to_s

    belongs_to :cart
    belongs_to :purchasable, :polymorphic => true

    structure do
      quantity    :integer, :validates => [:presence]
      timestamps
    end

    validates_presence_of :purchasable

    delegate :title, :tax_exempt, :tax_rate, :to => :purchasable

    default_scope -> { order(:updated_at) }

    def price
      if (purchasable.price || 0).kind_of?(Integer)
        purchasable.price || 0
      else
        ActiveSupport::Deprecation.warn('price is a non-integer. It should be an Integer representing the number of cents.  Continuing with (price * 100.0).floor conversion') unless EffectiveOrders.silence_deprecation_warnings
        (purchasable.price * 100.0).floor rescue 0
      end
    end

    def subtotal
      price * quantity
    end

    def tax
      tax_exempt ? 0 : (subtotal * tax_rate).ceil
    end

    def total
      subtotal + tax
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
effective_orders-1.2.0 app/models/effective/cart_item.rb
effective_orders-1.1.2 app/models/effective/cart_item.rb
effective_orders-1.1.1 app/models/effective/cart_item.rb
effective_orders-1.1.0 app/models/effective/cart_item.rb
effective_orders-1.0.0 app/models/effective/cart_item.rb