Module: ActiveCart::CartStorage
- Included in:
- StorageEngines::Memory
- Defined in:
- lib/active_cart/cart_storage.rb
Overview
The CartStorage 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, verifying_payment or failed 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, verifying_payment or failed to shopping
It will fire before_ and after callbacks with the same name as the transitions
Class Method Summary (collapse)
-
+ (Object) included(base)
:nodoc:.
Instance Method Summary (collapse)
-
- (Object) add_to_cart(item, quantity = 1, options = {})
Adds an item to the cart.
-
- (Object) enter_checkout
Called when entering the checkout state.
-
- (Object) enter_completed
Called when entering the completed state.
-
- (Object) enter_failed
Called when entering the failed state.
-
- (Object) enter_shopping
Called when entering the shopping state.
-
- (Object) enter_verifying_payment
Called when entering the verifying_payment state.
-
- (Object) exit_checkout
Called when existing the checkout state.
-
- (Object) exit_completed
Called when existing the completed state.
-
- (Object) exit_failed
Called when existing the failed state.
-
- (Object) exit_shopping
Called when exiting the shopping state.
-
- (Object) exit_verifying_payment
Called when exiting the verifying_payment state.
-
- (Object) guard_check_payment
Guard check payment.
-
- (Object) guard_checkout
Guard checkout.
-
- (Object) guard_continue_shopping
Guard continue shopping.
-
- (Object) guard_payment_failed
Guard payment failed.
-
- (Object) guard_payment_successful
Guard payment successful.
-
- (Object) invoice_id
Returns the unique invoice_id for this cart instance.
-
- (Object) quantity
Returns the number of items in the cart.
-
- (Object) remove_from_cart(item, quantity = 1, option = {})
Removes an item from the cart (identified by the id of the item).
- - (Object) state
-
- (Object) sub_total
Returns the sub-total of all the items in the cart.
Class Method Details
+ (Object) included(base)
:nodoc:
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/active_cart/cart_storage.rb', line 16 def self.included(base) #:nodoc: base.send :include, AASM base.aasm_initial_state :shopping base.aasm_state :shopping, :enter => :enter_shopping, :exit => :exit_shopping base.aasm_state :checkout, :enter => :enter_checkout, :exit => :exit_checkout base.aasm_state :verifying_payment, :enter => :enter_verifying_payment, :exit => :exit_verifying_payment base.aasm_state :completed, :enter => :enter_completed, :exit => :exit_completed base.aasm_state :failed, :enter => :enter_failed, :exit => :exit_failed base.aasm_event :continue_shopping do transitions :from => [ :checkout, :verifying_payment, :failed ], :to => :shopping, :guard => :guard_continue_shopping end base.aasm_event :checkout do transitions :from => [ :shopping, :verifying_payment, :failed ], :to => :checkout, :guard => :guard_checkout end base.aasm_event :check_payment do transitions :from => :checkout, :to => :verifying_payment, :guard => :guard_check_payment end base.aasm_event :payment_successful do transitions :from => :verifying_payment, :to => :completed, :guard => :guard_payment_successful end base.aasm_event :payment_failed do transitions :from => :verifying_payment, :to => :failed, :guard => :guard_payment_failed end end |
Instance Method Details
- (Object) add_to_cart(item, quantity = 1, options = {})
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].quantity # => 7
@cart[1] # => nil
@cart.add_to_cart(item_2, 4)
@cart.quantity => 100
@cart[0].quantity # => 7
@cart[1].quantity # => 4
153 154 155 156 157 158 159 160 161 |
# File 'lib/active_cart/cart_storage.rb', line 153 def add_to_cart(item, quantity = 1, = {}) if self.include?(item) index = self.index(item) self.at(index).quantity += quantity else item.quantity += quantity self << item end end |
- (Object) enter_checkout
Called when entering the checkout state
87 |
# File 'lib/active_cart/cart_storage.rb', line 87 def enter_checkout; end |
- (Object) enter_completed
Called when entering the completed state
103 |
# File 'lib/active_cart/cart_storage.rb', line 103 def enter_completed; end |
- (Object) enter_failed
Called when entering the failed state
111 |
# File 'lib/active_cart/cart_storage.rb', line 111 def enter_failed; end |
- (Object) enter_shopping
Called when entering the shopping state
79 |
# File 'lib/active_cart/cart_storage.rb', line 79 def enter_shopping; end |
- (Object) enter_verifying_payment
Called when entering the verifying_payment state
95 |
# File 'lib/active_cart/cart_storage.rb', line 95 def ; end |
- (Object) exit_checkout
Called when existing the checkout state
91 |
# File 'lib/active_cart/cart_storage.rb', line 91 def exit_checkout; end |
- (Object) exit_completed
Called when existing the completed state
107 |
# File 'lib/active_cart/cart_storage.rb', line 107 def exit_completed; end |
- (Object) exit_failed
Called when existing the failed state
115 |
# File 'lib/active_cart/cart_storage.rb', line 115 def exit_failed; end |
- (Object) exit_shopping
Called when exiting the shopping state
83 |
# File 'lib/active_cart/cart_storage.rb', line 83 def exit_shopping; end |
- (Object) exit_verifying_payment
Called when exiting the verifying_payment state
99 |
# File 'lib/active_cart/cart_storage.rb', line 99 def ; end |
- (Object) guard_check_payment
Guard check payment. If this method returns false, the transition will be halted
61 62 63 |
# File 'lib/active_cart/cart_storage.rb', line 61 def guard_check_payment true end |
- (Object) guard_checkout
Guard checkout. If this method returns false, the transition will be halted
55 56 57 |
# File 'lib/active_cart/cart_storage.rb', line 55 def guard_checkout true end |
- (Object) guard_continue_shopping
Guard continue shopping. If this method returns false, the transition will be halted
49 50 51 |
# File 'lib/active_cart/cart_storage.rb', line 49 def guard_continue_shopping true end |
- (Object) guard_payment_failed
Guard payment failed. If this method returns false, the transition will be halted
73 74 75 |
# File 'lib/active_cart/cart_storage.rb', line 73 def guard_payment_failed true end |
- (Object) guard_payment_successful
Guard payment successful. If this method returns false, the transition will be halted
67 68 69 |
# File 'lib/active_cart/cart_storage.rb', line 67 def guard_payment_successful true end |
- (Object) invoice_id
Returns the unique invoice_id for this cart instance. This MUST be overriden by the concrete class this module is mixed into, otherwise you will get a NotImplementedError
120 121 122 |
# File 'lib/active_cart/cart_storage.rb', line 120 def invoice_id raise NotImplementedError end |
- (Object) quantity
Returns the number of items in the cart. It takes into account the individual quantities of each item, eg if there are 3 items in the cart, each with a quantity of 2, this will return 6
134 135 136 |
# File 'lib/active_cart/cart_storage.rb', line 134 def quantity inject(0) { |t, item| t + item.quantity } end |
- (Object) remove_from_cart(item, quantity = 1, option = {})
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
@cart.add_to_cart(item, 3)
@cart[0].quantity # => 3
@cart_remove_from_cart(item, :all)
@cart[[0].quantity # => 0
176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/active_cart/cart_storage.rb', line 176 def remove_from_cart(item, quantity = 1, option = {}) if self.include?(item) index = self.index(item) quantity = self.at(index).quantity if quantity == :all if (existing = self.at(index)).quantity - quantity > 0 existing.quantity = existing.quantity - quantity else self.delete_at(index) end end end |
- (Object) state
190 191 192 |
# File 'lib/active_cart/cart_storage.rb', line 190 def state return aasm_current_state end |
- (Object) sub_total
Returns the sub-total of all the items in the cart. Usually returns a float.
@cart.sub_total # => 100.00
128 129 130 |
# File 'lib/active_cart/cart_storage.rb', line 128 def sub_total inject(0) { |t, item| t + (item.quantity * item.price.to_f) } end |