acts_as_shopping_cart 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.markdown +50 -4
- data/acts_as_shopping_cart.gemspec +2 -0
- data/features/shopping_cart.feature +46 -0
- data/features/step_definitions/product_steps.rb +3 -0
- data/features/step_definitions/shopping_cart_steps.rb +69 -0
- data/features/support/env.rb +8 -0
- data/features/support/rails_env.rb +24 -0
- data/lib/active_record/acts/shopping_cart.rb +4 -3
- data/lib/active_record/acts/shopping_cart/cart/instance_methods.rb +70 -0
- data/lib/active_record/acts/shopping_cart/item/instance_methods.rb +58 -0
- data/lib/active_record/acts/shopping_cart_item.rb +2 -2
- data/lib/active_record/acts/shopping_cart_item/{cart_item_instance_methods.rb → instance_methods.rb} +4 -4
- data/lib/acts_as_shopping_cart.rb +28 -6
- data/lib/acts_as_shopping_cart/schema.rb +17 -0
- data/lib/acts_as_shopping_cart/version.rb +1 -1
- data/spec/active_record/acts/shopping_cart/cart/instance_methods_spec.rb +155 -0
- data/spec/active_record/acts/shopping_cart/item/instance_methods_spec.rb +134 -0
- data/spec/active_record/acts/shopping_cart_item/instance_methods_spec.rb +38 -0
- data/spec/spec_helper.rb +2 -51
- metadata +52 -21
- data/lib/active_record/acts/shopping_cart/cart_instance_methods.rb +0 -67
- data/lib/active_record/acts/shopping_cart/item_instance_methods.rb +0 -64
- data/spec/active_record/acts/shopping_cart/cart_instance_methods_spec.rb +0 -220
- data/spec/active_record/acts/shopping_cart/cart_item_instance_methods_spec.rb +0 -46
- data/spec/active_record/acts/shopping_cart/item_instance_methods_spec.rb +0 -125
- data/spec/acts_as_shopping_cart_spec.rb +0 -16
@@ -1,67 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Acts
|
3
|
-
module ShoppingCart
|
4
|
-
module InstanceMethods
|
5
|
-
|
6
|
-
#
|
7
|
-
# Returns the subtotal by summing the price times quantity for all the items in the cart
|
8
|
-
#
|
9
|
-
def subtotal
|
10
|
-
("%.2f" % cart_items.sum("price * quantity")).to_f
|
11
|
-
end
|
12
|
-
|
13
|
-
#
|
14
|
-
# Returns the total by summing the subtotal, taxes and shipping_cost
|
15
|
-
#
|
16
|
-
def total
|
17
|
-
("%.2f" % (subtotal + self.taxes + shipping_cost)).to_f
|
18
|
-
end
|
19
|
-
|
20
|
-
#
|
21
|
-
# Adds a product to the cart
|
22
|
-
#
|
23
|
-
def add(object, price, quantity = 1)
|
24
|
-
cart_item = item_for(object)
|
25
|
-
|
26
|
-
unless cart_item
|
27
|
-
cart_items.create(:item => object, :price => price, :quantity => quantity)
|
28
|
-
else
|
29
|
-
cart_item.quantity = (cart_item.quantity + quantity)
|
30
|
-
cart_item.save
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
#
|
35
|
-
# Delete an item from the cart
|
36
|
-
#
|
37
|
-
def delete(object)
|
38
|
-
cart_item = item_for(object)
|
39
|
-
cart_items.delete(cart_item)
|
40
|
-
end
|
41
|
-
|
42
|
-
#
|
43
|
-
# Remove an item from the cart
|
44
|
-
#
|
45
|
-
def remove(object, quantity = 1)
|
46
|
-
cart_item = item_for(object)
|
47
|
-
if cart_item
|
48
|
-
if cart_item.quantity <= quantity
|
49
|
-
cart_items.delete(cart_item)
|
50
|
-
else
|
51
|
-
cart_item.quantity = (cart_item.quantity - quantity)
|
52
|
-
cart_item.save
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
#
|
58
|
-
# Return the number of unique items in the cart
|
59
|
-
#
|
60
|
-
def total_unique_items
|
61
|
-
cart_items.sum(:quantity)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
@@ -1,64 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Acts
|
3
|
-
module ShoppingCart
|
4
|
-
module InstanceMethods
|
5
|
-
|
6
|
-
#
|
7
|
-
# Returns the cart item for the specified object
|
8
|
-
#
|
9
|
-
def item_for(object)
|
10
|
-
cart_items.where(:item_id => object.id).first
|
11
|
-
end
|
12
|
-
|
13
|
-
#
|
14
|
-
# Returns the subtotal of a specified item by multiplying the quantity times
|
15
|
-
# the price of the item.
|
16
|
-
#
|
17
|
-
def subtotal_for(object)
|
18
|
-
item = item_for(object)
|
19
|
-
if item
|
20
|
-
item.quantity * item.price
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
#
|
25
|
-
# Returns the quantity of the specified object
|
26
|
-
#
|
27
|
-
def quantity_for(object)
|
28
|
-
item = item_for(object)
|
29
|
-
item ? item.quantity : 0
|
30
|
-
end
|
31
|
-
|
32
|
-
#
|
33
|
-
# Updates the quantity of the specified object
|
34
|
-
#
|
35
|
-
def update_quantity_for(object, new_quantity)
|
36
|
-
item = item_for(object)
|
37
|
-
if item
|
38
|
-
item.quantity = new_quantity
|
39
|
-
item.save
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
#
|
44
|
-
# Returns the price of the specified object
|
45
|
-
#
|
46
|
-
def price_for(object)
|
47
|
-
item = item_for(object)
|
48
|
-
item ? item.price : 0
|
49
|
-
end
|
50
|
-
|
51
|
-
#
|
52
|
-
# Updates the price of the specified object
|
53
|
-
#
|
54
|
-
def update_price_for(object, new_price)
|
55
|
-
item = item_for(object)
|
56
|
-
if item
|
57
|
-
item.price = new_price
|
58
|
-
item.save
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
@@ -1,220 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '../../../../spec_helper')
|
2
|
-
|
3
|
-
describe "ShoppingCart" do
|
4
|
-
before(:each) do
|
5
|
-
@cart = SomeCart.create
|
6
|
-
end
|
7
|
-
|
8
|
-
describe :add do
|
9
|
-
it "adds an item" do
|
10
|
-
@some_object = SomeClass.create
|
11
|
-
@cart.add(@some_object, 100)
|
12
|
-
@cart.cart_items(true).first.should_not be_nil
|
13
|
-
@cart.cart_items(true).first.item.should == @some_object
|
14
|
-
end
|
15
|
-
|
16
|
-
context "add more of an item already in the cart" do
|
17
|
-
it "increases the quantity of the item" do
|
18
|
-
@some_object = SomeClass.create
|
19
|
-
@cart.add(@some_object, 100)
|
20
|
-
@cart.add(@some_object, 100, 2)
|
21
|
-
|
22
|
-
@cart.cart_items.first.quantity.should == 3
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe :subtotal do
|
28
|
-
it "has a subtotal" do
|
29
|
-
@cart.should respond_to(:subtotal)
|
30
|
-
end
|
31
|
-
|
32
|
-
context "the cart has items" do
|
33
|
-
before(:each) do
|
34
|
-
@cart.add(SomeClass.create, 199.99, 2)
|
35
|
-
@cart.add(SomeClass.create, 299.99)
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should return the sum of the item prices" do
|
39
|
-
@cart.subtotal.should == 699.97
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
context "the cart has no item" do
|
44
|
-
it "should return 0" do
|
45
|
-
@cart.subtotal == 0
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
describe :total do
|
51
|
-
it "has a total" do
|
52
|
-
@cart.should respond_to(:total)
|
53
|
-
end
|
54
|
-
|
55
|
-
context "the cart has items" do
|
56
|
-
before(:each) do
|
57
|
-
@cart.add(SomeClass.create, 199.99, 2)
|
58
|
-
@cart.add(SomeClass.create, 299.99)
|
59
|
-
end
|
60
|
-
|
61
|
-
context "the cart has taxes" do
|
62
|
-
before(:each) do
|
63
|
-
@cart.taxes = 12.99
|
64
|
-
end
|
65
|
-
|
66
|
-
context "the cart has shipping cost" do
|
67
|
-
before(:each) do
|
68
|
-
@cart.shipping_cost = 3.99
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should return the sum of the item prices, taxes and shipping cost" do
|
72
|
-
@cart.total.should == 716.95
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
context "the cart hasn't shipping cost" do
|
77
|
-
it "should return the sum of the item prices and taxes" do
|
78
|
-
@cart.total.should == 712.96
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
context "the cart hasn't taxes" do
|
84
|
-
context "the cart has shipping cost" do
|
85
|
-
before(:each) do
|
86
|
-
@cart.shipping_cost = 3.99
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should return the sum of item prices and shipping cost" do
|
90
|
-
@cart.total.should == 703.96
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
context "the cart hasn't shipping cost" do
|
95
|
-
it "should return the sum of the item prices" do
|
96
|
-
@cart.total.should == 699.97
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
context "the cart has no item" do
|
103
|
-
context "the cart has taxes" do
|
104
|
-
before(:each) do
|
105
|
-
@cart.taxes = 12.99
|
106
|
-
end
|
107
|
-
|
108
|
-
context "the cart has shipping cost" do
|
109
|
-
before(:each) do
|
110
|
-
@cart.shipping_cost = 3.99
|
111
|
-
end
|
112
|
-
|
113
|
-
it "should return the sum of the item prices, taxes and shipping cost" do
|
114
|
-
@cart.total.should == 16.98
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
context "the cart hasn't shipping cost" do
|
119
|
-
it "should return the sum of the item prices and taxes" do
|
120
|
-
@cart.total.should == 12.99
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
context "the cart hasn't taxes" do
|
126
|
-
context "the cart has shipping cost" do
|
127
|
-
before(:each) do
|
128
|
-
@cart.shipping_cost = 3.99
|
129
|
-
end
|
130
|
-
|
131
|
-
it "should return the sum of item prices and shipping cost" do
|
132
|
-
@cart.total.should == 3.99
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
context "the cart hasn't shipping cost" do
|
137
|
-
it "should return 0" do
|
138
|
-
@cart.total == 0
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
describe :delete do
|
146
|
-
context "the cart has items" do
|
147
|
-
before(:each) do
|
148
|
-
@some_object = SomeClass.create
|
149
|
-
@cart.add(@some_object, 199.99)
|
150
|
-
@cart.add(SomeClass.create, 299.99)
|
151
|
-
end
|
152
|
-
|
153
|
-
it "removes the item from the cart" do
|
154
|
-
@cart.remove(@some_object)
|
155
|
-
@cart.cart_items.count.should == 1
|
156
|
-
@cart.item_for(@some_object).should be_nil
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
describe :remove do
|
162
|
-
context "the cart has items" do
|
163
|
-
before(:each) do
|
164
|
-
@some_object = SomeClass.create
|
165
|
-
@cart.add(@some_object, 199.99)
|
166
|
-
@cart.add(SomeClass.create, 299.99)
|
167
|
-
end
|
168
|
-
|
169
|
-
it "removes the item from the cart" do
|
170
|
-
@cart.remove(@some_object)
|
171
|
-
@cart.cart_items.count.should == 1
|
172
|
-
@cart.item_for(@some_object).should be_nil
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
context "remove some items" do
|
177
|
-
before(:each) do
|
178
|
-
@some_object = SomeClass.create
|
179
|
-
@cart.add(@some_object, 199.99, 5)
|
180
|
-
@cart.add(SomeClass.create, 299.99)
|
181
|
-
end
|
182
|
-
|
183
|
-
it "removes 2 items of the specific product" do
|
184
|
-
@cart.remove(@some_object, 2)
|
185
|
-
@cart.item_for(@some_object).should_not be_nil
|
186
|
-
@cart.item_for(@some_object).quantity.should == 3
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
context "the object is not on the cart" do
|
191
|
-
before(:each) do
|
192
|
-
@some_object = SomeClass.create
|
193
|
-
end
|
194
|
-
it "does nothing" do
|
195
|
-
@cart.remove(@some_object)
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
describe :total_unique_items do
|
200
|
-
context "there are different items in the cart" do
|
201
|
-
before(:each) do
|
202
|
-
@cart.add(SomeClass.create, 100, 1)
|
203
|
-
@cart.add(SomeClass.create, 100, 2)
|
204
|
-
@cart.add(SomeClass.create, 100, 3)
|
205
|
-
end
|
206
|
-
|
207
|
-
it "returns the sum of all the item quantities" do
|
208
|
-
@cart.total_unique_items.should == 6
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
context "the cart has no items" do
|
213
|
-
it "returns 0" do
|
214
|
-
@cart.total_unique_items == 0
|
215
|
-
end
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
@@ -1,46 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '../../../../spec_helper')
|
2
|
-
|
3
|
-
describe "ShoppingCart" do
|
4
|
-
before(:each) do
|
5
|
-
@cart = SomeCart.create
|
6
|
-
@cart.add(SomeClass.create, 100, 3)
|
7
|
-
@cart.add(SomeClass.create, 200, 6)
|
8
|
-
@cart.add(SomeClass.create, 300, 9)
|
9
|
-
end
|
10
|
-
|
11
|
-
describe :subtotal do
|
12
|
-
it "returns the quantity times the price for the specicfied object" do
|
13
|
-
@cart.cart_items[0].subtotal.should == (100 * 3)
|
14
|
-
@cart.cart_items[1].subtotal.should == (200 * 6)
|
15
|
-
@cart.cart_items[2].subtotal.should == (300 * 9)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
describe :update_quantity do
|
20
|
-
before(:each) do
|
21
|
-
@cart.cart_items[0].update_quantity(6)
|
22
|
-
@cart.cart_items[1].update_quantity(9)
|
23
|
-
@cart.cart_items[2].update_quantity(12)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "returns the quantity of the specified object" do
|
27
|
-
@cart.cart_items[0].quantity.should == (6)
|
28
|
-
@cart.cart_items[1].quantity.should == (9)
|
29
|
-
@cart.cart_items[2].quantity.should == (12)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe :update_price do
|
34
|
-
before(:each) do
|
35
|
-
@cart.cart_items[0].update_price(50)
|
36
|
-
@cart.cart_items[1].update_price(100)
|
37
|
-
@cart.cart_items[2].update_price(150)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "returns the quantity of the specified object" do
|
41
|
-
@cart.cart_items[0].price.should == (50)
|
42
|
-
@cart.cart_items[1].price.should == (100)
|
43
|
-
@cart.cart_items[2].price.should == (150)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
@@ -1,125 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '../../../../spec_helper')
|
2
|
-
|
3
|
-
describe "ShoppingCart" do
|
4
|
-
before(:each) do
|
5
|
-
@cart = SomeCart.create
|
6
|
-
end
|
7
|
-
|
8
|
-
describe :item_for do
|
9
|
-
context "the item is in the cart" do
|
10
|
-
before(:each) do
|
11
|
-
@some_object = SomeClass.create
|
12
|
-
@cart.add(@some_object, 1)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "returns the item object" do
|
16
|
-
@cart.item_for(@some_object).item.should == @some_object
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context "the item is not on the cart" do
|
21
|
-
it "returns nil" do
|
22
|
-
product = SomeClass.create
|
23
|
-
@cart.item_for(product).should be_nil
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe :subtotal_for do
|
28
|
-
context "the item exists in the cart" do
|
29
|
-
before(:each) do
|
30
|
-
@some_object = SomeClass.create
|
31
|
-
@cart.add(@some_object, 300, 5)
|
32
|
-
@cart.add(SomeClass.create, 100, 3)
|
33
|
-
end
|
34
|
-
|
35
|
-
it "returns the quantity times the price for the specicfied object" do
|
36
|
-
@cart.subtotal_for(@some_object).should == (300 * 5)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context "the item doesn't exist on the cart" do
|
41
|
-
it "returns nil" do
|
42
|
-
@cart.subtotal_for(SomeClass.create).should be_nil
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
describe :quantity_for do
|
48
|
-
context "the item is in the cart" do
|
49
|
-
before(:each) do
|
50
|
-
@some_object = SomeClass.create
|
51
|
-
@cart.add(@some_object, 100, 5)
|
52
|
-
end
|
53
|
-
|
54
|
-
it "returns the quantity of the specified object" do
|
55
|
-
@cart.quantity_for(@some_object).should == 5
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
context "the item is not on the cart" do
|
60
|
-
it "returns 0" do
|
61
|
-
@cart.quantity_for(SomeClass.create).should == 0
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
describe :update_quantity_for do
|
67
|
-
context "the item is in the cart" do
|
68
|
-
before(:each) do
|
69
|
-
@some_object = SomeClass.create
|
70
|
-
@cart.add(@some_object, 100, 5)
|
71
|
-
end
|
72
|
-
|
73
|
-
it "returns the quantity of the specified object" do
|
74
|
-
@cart.update_quantity_for(@some_object, 7)
|
75
|
-
@cart.quantity_for(@some_object).should == 7
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
context "the item is not on the cart" do
|
80
|
-
it "deos nothing" do
|
81
|
-
@cart.update_quantity_for(SomeClass.create, 7)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
describe :price_for do
|
87
|
-
context "the item is in the cart" do
|
88
|
-
before(:each) do
|
89
|
-
@some_object = SomeClass.create
|
90
|
-
@cart.add(@some_object, 99.99, 5)
|
91
|
-
end
|
92
|
-
|
93
|
-
it "returns the price of the specified object" do
|
94
|
-
@cart.price_for(@some_object).should == 99.99
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
context "the item is not on the cart" do
|
99
|
-
it "returns 0" do
|
100
|
-
@cart.price_for(SomeClass.create).should == 0
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
describe :update_price_for do
|
106
|
-
context "the item is in the cart" do
|
107
|
-
before(:each) do
|
108
|
-
@some_object = SomeClass.create
|
109
|
-
@cart.add(@some_object, 100, 5)
|
110
|
-
end
|
111
|
-
|
112
|
-
it "returns the quantity of the specified object" do
|
113
|
-
@cart.update_price_for(@some_object, 39.99)
|
114
|
-
@cart.price_for(@some_object).should == 39.99
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
context "the item is not on the cart" do
|
119
|
-
it "deos nothing" do
|
120
|
-
@cart.update_price_for(SomeClass.create, 39.99)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|