Class: Cart

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Carter::ActiveRecord::Extensions
Includes:
Carter::Cart
Defined in:
app/models/cart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Carter::ActiveRecord::Extensions

money_composed_column, money_constructor, money_converter

Methods included from Carter::Cart

included

Instance Attribute Details

#gateway_responseObject

Returns the value of attribute gateway_response.



8
9
10
# File 'app/models/cart.rb', line 8

def gateway_response
  @gateway_response
end

Instance Method Details

#add_item(cartable, quantity = 1, owner = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/cart.rb', line 10

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

#empty?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/models/cart.rb', line 30

def empty?
  cart_items.blank?
end

#totalObject

TODO cache this value



26
27
28
# File 'app/models/cart.rb', line 26

def total
  cart_items.reload.map.sum(&:total_price).to_money
end