acts_as_shopping_cart 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.markdown +125 -0
- data/Rakefile +1 -0
- data/VERSION +1 -0
- data/acts_as_shopping_cart.gemspec +25 -0
- data/lib/active_record/acts/shopping_cart/cart_instance_methods.rb +67 -0
- data/lib/active_record/acts/shopping_cart/item_instance_methods.rb +64 -0
- data/lib/active_record/acts/shopping_cart.rb +35 -0
- data/lib/active_record/acts/shopping_cart_item/cart_item_instance_methods.rb +30 -0
- data/lib/active_record/acts/shopping_cart_item.rb +36 -0
- data/lib/acts_as_shopping_cart/version.rb +3 -0
- data/lib/acts_as_shopping_cart.rb +10 -0
- data/spec/active_record/acts/shopping_cart/cart_instance_methods_spec.rb +220 -0
- data/spec/active_record/acts/shopping_cart/cart_item_instance_methods_spec.rb +46 -0
- data/spec/active_record/acts/shopping_cart/item_instance_methods_spec.rb +125 -0
- data/spec/acts_as_shopping_cart_spec.rb +16 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +65 -0
- metadata +116 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 David Padilla
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# acts_as_shopping_cart
|
2
|
+
|
3
|
+
A simple shopping cart implementation. Still on Alpha, use it at your own risk.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
### Rails 3
|
8
|
+
|
9
|
+
Include it in your Gemfile
|
10
|
+
|
11
|
+
gem 'acts_as_shopping_cart', :git => "git@github.com:crowdint/acts_as_shopping_cart.git"
|
12
|
+
|
13
|
+
And run bundler
|
14
|
+
|
15
|
+
bundle install
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
You need two models, one to hold the Shopping Carts and another to hold the Items
|
20
|
+
|
21
|
+
You can use any name for the models, you just have to let each model know about each other.
|
22
|
+
|
23
|
+
### Examples
|
24
|
+
|
25
|
+
For the Shopping Cart:
|
26
|
+
|
27
|
+
class Cart < ActiveRecord::Base
|
28
|
+
acts_as_shopping_cart_using :cart_item
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
For the items:
|
33
|
+
|
34
|
+
class CartItem < ActiveRecord::Base
|
35
|
+
acts_as_shopping_cart_item_for :cart
|
36
|
+
end
|
37
|
+
|
38
|
+
or, if you want to use convention over configuration, make sure your models are called *ShoppingCart* and *ShoppingCartItems*,
|
39
|
+
then just use the shortcuts:
|
40
|
+
|
41
|
+
class ShoppingCart < ActiveRecord::Base
|
42
|
+
acts_as_shopping_cart
|
43
|
+
end
|
44
|
+
|
45
|
+
class ShoppingCartItem < ActiveRecord::Base
|
46
|
+
acts_as_shopping_cart_item
|
47
|
+
end
|
48
|
+
|
49
|
+
### Migrations
|
50
|
+
|
51
|
+
In order for this to work, the Shopping Cart Item model should have the following fields:
|
52
|
+
|
53
|
+
create_table :cart_items do |t|
|
54
|
+
t.integer :owner_id # Holds the owner id, for polymorphism
|
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
|
60
|
+
end
|
61
|
+
|
62
|
+
### Add Items
|
63
|
+
|
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.
|
65
|
+
|
66
|
+
So, if you had a Product class, you would do something like this:
|
67
|
+
|
68
|
+
@cart = Cart.create
|
69
|
+
@product = Product.find(1)
|
70
|
+
|
71
|
+
@cart.add(@product, 99.99)
|
72
|
+
|
73
|
+
In the case where your product has a price field you could do something like:
|
74
|
+
|
75
|
+
@cart.add(@product, @product.price)
|
76
|
+
|
77
|
+
I tried to make it independent to the models in case you calculate discounts, sale prices or anything customized.
|
78
|
+
|
79
|
+
You can include a quantity parameter too.
|
80
|
+
|
81
|
+
@cart.add(@product, 99.99, 5)
|
82
|
+
|
83
|
+
In that case, you would add 5 of the same products to the shopping cart. If you don't specify the quantity 1 will be assumed.
|
84
|
+
|
85
|
+
### Remove Items
|
86
|
+
|
87
|
+
To remove an item from the cart you can use the remove method. You just have to send the object and the quantity you want to remove.
|
88
|
+
|
89
|
+
@cart.remove(@product, 1)
|
90
|
+
|
91
|
+
### Total
|
92
|
+
|
93
|
+
You can find out about the total using the _total_ method:
|
94
|
+
|
95
|
+
@cart.total # => 99.99
|
96
|
+
|
97
|
+
### Total unique items
|
98
|
+
|
99
|
+
You can find out how many unique items you have on your cart using the _total_unique_items_ method.
|
100
|
+
|
101
|
+
So, if you had something like:
|
102
|
+
|
103
|
+
@cart.add(@product, 99.99, 5)
|
104
|
+
|
105
|
+
Then,
|
106
|
+
|
107
|
+
@cart.total_unique_items # => 5
|
108
|
+
|
109
|
+
## Development
|
110
|
+
|
111
|
+
Install the dependencies
|
112
|
+
|
113
|
+
bundle install
|
114
|
+
|
115
|
+
### Test
|
116
|
+
|
117
|
+
Run rspec
|
118
|
+
|
119
|
+
rspec spec
|
120
|
+
|
121
|
+
## TODO
|
122
|
+
|
123
|
+
* Finish this document
|
124
|
+
* Test it on Rails 2
|
125
|
+
* Some more useful methods, like @cart.quantity_for(@product), @cart.price_for(@product)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_shopping_cart/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "acts_as_shopping_cart"
|
7
|
+
s.version = ActsAsShoppingCart::VERSION
|
8
|
+
s.authors = ["David Padilla"]
|
9
|
+
s.email = ["david@crowdint.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Simple Shopping Cart implementation}
|
12
|
+
s.description = %q{Simple Shopping Cart implementation}
|
13
|
+
|
14
|
+
s.rubyforge_project = "acts_as_shopping_cart"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency 'activerecord', '~> 3.0.0'
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_development_dependency "sqlite3"
|
24
|
+
s.add_development_dependency "simplecov"
|
25
|
+
end
|
@@ -0,0 +1,67 @@
|
|
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
|
+
|
@@ -0,0 +1,64 @@
|
|
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
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Acts
|
3
|
+
module ShoppingCart
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
#
|
10
|
+
# Prepares the class to act as a cart.
|
11
|
+
#
|
12
|
+
# Receives as a parameter the name of the class that will hold the items
|
13
|
+
#
|
14
|
+
# Example:
|
15
|
+
#
|
16
|
+
# acts_as_shopping_cart :cart_item
|
17
|
+
#
|
18
|
+
#
|
19
|
+
def acts_as_shopping_cart_using(item_class)
|
20
|
+
self.send :include, ActiveRecord::Acts::ShoppingCart::InstanceMethods
|
21
|
+
has_many :cart_items, :class_name => item_class.to_s.classify, :as => :owner
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Alias for:
|
26
|
+
#
|
27
|
+
# acts_as_shopping_cart_using :shopping_cart_item
|
28
|
+
#
|
29
|
+
def acts_as_shopping_cart
|
30
|
+
acts_as_shopping_cart_using :shopping_cart_item
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Acts
|
3
|
+
module ShoppingCartItem
|
4
|
+
module InstanceMethods
|
5
|
+
#
|
6
|
+
# Returns the subtotal, multiplying the quantity times the price of the item.
|
7
|
+
#
|
8
|
+
def subtotal
|
9
|
+
self.quantity * self.price
|
10
|
+
end
|
11
|
+
|
12
|
+
#
|
13
|
+
# Updates the quantity of the item
|
14
|
+
#
|
15
|
+
def update_quantity(new_quantity)
|
16
|
+
self.quantity = new_quantity
|
17
|
+
self.save
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Updates the price of the item
|
22
|
+
#
|
23
|
+
def update_price(new_price)
|
24
|
+
self.price = new_price
|
25
|
+
self.save
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Acts
|
3
|
+
module ShoppingCartItem
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
#
|
10
|
+
# Prepares the class to act as a cart item.
|
11
|
+
#
|
12
|
+
# Receives as a parameter the name of the class that acts as a cart
|
13
|
+
#
|
14
|
+
# Example:
|
15
|
+
#
|
16
|
+
# acts_as_shopping_cart_item :cart
|
17
|
+
#
|
18
|
+
#
|
19
|
+
def acts_as_shopping_cart_item_for(cart_class)
|
20
|
+
self.send :include, ActiveRecord::Acts::ShoppingCartItem::InstanceMethods
|
21
|
+
belongs_to :owner, :polymorphic => true
|
22
|
+
belongs_to :item, :polymorphic => true
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
# Alias for:
|
27
|
+
#
|
28
|
+
# acts_as_shopping_cart_item_for :shopping_cart
|
29
|
+
#
|
30
|
+
def acts_as_shopping_cart_item
|
31
|
+
acts_as_shopping_cart_item_for :shopping_cart
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'acts_as_shopping_cart/version'
|
2
|
+
require 'active_record/acts/shopping_cart'
|
3
|
+
require 'active_record/acts/shopping_cart/cart_instance_methods'
|
4
|
+
require 'active_record/acts/shopping_cart/item_instance_methods'
|
5
|
+
|
6
|
+
require 'active_record/acts/shopping_cart_item'
|
7
|
+
require 'active_record/acts/shopping_cart_item/cart_item_instance_methods'
|
8
|
+
|
9
|
+
ActiveRecord::Base.send :extend, ActiveRecord::Acts::ShoppingCart::ClassMethods
|
10
|
+
ActiveRecord::Base.send :extend, ActiveRecord::Acts::ShoppingCartItem::ClassMethods
|
@@ -0,0 +1,220 @@
|
|
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
|
+
|
@@ -0,0 +1,46 @@
|
|
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
|
@@ -0,0 +1,125 @@
|
|
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
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "ActsAsShoppingCart" do
|
4
|
+
before(:each) do
|
5
|
+
@cart = SomeCart.create
|
6
|
+
end
|
7
|
+
|
8
|
+
it "has many items" do
|
9
|
+
@cart.should respond_to(:cart_items)
|
10
|
+
something = SomeClass.create
|
11
|
+
|
12
|
+
@cart.should respond_to(:remove)
|
13
|
+
# Test to ensure that any other class doesn't have the methods
|
14
|
+
something.should_not respond_to(:remove)
|
15
|
+
end
|
16
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
# require 'spec'
|
4
|
+
# require 'spec/autorun'
|
5
|
+
# require 'rubygems'
|
6
|
+
#
|
7
|
+
require 'rubygems'
|
8
|
+
require 'bundler/setup'
|
9
|
+
|
10
|
+
require 'simplecov'
|
11
|
+
require 'active_record'
|
12
|
+
|
13
|
+
require 'acts_as_shopping_cart'
|
14
|
+
|
15
|
+
SimpleCov.start
|
16
|
+
|
17
|
+
#
|
18
|
+
# Required environment for the tests
|
19
|
+
#
|
20
|
+
class SomeCart < ActiveRecord::Base
|
21
|
+
acts_as_shopping_cart_using :some_cart_item
|
22
|
+
end
|
23
|
+
|
24
|
+
class SomeCartItem < ActiveRecord::Base
|
25
|
+
acts_as_shopping_cart_item_for :some_cart
|
26
|
+
end
|
27
|
+
|
28
|
+
class ShoppingCart < ActiveRecord::Base
|
29
|
+
acts_as_shopping_cart
|
30
|
+
end
|
31
|
+
|
32
|
+
class ShoppingCartItem < ActiveRecord::Base
|
33
|
+
acts_as_shopping_cart_item
|
34
|
+
end
|
35
|
+
|
36
|
+
class SomeClass < ActiveRecord::Base
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
41
|
+
|
42
|
+
ActiveRecord::Schema.define(:version => 1) do
|
43
|
+
create_table :some_carts do |t|
|
44
|
+
t.float :taxes, :default => 0
|
45
|
+
t.float :shipping_cost, :default => 0
|
46
|
+
end
|
47
|
+
|
48
|
+
create_table :some_classes do |t|
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
create_table :some_cart_items do |t|
|
53
|
+
t.integer :owner_id
|
54
|
+
t.string :owner_type
|
55
|
+
t.integer :quantity
|
56
|
+
t.integer :item_id
|
57
|
+
t.string :item_type
|
58
|
+
t.float :price
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
RSpec.configure do |config|
|
63
|
+
|
64
|
+
end
|
65
|
+
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_shopping_cart
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Padilla
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &70170789806420 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70170789806420
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70170789822380 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70170789822380
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sqlite3
|
38
|
+
requirement: &70170789821920 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70170789821920
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: simplecov
|
49
|
+
requirement: &70170789821500 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70170789821500
|
58
|
+
description: Simple Shopping Cart implementation
|
59
|
+
email:
|
60
|
+
- david@crowdint.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .document
|
66
|
+
- .gitignore
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE
|
69
|
+
- README.markdown
|
70
|
+
- Rakefile
|
71
|
+
- VERSION
|
72
|
+
- acts_as_shopping_cart.gemspec
|
73
|
+
- lib/active_record/acts/shopping_cart.rb
|
74
|
+
- lib/active_record/acts/shopping_cart/cart_instance_methods.rb
|
75
|
+
- lib/active_record/acts/shopping_cart/item_instance_methods.rb
|
76
|
+
- lib/active_record/acts/shopping_cart_item.rb
|
77
|
+
- lib/active_record/acts/shopping_cart_item/cart_item_instance_methods.rb
|
78
|
+
- lib/acts_as_shopping_cart.rb
|
79
|
+
- lib/acts_as_shopping_cart/version.rb
|
80
|
+
- spec/active_record/acts/shopping_cart/cart_instance_methods_spec.rb
|
81
|
+
- spec/active_record/acts/shopping_cart/cart_item_instance_methods_spec.rb
|
82
|
+
- spec/active_record/acts/shopping_cart/item_instance_methods_spec.rb
|
83
|
+
- spec/acts_as_shopping_cart_spec.rb
|
84
|
+
- spec/spec.opts
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
homepage: ''
|
87
|
+
licenses: []
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project: acts_as_shopping_cart
|
106
|
+
rubygems_version: 1.8.10
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Simple Shopping Cart implementation
|
110
|
+
test_files:
|
111
|
+
- spec/active_record/acts/shopping_cart/cart_instance_methods_spec.rb
|
112
|
+
- spec/active_record/acts/shopping_cart/cart_item_instance_methods_spec.rb
|
113
|
+
- spec/active_record/acts/shopping_cart/item_instance_methods_spec.rb
|
114
|
+
- spec/acts_as_shopping_cart_spec.rb
|
115
|
+
- spec/spec.opts
|
116
|
+
- spec/spec_helper.rb
|