active_cart 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/active_cart/acts_as_cart.rb +2 -0
- data/lib/active_cart/cart.rb +24 -0
- data/lib/active_cart/cart_storage.rb +8 -1
- data/test/unit/acts_as_cart_test.rb +12 -0
- data/test/unit/cart_storage_test.rb +11 -0
- data/test/unit/cart_test.rb +82 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.10
|
@@ -83,6 +83,8 @@ module ActiveCart
|
|
83
83
|
def remove_from_cart(item, quantity = 1)
|
84
84
|
cart_item = find_cart_item(item)
|
85
85
|
if cart_item
|
86
|
+
quantity = cart_item.quantity if quantity == :all #TEST!!!
|
87
|
+
|
86
88
|
if cart_item.quantity - quantity > 0
|
87
89
|
cart_item.quantity = cart_item.quantity - quantity
|
88
90
|
cart_item.save!
|
data/lib/active_cart/cart.rb
CHANGED
@@ -109,6 +109,30 @@ module ActiveCart
|
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
112
|
+
# Sets the quantity of an item to the supplied value
|
113
|
+
#
|
114
|
+
# @cart.add_to_cart(item, 5)
|
115
|
+
# @cart[0].quantity # => 5
|
116
|
+
# @cart.update_cart(item, 15)
|
117
|
+
# @cart[0].quantity # => 15
|
118
|
+
# @cart.update_cart(item, 2)
|
119
|
+
# @cart[0].quantity # => 2
|
120
|
+
#
|
121
|
+
def update_cart(item, quantity = 1)
|
122
|
+
if @storage_engine.include?(item)
|
123
|
+
index = @storage_engine.index(item)
|
124
|
+
diff = quantity - @storage_engine.at(index).quantity
|
125
|
+
|
126
|
+
if diff < 0
|
127
|
+
return remove_from_cart(item, -1 * diff)
|
128
|
+
else
|
129
|
+
return add_to_cart(item, diff)
|
130
|
+
end
|
131
|
+
else
|
132
|
+
return add_to_cart(item, quantity)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
112
136
|
# 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
|
113
137
|
#
|
114
138
|
# @cart.add_to_cart(item, 5)
|
@@ -168,10 +168,17 @@ module ActiveCart
|
|
168
168
|
# @cart[0].quantity # => 2
|
169
169
|
# @cart.remove_from_cart(item, 2)
|
170
170
|
# @cart[0] # => nil
|
171
|
-
#
|
171
|
+
#
|
172
|
+
# @cart.add_to_cart(item, 3)
|
173
|
+
# @cart[0].quantity # => 3
|
174
|
+
# @cart_remove_from_cart(item, :all)
|
175
|
+
# @cart[[0].quantity # => 0
|
172
176
|
def remove_from_cart(item, quantity = 1)
|
173
177
|
if self.include?(item)
|
174
178
|
index = self.index(item)
|
179
|
+
|
180
|
+
quantity = self.at(index).quantity if quantity == :all
|
181
|
+
|
175
182
|
if (existing = self.at(index)).quantity - quantity > 0
|
176
183
|
existing.quantity = existing.quantity - quantity
|
177
184
|
else
|
@@ -459,6 +459,18 @@ class ActsAsCartTest < ActiveSupport::TestCase
|
|
459
459
|
assert_not_nil Item.find(item.id)
|
460
460
|
end
|
461
461
|
|
462
|
+
should 'remove all the items from the cart if the quantity is set to :all' do
|
463
|
+
item = Item.make
|
464
|
+
@cart.add_to_cart(item, 10)
|
465
|
+
assert_equal 1, @cart.size
|
466
|
+
assert_equal 10, @cart.quantity
|
467
|
+
|
468
|
+
@cart.remove_from_cart(item, :all)
|
469
|
+
assert_equal 0, @cart.size
|
470
|
+
|
471
|
+
assert_not_nil Item.find(item.id)
|
472
|
+
end
|
473
|
+
|
462
474
|
should "simply return if the item doesn't exist in the cart" do
|
463
475
|
item = Item.make
|
464
476
|
@cart.add_to_cart(item, 10)
|
@@ -297,6 +297,17 @@ class CartStorageTest < ActiveSupport::TestCase
|
|
297
297
|
assert_equal 1, @cart.size
|
298
298
|
assert_equal 10, @cart.quantity
|
299
299
|
end
|
300
|
+
|
301
|
+
should "remove all items if the quantity is set to :all" do
|
302
|
+
item = TestItem.new(1)
|
303
|
+
@cart.add_to_cart(item, 10)
|
304
|
+
assert_equal 1, @cart.size
|
305
|
+
assert_equal 10, @cart.quantity
|
306
|
+
|
307
|
+
@cart.remove_from_cart(item, :all)
|
308
|
+
assert_equal 0, @cart.size
|
309
|
+
assert_equal 0, @cart.quantity
|
310
|
+
end
|
300
311
|
end
|
301
312
|
end
|
302
313
|
end
|
data/test/unit/cart_test.rb
CHANGED
@@ -8,6 +8,39 @@ class CartTest < ActiveSupport::TestCase
|
|
8
8
|
@cart = ActiveCart::Cart.new(@cart_storage_engine)
|
9
9
|
end
|
10
10
|
|
11
|
+
context 'update_cart' do
|
12
|
+
should 'update the number of items in the cart if the item is in the cart' do
|
13
|
+
item = TestItem.new(1)
|
14
|
+
@cart.add_to_cart(item, 10)
|
15
|
+
assert_equal 1, @cart.size
|
16
|
+
assert_equal 10, @cart.quantity
|
17
|
+
|
18
|
+
@cart.update_cart(item, 20)
|
19
|
+
assert_equal 1, @cart.size
|
20
|
+
assert_equal 20, @cart.quantity
|
21
|
+
end
|
22
|
+
|
23
|
+
should 'set the given quantity of items in to the cart if the item is not yet in the cart' do
|
24
|
+
item = TestItem.new(1)
|
25
|
+
|
26
|
+
@cart.update_cart(item, 20)
|
27
|
+
assert_equal 1, @cart.size
|
28
|
+
assert_equal 20, @cart.quantity
|
29
|
+
end
|
30
|
+
|
31
|
+
should 'set the given quantity of item in to the cart if the requested value is lower than the current quantity' do
|
32
|
+
item = TestItem.new(1)
|
33
|
+
|
34
|
+
@cart.add_to_cart(item, 10)
|
35
|
+
assert_equal 1, @cart.size
|
36
|
+
assert_equal 10, @cart.quantity
|
37
|
+
|
38
|
+
@cart.update_cart(item, 4)
|
39
|
+
assert_equal 1, @cart.size
|
40
|
+
assert_equal 4, @cart.quantity
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
11
44
|
context 'callbacks' do
|
12
45
|
context 'items' do
|
13
46
|
should 'fire the item before_add_to_cart callback on add to cart' do
|
@@ -51,6 +84,55 @@ class CartTest < ActiveSupport::TestCase
|
|
51
84
|
item.expects(:after_remove_from_cart).with(1)
|
52
85
|
@cart.remove_from_cart(item, 1)
|
53
86
|
end
|
87
|
+
|
88
|
+
should 'fire the item before_add_to_cart callback if update_cart adds items to the cart' do
|
89
|
+
item = TestItem.new
|
90
|
+
@cart.add_to_cart(item, 10)
|
91
|
+
item.expects(:before_add_to_cart).with(10).returns(true)
|
92
|
+
@cart.update_cart(item, 20)
|
93
|
+
assert 20, @cart.quantity
|
94
|
+
end
|
95
|
+
|
96
|
+
should 'halt and return false if before_add_to_cart callback returns fale when update_cart adds items to the cart' do
|
97
|
+
item = TestItem.new
|
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)
|
101
|
+
assert 10, @cart.quantity
|
102
|
+
end
|
103
|
+
|
104
|
+
should 'fire the item after_add_to_cart callback if update_cart adds items to the cart' do
|
105
|
+
item = TestItem.new
|
106
|
+
@cart.add_to_cart(item, 10)
|
107
|
+
item.expects(:after_add_to_cart).with(10).returns(true)
|
108
|
+
@cart.update_cart(item, 20)
|
109
|
+
assert 20, @cart.quantity
|
110
|
+
end
|
111
|
+
|
112
|
+
should 'fire the item after_remove_from_cart callback if update_cart removes items from the cart' do
|
113
|
+
item = TestItem.new
|
114
|
+
@cart.add_to_cart(item, 10)
|
115
|
+
item.expects(:before_remove_from_cart).with(5).returns(true)
|
116
|
+
@cart.update_cart(item, 5)
|
117
|
+
assert 5, @cart.quantity
|
118
|
+
end
|
119
|
+
|
120
|
+
should 'halt and return false if after_remove_from_cart callback returns false when update_cart removes items from the cart' do
|
121
|
+
item = TestItem.new
|
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)
|
125
|
+
assert 10, @cart.quantity
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
should 'fire the item after_remove_from_cart callback if update_cart adds items to the cart' do
|
130
|
+
item = TestItem.new
|
131
|
+
@cart.add_to_cart(item, 10)
|
132
|
+
item.expects(:after_remove_from_cart).with(5).returns(true)
|
133
|
+
@cart.update_cart(item, 5)
|
134
|
+
assert 10, @cart.quantity
|
135
|
+
end
|
54
136
|
end
|
55
137
|
|
56
138
|
context 'storage engines' do
|
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.
|
4
|
+
version: 0.0.10
|
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-
|
12
|
+
date: 2010-05-07 00:00:00 +08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|