Sha256: 7366bb762f8d8215786f88eed6375edd6fc5d9f76df84a0fccfdf75f81824144

Contents?: true

Size: 1.02 KB

Versions: 12

Compression:

Stored size: 1.02 KB

Contents

class Cart < ActiveRecord::Base
  belongs_to :shopper, :polymorphic => true # for extra persistance
  has_many :cart_items, :dependent => :destroy

  include Carter::Cart
  extend Carter::ActiveRecord::Extensions

  attr_accessor :gateway_response
  
  def add_item(cartable, quantity = 1, owner=nil)
    existing_cart_item = cart_item_for_cartable_and_owner(cartable, owner)
    Cart.transaction do
      if existing_cart_item.blank?
        cart_items.create!(:cartable => cartable, :name => cartable.cartable_name, :price => cartable.cartable_price, :quantity => quantity, :owner => owner)
      else
        if cartable.allow_multiples?
          existing_cart_item.update_attributes(:quantity => existing_cart_item.quantity + quantity)
        else
          raise Carter::MultipleCartItemsNotAllowed, "#{cartable.cartable_name} is already in your basket"
        end
      end
    end
  end
  
  # TODO cache this value
  def total
    cart_items.reload.map.sum(&:total_price).to_money
  end
  
  def empty?
    cart_items.blank?
  end
  
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
carter-0.8.1 app/models/cart.rb
carter-0.8.0 app/models/cart.rb
carter-0.7.2 app/models/cart.rb
carter-0.7.1 app/models/cart.rb
carter-0.7.0 app/models/cart.rb
carter-0.6.4 app/models/cart.rb
carter-0.6.3 app/models/cart.rb
carter-0.6.2 app/models/cart.rb
carter-0.6.1 app/models/cart.rb
carter-0.6.0 app/models/cart.rb
carter-0.5.6 app/models/cart.rb
carter-0.5.5 app/models/cart.rb