acts_as_shopping_cart 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +12 -8
- data/Rakefile +13 -0
- data/acts_as_shopping_cart.gemspec +1 -0
- data/features/step_definitions/shopping_cart_steps.rb +1 -1
- data/features/support/env.rb +3 -0
- data/lib/active_record/acts/shopping_cart.rb +1 -1
- data/lib/active_record/acts/shopping_cart/cart/instance_methods.rb +8 -3
- data/lib/active_record/acts/shopping_cart/item/instance_methods.rb +1 -1
- data/lib/acts_as_shopping_cart/version.rb +1 -1
- data/spec/active_record/acts/shopping_cart/cart/instance_methods_spec.rb +5 -5
- data/spec/active_record/acts/shopping_cart/item/instance_methods_spec.rb +4 -4
- metadata +25 -14
data/README.markdown
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# acts_as_shopping_cart
|
2
2
|
|
3
|
-
A simple shopping cart implementation.
|
3
|
+
A simple shopping cart implementation.
|
4
4
|
|
5
5
|
## Install
|
6
6
|
|
@@ -51,14 +51,14 @@ then just use the shortcuts:
|
|
51
51
|
In order for this to work, the Shopping Cart Item model should have the following fields:
|
52
52
|
|
53
53
|
create_table :cart_items do |t|
|
54
|
-
|
55
|
-
t.integer :owner_type # Holds the type of the owner, for polymorphism
|
56
|
-
t.integer :quantity # Holds the quantity of the object
|
57
|
-
t.integer :item_id # Holds the object id
|
58
|
-
t.string :item_type # Holds the type of the object, for polymorphism
|
59
|
-
t.float :price # Holds the price of the item
|
54
|
+
t.shopping_cart_item_fields # Creates the cart items fields
|
60
55
|
end
|
61
56
|
|
57
|
+
### Shopping Cart Items
|
58
|
+
|
59
|
+
Your ShoppingCart class will have a _shopping_cart_items_ association
|
60
|
+
that returns all the ShoppingCartItem objects in your cart.
|
61
|
+
|
62
62
|
### Add Items
|
63
63
|
|
64
64
|
To add an item to the cart you use the add method. You have to send the object and the price of the object as parameters.
|
@@ -162,7 +162,11 @@ Run rspec
|
|
162
162
|
|
163
163
|
Run cucumber features
|
164
164
|
|
165
|
-
|
165
|
+
cucumber
|
166
|
+
|
167
|
+
Both:
|
168
|
+
|
169
|
+
rake
|
166
170
|
|
167
171
|
# About the Author
|
168
172
|
|
data/Rakefile
CHANGED
@@ -1 +1,14 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'cucumber/rake/task'
|
4
|
+
Cucumber::Rake::Task.new do |t|
|
5
|
+
t.cucumber_opts = %w{--format progress}
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
RSpec::Core::RakeTask.new do |t|
|
10
|
+
t.skip_bundler = true
|
11
|
+
t.fail_on_error = true
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => [:spec, :cucumber]
|
data/features/support/env.rb
CHANGED
@@ -19,7 +19,7 @@ module ActiveRecord
|
|
19
19
|
def acts_as_shopping_cart_using(item_class)
|
20
20
|
self.send :include, ActiveRecord::Acts::ShoppingCart::Cart::InstanceMethods
|
21
21
|
self.send :include, ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods
|
22
|
-
has_many :
|
22
|
+
has_many :shopping_cart_items, :class_name => item_class.to_s.classify, :as => :owner
|
23
23
|
end
|
24
24
|
|
25
25
|
#
|
@@ -10,7 +10,7 @@ module ActiveRecord
|
|
10
10
|
cart_item = item_for(object)
|
11
11
|
|
12
12
|
unless cart_item
|
13
|
-
|
13
|
+
shopping_cart_items.create(:item => object, :price => price, :quantity => quantity)
|
14
14
|
else
|
15
15
|
cart_item.quantity = (cart_item.quantity + quantity)
|
16
16
|
cart_item.save
|
@@ -35,7 +35,7 @@ module ActiveRecord
|
|
35
35
|
# Returns the subtotal by summing the price times quantity for all the items in the cart
|
36
36
|
#
|
37
37
|
def subtotal
|
38
|
-
("%.2f" %
|
38
|
+
("%.2f" % shopping_cart_items.inject(0) { |sum, item| sum += (item.price * item.quantity) }).to_f
|
39
39
|
end
|
40
40
|
|
41
41
|
def shipping_cost
|
@@ -61,7 +61,12 @@ module ActiveRecord
|
|
61
61
|
# Return the number of unique items in the cart
|
62
62
|
#
|
63
63
|
def total_unique_items
|
64
|
-
|
64
|
+
shopping_cart_items.inject(0) { |sum, item| sum += item.quantity }
|
65
|
+
end
|
66
|
+
|
67
|
+
def cart_items
|
68
|
+
warn "ShoppingCart#cart_items WILL BE DEPRECATED IN LATER VERSIONS OF acts_as_shopping_cart, please use ShoppingCart#shopping_cart_items instead"
|
69
|
+
self.shopping_cart_items
|
65
70
|
end
|
66
71
|
end
|
67
72
|
end
|
@@ -9,7 +9,7 @@ describe ActiveRecord::Acts::ShoppingCart::Cart::InstanceMethods do
|
|
9
9
|
|
10
10
|
let(:subject) do
|
11
11
|
subject = klass.new
|
12
|
-
subject.stub(:
|
12
|
+
subject.stub(:shopping_cart_items).and_return([])
|
13
13
|
subject
|
14
14
|
end
|
15
15
|
|
@@ -26,7 +26,7 @@ describe ActiveRecord::Acts::ShoppingCart::Cart::InstanceMethods do
|
|
26
26
|
end
|
27
27
|
|
28
28
|
it "creates a new shopping cart item" do
|
29
|
-
subject.
|
29
|
+
subject.shopping_cart_items.should_receive(:create).with(:item => object, :price => 19.99, :quantity => 3)
|
30
30
|
subject.add(object, 19.99, 3)
|
31
31
|
end
|
32
32
|
end
|
@@ -78,7 +78,7 @@ describe ActiveRecord::Acts::ShoppingCart::Cart::InstanceMethods do
|
|
78
78
|
describe :subtotal do
|
79
79
|
context "cart has no items" do
|
80
80
|
before do
|
81
|
-
subject.stub(:
|
81
|
+
subject.stub(:shopping_cart_items).and_return([])
|
82
82
|
end
|
83
83
|
|
84
84
|
it "returns 0" do
|
@@ -89,7 +89,7 @@ describe ActiveRecord::Acts::ShoppingCart::Cart::InstanceMethods do
|
|
89
89
|
context "cart has items" do
|
90
90
|
before do
|
91
91
|
items = [stub(:quantity => 2, :price => 33.99), stub(:quantity => 1, :price => 45.99)]
|
92
|
-
subject.stub(:
|
92
|
+
subject.stub(:shopping_cart_items).and_return(items)
|
93
93
|
end
|
94
94
|
|
95
95
|
it "returns the sum of the price * quantity for all items" do
|
@@ -144,7 +144,7 @@ describe ActiveRecord::Acts::ShoppingCart::Cart::InstanceMethods do
|
|
144
144
|
context "cart has some items" do
|
145
145
|
before do
|
146
146
|
items = [stub(:quantity => 2, :price => 33.99), stub(:quantity => 1, :price => 45.99)]
|
147
|
-
subject.stub(:
|
147
|
+
subject.stub(:shopping_cart_items).and_return(items)
|
148
148
|
end
|
149
149
|
|
150
150
|
it "returns the sum of the quantities of all shopping cart items" do
|
@@ -7,11 +7,11 @@ describe ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods do
|
|
7
7
|
klass
|
8
8
|
end
|
9
9
|
|
10
|
-
let(:
|
10
|
+
let(:shopping_cart_items) { stub }
|
11
11
|
|
12
12
|
let(:subject) do
|
13
13
|
subject = klass.new
|
14
|
-
subject.stub(:
|
14
|
+
subject.stub(:shopping_cart_items).and_return(shopping_cart_items)
|
15
15
|
subject
|
16
16
|
end
|
17
17
|
|
@@ -21,7 +21,7 @@ describe ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods do
|
|
21
21
|
describe :item_for do
|
22
22
|
context "no cart item exists for the object" do
|
23
23
|
before do
|
24
|
-
|
24
|
+
shopping_cart_items.should_receive(:where).with(:item_id => object.id).and_return([])
|
25
25
|
end
|
26
26
|
|
27
27
|
it "returns the shopping cart item object for the requested object" do
|
@@ -31,7 +31,7 @@ describe ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods do
|
|
31
31
|
|
32
32
|
context "a cart item exists for the object" do
|
33
33
|
before do
|
34
|
-
|
34
|
+
shopping_cart_items.should_receive(:where).with(:item_id => object.id).and_return([ item ])
|
35
35
|
end
|
36
36
|
|
37
37
|
it "returns that item" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_shopping_cart
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70301979448540 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70301979448540
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: cucumber
|
27
|
-
requirement: &
|
27
|
+
requirement: &70301979448100 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70301979448100
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: database_cleaner
|
38
|
-
requirement: &
|
38
|
+
requirement: &70301979447640 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70301979447640
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &70301979447200 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70301979447200
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: sqlite3
|
60
|
-
requirement: &
|
60
|
+
requirement: &70301979446740 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70301979446740
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: simplecov
|
71
|
-
requirement: &
|
71
|
+
requirement: &70301979446280 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,7 +76,18 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70301979446280
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: rake
|
82
|
+
requirement: &70301979445840 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70301979445840
|
80
91
|
description: Simple Shopping Cart implementation
|
81
92
|
email:
|
82
93
|
- david@crowdint.com
|