active_cart 0.0.11 → 0.0.12

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.11
1
+ 0.0.12
@@ -63,27 +63,27 @@ module ActiveCart
63
63
  end
64
64
 
65
65
  #:nodoc
66
- def find_cart_item(item)
66
+ def find_cart_item(item, options = {})
67
67
  self.send(:cart_items).find(:first, :conditions => [ 'original_id = ? AND original_type = ?', item.id, item.class.to_s ])
68
68
  end
69
69
 
70
70
  #:nodoc
71
- def add_to_cart(item, quantity = 1)
72
- cart_item = find_cart_item(item)
71
+ def add_to_cart(item, quantity = 1, options = {})
72
+ cart_item = find_cart_item(item, options)
73
73
  if cart_item
74
74
  cart_item.quantity += quantity
75
75
  cart_item.save!
76
76
  else
77
- cart_item = self.send(:cart_items).create!(self.aac_config[:cart_items].to_s.classify.constantize.new_from_item(item).attributes.merge(:quantity => quantity, :original_id => item.id, :original_type => item.class.to_s))
77
+ cart_item = self.send(:cart_items).create!(self.aac_config[:cart_items].to_s.classify.constantize.new_from_item(item, options).attributes.merge(:quantity => quantity, :original_id => item.id, :original_type => item.class.to_s))
78
78
  end
79
79
  self.reload
80
80
  end
81
81
 
82
82
  #:nodoc
83
- def remove_from_cart(item, quantity = 1)
84
- cart_item = find_cart_item(item)
83
+ def remove_from_cart(item, quantity = 1, options = {})
84
+ cart_item = find_cart_item(item, options)
85
85
  if cart_item
86
- quantity = cart_item.quantity if quantity == :all #TEST!!!
86
+ quantity = cart_item.quantity if quantity == :all
87
87
 
88
88
  if cart_item.quantity - quantity > 0
89
89
  cart_item.quantity = cart_item.quantity - quantity
@@ -96,18 +96,18 @@ module ActiveCart
96
96
  end
97
97
 
98
98
  #:nodoc
99
- def update_cart(item, quantity = 1)
100
- cart_item = find_cart_item(item)
99
+ def update_cart(item, quantity = 1, options = {})
100
+ cart_item = find_cart_item(item, options)
101
101
  if cart_item
102
102
  diff = quantity - cart_item.quantity
103
103
 
104
104
  if diff < 0
105
- return remove_from_cart(item, -1 * diff)
105
+ return remove_from_cart(item, -1 * diff, options)
106
106
  else
107
- return add_to_cart(item, diff)
107
+ return add_to_cart(item, diff, options)
108
108
  end
109
109
  else
110
- return add_to_cart(item, quantity)
110
+ return add_to_cart(item, quantity, options)
111
111
  end
112
112
  end
113
113
  end
@@ -200,11 +200,13 @@ module ActiveCart
200
200
  #
201
201
  # The default copies all the common attributes from the passed in item to new cart_item (Except id). Override it if you want to do something special.
202
202
  #
203
- def new_from_item(item)
203
+ def new_from_item(item, options = {})
204
204
  cart_item = self.new
205
205
  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) }
206
206
  cart_item.original = item
207
+ # TODO Add a callback
207
208
  cart_item
209
+ # TODO Add a callback
208
210
  end
209
211
 
210
212
  belongs_to self.aaci_config[:cart], :foreign_key => self.aaci_config[:foreign_key]
@@ -103,9 +103,9 @@ module ActiveCart
103
103
  # 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.
104
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.
105
105
  #
106
- def add_to_cart(item, quantity = 1)
107
- with_callbacks(:add_to_cart, item, quantity) do
108
- @storage_engine.add_to_cart(item, quantity)
106
+ def add_to_cart(item, quantity = 1, options = {})
107
+ with_callbacks(:add_to_cart, item, quantity, options) do
108
+ @storage_engine.add_to_cart(item, quantity, options)
109
109
  end
110
110
  end
111
111
 
@@ -118,18 +118,18 @@ module ActiveCart
118
118
  # @cart.update_cart(item, 2)
119
119
  # @cart[0].quantity # => 2
120
120
  #
121
- def update_cart(item, quantity = 1)
121
+ def update_cart(item, quantity = 1, options = {})
122
122
  if @storage_engine.include?(item)
123
123
  index = @storage_engine.index(item)
124
124
  diff = quantity - @storage_engine.at(index).quantity
125
125
 
126
126
  if diff < 0
127
- return remove_from_cart(item, -1 * diff)
127
+ return remove_from_cart(item, -1 * diff, options)
128
128
  else
129
- return add_to_cart(item, diff)
129
+ return add_to_cart(item, diff, options)
130
130
  end
131
131
  else
132
- return add_to_cart(item, quantity)
132
+ return add_to_cart(item, quantity, options)
133
133
  end
134
134
  end
135
135
 
@@ -147,9 +147,9 @@ module ActiveCart
147
147
  # 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.
148
148
  # 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.
149
149
  #
150
- def remove_from_cart(item, quantity = 1)
151
- with_callbacks(:remove_from_cart, item, quantity) do
152
- @storage_engine.remove_from_cart(item, quantity)
150
+ def remove_from_cart(item, quantity = 1, options = {})
151
+ with_callbacks(:remove_from_cart, item, quantity, options) do
152
+ @storage_engine.remove_from_cart(item, quantity, options)
153
153
  end
154
154
  end
155
155
 
@@ -176,14 +176,14 @@ module ActiveCart
176
176
  end
177
177
 
178
178
  private
179
- def with_callbacks(type, item, quantity, &blk)
180
- return false unless item.send("before_#{type}".to_sym, quantity) if item.respond_to?("before_#{type}".to_sym)
181
- return false unless @storage_engine.send("before_#{type}".to_sym, item, quantity) if @storage_engine.respond_to?("before_#{type}".to_sym)
179
+ def with_callbacks(type, item, quantity, options, &blk)
180
+ return false unless item.send("before_#{type}".to_sym, quantity, options) if item.respond_to?("before_#{type}".to_sym)
181
+ return false unless @storage_engine.send("before_#{type}".to_sym, quantity, options) if @storage_engine.respond_to?("before_#{type}".to_sym)
182
182
 
183
183
  yield
184
184
 
185
- @storage_engine.send("after_#{type}".to_sym, item, quantity) if @storage_engine.respond_to?("after_#{type}".to_sym)
186
- item.send("after_#{type}".to_sym, quantity) if item.respond_to?("after_#{type}".to_sym)
185
+ @storage_engine.send("after_#{type}".to_sym, quantity, options) if @storage_engine.respond_to?("after_#{type}".to_sym)
186
+ item.send("after_#{type}".to_sym, quantity, options) if item.respond_to?("after_#{type}".to_sym)
187
187
  end
188
188
  end
189
189
  end
@@ -150,7 +150,7 @@ module ActiveCart
150
150
  # @cart[0].quantity # => 7
151
151
  # @cart[1].quantity # => 4
152
152
  #
153
- def add_to_cart(item, quantity = 1)
153
+ def add_to_cart(item, quantity = 1, options = {})
154
154
  if self.include?(item)
155
155
  index = self.index(item)
156
156
  self.at(index).quantity += quantity
@@ -173,7 +173,7 @@ module ActiveCart
173
173
  # @cart[0].quantity # => 3
174
174
  # @cart_remove_from_cart(item, :all)
175
175
  # @cart[[0].quantity # => 0
176
- def remove_from_cart(item, quantity = 1)
176
+ def remove_from_cart(item, quantity = 1, option = {})
177
177
  if self.include?(item)
178
178
  index = self.index(item)
179
179
 
@@ -363,6 +363,29 @@ class ActsAsCartTest < ActiveSupport::TestCase
363
363
  end
364
364
  end
365
365
 
366
+ context 'option passing' do
367
+ should 'pass options from add_to_cart to find_cart_item' do
368
+ item = Item.make
369
+ @cart.expects(:find_cart_item).with(item, { :options => 'value' })
370
+ @cart.add_to_cart(item, 1, { :options => 'value' })
371
+ end
372
+
373
+ should 'pass options from remove_from_cart to find_cart_item' do
374
+ item = Item.make
375
+ @cart.add_to_cart(item, 1)
376
+ @cart.expects(:find_cart_item).with(item, { :options => 'value' })
377
+ @cart.remove_from_cart(item, 1, { :options => 'value' })
378
+ end
379
+
380
+ should 'pass options from update_cart to find_cart_item' do
381
+ item = Item.make
382
+ @cart.add_to_cart(item, 1)
383
+ @cart.expects(:find_cart_item).with(item, { :options => 'value' }).times(2)
384
+ @cart.update_cart(item, 3, { :options => 'value' })
385
+ end
386
+
387
+ end
388
+
366
389
  context 'add to cart' do
367
390
  should 'add an item to the cart if the cart is empty' do
368
391
  assert @cart.empty?
@@ -459,8 +482,6 @@ class ActsAsCartTest < ActiveSupport::TestCase
459
482
  end
460
483
  end
461
484
 
462
-
463
-
464
485
  context 'remove_from_cart' do
465
486
  should 'remove the quantity supplied from the cart' do
466
487
  item = Item.make
@@ -45,28 +45,28 @@ class CartTest < ActiveSupport::TestCase
45
45
  context 'items' do
46
46
  should 'fire the item before_add_to_cart callback on add to cart' do
47
47
  item = TestItem.new
48
- item.expects(:before_add_to_cart).with(1).returns(true)
48
+ item.expects(:before_add_to_cart).with(1, {}).returns(true)
49
49
  @cart.add_to_cart(item, 1)
50
50
  assert_equal 1, @cart.quantity
51
51
  end
52
52
 
53
53
  should 'halt and return false if before_add_to_cart returns false' do
54
54
  item = TestItem.new
55
- item.expects(:before_add_to_cart).with(1).returns(false)
56
- assert !@cart.add_to_cart(item, 1)
55
+ item.expects(:before_add_to_cart).with(1, { :option => 'value' }).returns(false)
56
+ assert !@cart.add_to_cart(item, 1, { :option => 'value' })
57
57
  assert_equal 0, @cart.quantity
58
58
  end
59
59
 
60
60
  should 'fire the item after_add_to_cart callback' do
61
61
  item = TestItem.new
62
- item.expects(:after_add_to_cart).with(1)
63
- @cart.add_to_cart(item, 1)
62
+ item.expects(:after_add_to_cart).with(1, { :option => 'value' })
63
+ @cart.add_to_cart(item, 1, { :option => 'value' })
64
64
  end
65
65
 
66
66
  should 'fire the item before_remove_from_cart callback on add to cart' do
67
67
  item = TestItem.new
68
- item.expects(:before_remove_from_cart).with(1).returns(true)
69
- @cart.remove_from_cart(item, 1)
68
+ item.expects(:before_remove_from_cart).with(1, { :option => 'value' }).returns(true)
69
+ @cart.remove_from_cart(item, 1, { :option => 'value' })
70
70
  assert_equal 0, @cart.quantity
71
71
  end
72
72
 
@@ -74,21 +74,21 @@ class CartTest < ActiveSupport::TestCase
74
74
  item = TestItem.new
75
75
  @cart.add_to_cart(item, 1)
76
76
  assert_equal 1, @cart.quantity
77
- item.expects(:before_remove_from_cart).with(1).returns(false)
77
+ item.expects(:before_remove_from_cart).with(1, {}).returns(false)
78
78
  assert !@cart.remove_from_cart(item, 1)
79
79
  assert_equal 1, @cart.quantity
80
80
  end
81
81
 
82
82
  should 'fire the item after_remove_from_cart callback' do
83
83
  item = TestItem.new
84
- item.expects(:after_remove_from_cart).with(1)
85
- @cart.remove_from_cart(item, 1)
84
+ item.expects(:after_remove_from_cart).with(1, { :option => 'value' })
85
+ @cart.remove_from_cart(item, 1, { :option => 'value' })
86
86
  end
87
87
 
88
88
  should 'fire the item before_add_to_cart callback if update_cart adds items to the cart' do
89
89
  item = TestItem.new
90
90
  @cart.add_to_cart(item, 10)
91
- item.expects(:before_add_to_cart).with(10).returns(true)
91
+ item.expects(:before_add_to_cart).with(10, {}).returns(true)
92
92
  @cart.update_cart(item, 20)
93
93
  assert 20, @cart.quantity
94
94
  end
@@ -96,15 +96,15 @@ class CartTest < ActiveSupport::TestCase
96
96
  should 'halt and return false if before_add_to_cart callback returns fale when update_cart adds items to the cart' do
97
97
  item = TestItem.new
98
98
  @cart.add_to_cart(item, 10)
99
- item.expects(:before_add_to_cart).with(10).returns(false)
100
- assert !@cart.update_cart(item, 20)
99
+ item.expects(:before_add_to_cart).with(10, { :option => 'value' }).returns(false)
100
+ assert !@cart.update_cart(item, 20, { :option => 'value' })
101
101
  assert 10, @cart.quantity
102
102
  end
103
103
 
104
104
  should 'fire the item after_add_to_cart callback if update_cart adds items to the cart' do
105
105
  item = TestItem.new
106
106
  @cart.add_to_cart(item, 10)
107
- item.expects(:after_add_to_cart).with(10).returns(true)
107
+ item.expects(:after_add_to_cart).with(10, {}).returns(true)
108
108
  @cart.update_cart(item, 20)
109
109
  assert 20, @cart.quantity
110
110
  end
@@ -112,7 +112,7 @@ class CartTest < ActiveSupport::TestCase
112
112
  should 'fire the item after_remove_from_cart callback if update_cart removes items from the cart' do
113
113
  item = TestItem.new
114
114
  @cart.add_to_cart(item, 10)
115
- item.expects(:before_remove_from_cart).with(5).returns(true)
115
+ item.expects(:before_remove_from_cart).with(5, {}).returns(true)
116
116
  @cart.update_cart(item, 5)
117
117
  assert 5, @cart.quantity
118
118
  end
@@ -120,8 +120,8 @@ class CartTest < ActiveSupport::TestCase
120
120
  should 'halt and return false if after_remove_from_cart callback returns false when update_cart removes items from the cart' do
121
121
  item = TestItem.new
122
122
  @cart.add_to_cart(item, 10)
123
- item.expects(:before_remove_from_cart).with(5).returns(false)
124
- assert !@cart.update_cart(item, 5)
123
+ item.expects(:before_remove_from_cart).with(5, { :option => 'value' }).returns(false)
124
+ assert !@cart.update_cart(item, 5, { :option => 'value' })
125
125
  assert 10, @cart.quantity
126
126
  end
127
127
 
@@ -129,7 +129,7 @@ class CartTest < ActiveSupport::TestCase
129
129
  should 'fire the item after_remove_from_cart callback if update_cart adds items to the cart' do
130
130
  item = TestItem.new
131
131
  @cart.add_to_cart(item, 10)
132
- item.expects(:after_remove_from_cart).with(5).returns(true)
132
+ item.expects(:after_remove_from_cart).with(5, {}).returns(true)
133
133
  @cart.update_cart(item, 5)
134
134
  assert 10, @cart.quantity
135
135
  end
@@ -138,27 +138,27 @@ class CartTest < ActiveSupport::TestCase
138
138
  context 'storage engines' do
139
139
  should 'fire the storage engines before_add_to_cart callback on add to cart' do
140
140
  item = TestItem.new
141
- @cart_storage_engine.expects(:before_add_to_cart).with(item, 1).returns(true)
142
- @cart.add_to_cart(item, 1)
141
+ @cart_storage_engine.expects(:before_add_to_cart).with(1, { :option => 'value' }).returns(true)
142
+ @cart.add_to_cart(item, 1, { :option => 'value' })
143
143
  assert_equal 1, @cart.quantity
144
144
  end
145
145
 
146
146
  should 'halt and return false if before_add_to_cart returns false' do
147
147
  item = TestItem.new
148
- @cart_storage_engine.expects(:before_add_to_cart).with(item, 1).returns(false)
149
- assert !@cart.add_to_cart(item, 1)
148
+ @cart_storage_engine.expects(:before_add_to_cart).with(1, { :option => 'value' }).returns(false)
149
+ assert !@cart.add_to_cart(item, 1, { :option => 'value' })
150
150
  assert_equal 0, @cart.quantity
151
151
  end
152
152
 
153
153
  should 'fire the storage engines after_add_to_cart callback' do
154
154
  item = TestItem.new
155
- @cart_storage_engine.expects(:after_add_to_cart).with(item, 1)
155
+ @cart_storage_engine.expects(:after_add_to_cart).with(1, {})
156
156
  @cart.add_to_cart(item, 1)
157
157
  end
158
158
 
159
159
  should 'fire the storage engines before_remove_from_cart callback on add to cart' do
160
160
  item = TestItem.new
161
- @cart_storage_engine.expects(:before_remove_from_cart).with(item, 1).returns(true)
161
+ @cart_storage_engine.expects(:before_remove_from_cart).with(1, {}).returns(true)
162
162
  @cart.remove_from_cart(item, 1)
163
163
  assert_equal 0, @cart.quantity
164
164
  end
@@ -167,15 +167,15 @@ class CartTest < ActiveSupport::TestCase
167
167
  item = TestItem.new
168
168
  @cart.add_to_cart(item, 1)
169
169
  assert_equal 1, @cart.quantity
170
- @cart_storage_engine.expects(:before_remove_from_cart).with(item, 1).returns(false)
171
- assert !@cart.remove_from_cart(item, 1)
170
+ @cart_storage_engine.expects(:before_remove_from_cart).with(1, { :option => 'value' }).returns(false)
171
+ assert !@cart.remove_from_cart(item, 1, { :option => 'value' })
172
172
  assert_equal 1, @cart.quantity
173
173
  end
174
174
 
175
175
  should 'fire the storage engines after_remove_from_cart callback' do
176
176
  item = TestItem.new
177
- @cart_storage_engine.expects(:after_remove_from_cart).with(item, 1)
178
- @cart.remove_from_cart(item, 1)
177
+ @cart_storage_engine.expects(:after_remove_from_cart).with(1, { :option => 'value' })
178
+ @cart.remove_from_cart(item, 1, { :option => 'value' })
179
179
  end
180
180
  end
181
181
  end
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.11
4
+ version: 0.0.12
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-05-07 00:00:00 +08:00
12
+ date: 2010-05-19 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency