Class: ActiveCart::Cart

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/active_cart/cart.rb

Overview

The Cart class is the core class in ActiveCart. It is a singleton (so you can only have one cart per application), that gets setup initially by passing in a storage engine instance. Storage engines abstract away the storage of the cart, and is left as an exercise to the user. See the Storage engine docs for details.

The Cart class also takes order_total objects, which will calculate order totals. it may include thinkgs like shipping, or gift vouchers etc. See the Order Total docs for details.

The Cart object delegates a number of Array methods: :[], :<<, :[]=, :at, :clear, :collect, :map, :delete, :delete_at, :each, :each_index, :empty?, :eql?, :first, :include?, :index, :inject, :last, :length, :pop, :push, :shift, :size, :unshift

The CartEngine object uses a state machine to track the state of the cart. The default states are: shopping, checkout, verifying_payment, completed, failed. It exposed the following transitions: continue_shopping, checkout, check_payment, payment_successful, payment_failed

@cart.checkout! # transitions from shopping or verifying_payment to checkout
@cart.check_payment! # transistions from checkout to verifying_payment
@cart.payment_successful! # transitions from verifying_payment to completed
@cart.payment_failed! # transitions from verifying_payment to failed
@cart.continue_shopping! # transitions from checkout or verifying_payment to shopping

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_engine, &block) ⇒ Cart

You need to supply a storage engine. An optional block can be given which allows you to add order total items.

A typical initialization block might look like this

@cart = Cart.new(MyAwesomeStorageEngine.new) do |o|
  o << ShippingOrderTotal.new
  o << GstOrderTotal.new
end


34
35
36
37
38
39
40
41
# File 'lib/active_cart/cart.rb', line 34

def initialize(storage_engine, &block)
  @storage_engine = storage_engine
  @order_total_calculators = OrderTotalCollection.new(self)

  if block_given?
    yield order_total_calculators
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

:nodoc



169
170
171
172
173
174
175
176
# File 'lib/active_cart/cart.rb', line 169

def method_missing(symbol, *args)
  # This allows developers to add extra aasm event transaction, and still allow them to called from the cart
  if @storage_engine.state_paths.events.include?(symbol.to_s[0..-2].to_sym)
    @storage_engine.send(symbol)
  else
    super
  end
end

Instance Attribute Details

#customerObject

Returns the value of attribute customer.



22
23
24
# File 'lib/active_cart/cart.rb', line 22

def customer
  @customer
end

#order_total_calculatorsObject

Returns the value of attribute order_total_calculators.



22
23
24
# File 'lib/active_cart/cart.rb', line 22

def order_total_calculators
  @order_total_calculators
end

#storage_engineObject

Returns the value of attribute storage_engine.



22
23
24
# File 'lib/active_cart/cart.rb', line 22

def storage_engine
  @storage_engine
end

Instance Method Details

#add_to_cart(item, quantity = 1, options = {}) ⇒ Object

Adds an item to the cart. If the item already exists in the cart (identified by the id of the item), then the quantity will be increased but the supplied quantity (default: 1)

@cart.add_to_cart(item, 5)
@cart.quantity # => 5

@cart.add_to_cart(item, 2)
@cart.quantity # => 7
@cart[0].size # => 7
@cart[1] # => nil

@cart.add_to_cart(item_2, 4)
@cart.quantity => 100
@cart[0].size # => 7
@cart[1].size # => 4

Callbacks:

Calls the storage engines before_add_to_cart(item, quantity) and after_add_to_cart(item, quantity) methods (if they exist). If before_add_to_cart returns false, the add will be halted. Calls the items before_add_to_item(quantity) and after_add_to_cart(quantity) methods (if they exist). If before_add_to_cart returns false, the add will be halted.



106
107
108
109
110
# File 'lib/active_cart/cart.rb', line 106

def add_to_cart(item, quantity = 1, options = {})
  with_callbacks(:add_to_cart, item, quantity, options) do
    @storage_engine.add_to_cart(item, quantity, options)
  end
end

#invoice_idObject

Returns a unique id for the invoice. It’s upto the storage engine to generate and track these numbers



69
70
71
# File 'lib/active_cart/cart.rb', line 69

def invoice_id
  @storage_engine.invoice_id
end

#quantityObject

Returns the sub-total of all the items in the cart. Usually returns a float.

@cart.sub_total # => 100.00


77
78
79
# File 'lib/active_cart/cart.rb', line 77

def quantity
  @storage_engine.quantity
end

#remove_from_cart(item, quantity = 1, options = {}) ⇒ Object

Removes an item from the cart (identified by the id of the item). If the supplied quantity is greater than equal to the number in the cart, the item will be removed, otherwise the quantity will simply be decremented by the supplied amount

@cart.add_to_cart(item, 5)
@cart[0].quantity # => 5
@cart.remove_from_cart(item, 3)
@cart[0].quantity # => 2
@cart.remove_from_cart(item, 2)
@cart[0] # => nil

Callbacks:

Calls the storage engines before_remove_from_cart(item, quantity) and after_remove_from_cart(item, quantity) methods (if they exist). If before_remove_from_cart returns false, the remove will be halted. Calls the items before_remove_from_item(quantity) and after_remove_from_cart(quantity) methods (if they exist). If before_remove_from_cart returns false, the remove will be halted.



150
151
152
153
154
# File 'lib/active_cart/cart.rb', line 150

def remove_from_cart(item, quantity = 1, options = {})
  with_callbacks(:remove_from_cart, item, quantity, options) do
    @storage_engine.remove_from_cart(item, quantity, options)
  end
end

#stateObject

Returns the current state of the cart storage engine



164
165
166
# File 'lib/active_cart/cart.rb', line 164

def state
  storage_engine.state
end

#sub_totalObject

Returns the subtotal of cart, which is effectively the total of all the items multiplied by their quantites. Does NOT include order_totals



82
83
84
# File 'lib/active_cart/cart.rb', line 82

def sub_total
  @storage_engine.sub_total
end

#totalObject

Returns the total of the cart. This includes all the order_total calculations



158
159
160
# File 'lib/active_cart/cart.rb', line 158

def total
  sub_total + order_total_calculators.total
end

#update_cart(item, quantity = 1, options = {}) ⇒ Object

Sets the quantity of an item to the supplied value

@cart.add_to_cart(item, 5)
@cart[0].quantity # => 5
@cart.update_cart(item, 15)
@cart[0].quantity # => 15
@cart.update_cart(item, 2)
@cart[0].quantity # => 2


121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/active_cart/cart.rb', line 121

def update_cart(item, quantity = 1, options = {})
  if @storage_engine.include?(item)
    index = @storage_engine.index(item)
    diff = quantity - @storage_engine.at(index).quantity
    
    if diff < 0
      return remove_from_cart(item, -1 * diff, options)
    else
      return add_to_cart(item, diff, options)
    end
  else
    return add_to_cart(item, quantity, options)
  end
end