active_cart 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.8
1
+ 0.0.9
@@ -184,7 +184,8 @@ module ActiveCart
184
184
  #
185
185
  def new_from_item(item)
186
186
  cart_item = self.new
187
- item.class.columns.each { |col| cart_item.send((col.to_s + "=").to_sym, item.send(col)) if cart_item.respond_to?((col.to_s + "=").to_sym) }
187
+ item.class.columns.map {|col| col.name }.each { |col| cart_item.send((col.to_s + "=").to_sym, item.send(col)) if cart_item.respond_to?((col.to_s + "=").to_sym) }
188
+ cart_item.original = item
188
189
  cart_item
189
190
  end
190
191
 
@@ -42,6 +42,26 @@ module ActiveCart
42
42
 
43
43
  extend Forwardable
44
44
  # Storage Engine Array delegators
45
+ # TODO: Consolidate - not all of these make sense, and some need overriding so they take the callbacks into account
46
+ #
47
+ # Candidates for removal:
48
+ # delete_at
49
+ # delete_if
50
+ # []=
51
+ # replace
52
+ # insert
53
+ # shift
54
+ # unshift
55
+ # pop
56
+ #
57
+ # Aliases
58
+ # delete => remove_from_cart(obj, 1)
59
+ # << => add_to_cart(obj, 1)
60
+ # push => add_to_cart(obj, 1)
61
+ #
62
+ # Overrides
63
+ # clear => .each { |obj| self.remove_from_cart(obj) }
64
+ #
45
65
  def_delegators :@storage_engine, :[], :<<, :[]=, :at, :clear, :collect, :map, :delete, :delete_at, :each, :each_index, :empty?, :eql?, :first, :include?, :index, :inject, :last, :length, :pop, :push, :shift, :size, :unshift
46
66
 
47
67
  # Returns a unique id for the invoice. It's upto the storage engine to generate and track these numbers
@@ -84,11 +104,9 @@ module ActiveCart
84
104
  # 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.
85
105
  #
86
106
  def add_to_cart(item, quantity = 1)
87
- return false unless item.before_add_to_cart(quantity) if item.respond_to?(:before_add_to_cart)
88
- return false unless @storage_engine.before_add_to_cart(item, quantity) if @storage_engine.respond_to?(:before_add_to_cart)
89
- @storage_engine.add_to_cart(item, quantity)
90
- @storage_engine.after_add_to_cart(item, quantity) if @storage_engine.respond_to?(:after_add_to_cart)
91
- item.after_add_to_cart(quantity) if item.respond_to?(:after_add_to_cart)
107
+ with_callbacks(:add_to_cart, item, quantity) do
108
+ @storage_engine.add_to_cart(item, quantity)
109
+ end
92
110
  end
93
111
 
94
112
  # 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
@@ -106,11 +124,9 @@ module ActiveCart
106
124
  # 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.
107
125
  #
108
126
  def remove_from_cart(item, quantity = 1)
109
- return false unless item.before_remove_from_cart(quantity) if item.respond_to?(:before_remove_from_cart)
110
- return false unless @storage_engine.before_remove_from_cart(item, quantity) if @storage_engine.respond_to?(:before_remove_from_cart)
111
- @storage_engine.remove_from_cart(item, quantity)
112
- @storage_engine.after_remove_from_cart(item, quantity) if @storage_engine.respond_to?(:after_remove_from_cart)
113
- item.after_remove_from_cart(quantity) if item.respond_to?(:after_remove_from_cart)
127
+ with_callbacks(:remove_from_cart, item, quantity) do
128
+ @storage_engine.remove_from_cart(item, quantity)
129
+ end
114
130
  end
115
131
 
116
132
  # Returns the total of the cart. This includes all the order_total calculations
@@ -134,5 +150,16 @@ module ActiveCart
134
150
  super
135
151
  end
136
152
  end
153
+
154
+ private
155
+ def with_callbacks(type, item, quantity, &blk)
156
+ return false unless item.send("before_#{type}".to_sym, quantity) if item.respond_to?("before_#{type}".to_sym)
157
+ return false unless @storage_engine.send("before_#{type}".to_sym, item, quantity) if @storage_engine.respond_to?("before_#{type}".to_sym)
158
+
159
+ yield
160
+
161
+ @storage_engine.send("after_#{type}".to_sym, item, quantity) if @storage_engine.respond_to?("after_#{type}".to_sym)
162
+ item.send("after_#{type}".to_sym, quantity) if item.respond_to?("after_#{type}".to_sym)
163
+ end
137
164
  end
138
165
  end
@@ -20,7 +20,7 @@ module ActiveCart
20
20
  base.aasm_state :shopping, :enter => :enter_shopping, :exit => :exit_shopping
21
21
  base.aasm_state :checkout, :enter => :enter_checkout, :exit => :exit_checkout
22
22
  base.aasm_state :verifying_payment, :enter => :enter_verifying_payment, :exit => :exit_verifying_payment
23
- base.aasm_state :completed, :enter => :enter_completed, :ext => :exit_completed
23
+ base.aasm_state :completed, :enter => :enter_completed, :exit => :exit_completed
24
24
  base.aasm_state :failed, :enter => :enter_failed, :exit => :exit_failed
25
25
 
26
26
  base.aasm_event :continue_shopping do
@@ -292,6 +292,18 @@ class ActsAsCartTest < ActiveSupport::TestCase
292
292
  end
293
293
  end
294
294
 
295
+ context 'new_from_item' do
296
+ should 'copy all the relevent paramaters from the supplied item into a new cart item' do
297
+ @item = Item.make
298
+ @cart_item = CartItem.new_from_item(@item)
299
+ assert @cart_item.valid?
300
+ assert_equal @item.name, @cart_item.name
301
+ assert_equal @item.price, @cart_item.price
302
+ assert_equal @item, @cart_item.original
303
+ end
304
+ end
305
+
306
+
295
307
  context 'acts_as_order_total' do
296
308
  context 'configuration' do
297
309
  should 'set defaults' do
@@ -17,7 +17,7 @@ class CartTest < ActiveSupport::TestCase
17
17
  assert_equal 1, @cart.quantity
18
18
  end
19
19
 
20
- should 'halt and return false if beforee_add_to_cart returnds false' do
20
+ should 'halt and return false if before_add_to_cart returns false' do
21
21
  item = TestItem.new
22
22
  item.expects(:before_add_to_cart).with(1).returns(false)
23
23
  assert !@cart.add_to_cart(item, 1)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_cart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Myles Eftos
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-13 00:00:00 +08:00
12
+ date: 2010-03-25 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency