acts_as_shopping_cart 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb74faf4e8e1ab7a2a84ae44b351e8d0f0e12e69
4
+ data.tar.gz: 56f5c146d75b6b7c823f09039d5fac7e29e7f0ae
5
+ SHA512:
6
+ metadata.gz: 61cfd0d719b47dc26f72d8f0ca735af97541409717fe34fc6b01404732288ab5347211aab8ca83e93d6a33f4635112f24c754b2daa2e7de2b1987bfe9cb97700
7
+ data.tar.gz: 75dd9884120efe0e5b80b3db1afde432cdda75822c689d6fd67dfaec5a1a61d517fdafd7eb7aa4cc60488e9c801fdf82c9acd17dc352f71bd8163f73d84ed920
data/.travis.yml CHANGED
@@ -1,4 +1,2 @@
1
1
  rvm:
2
- - 1.8.7
3
- - 1.9.2
4
- - 1.9.3
2
+ - 2.0.0
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in acts_as_shopping_cart.gemspec
4
4
  gemspec
data/README.markdown CHANGED
@@ -10,14 +10,27 @@ You can find an example application [here](https://github.com/crowdint/acts_as_s
10
10
 
11
11
  ### Rails 3
12
12
 
13
+ **As of Version 0.2.0 Rails 3 is no longer supported. Please use the 0-1-x branch
14
+ if you still need to implement this gem in a Rails 3 app**
15
+
13
16
  Include it in your Gemfile
14
17
 
15
- gem 'acts_as_shopping_cart', '~> 0.1.4'
18
+ gem 'acts_as_shopping_cart', :github => 'crowdint/acts_as_shopping_cart', :branch => '0-1-x'
16
19
 
17
20
  And run bundler
18
21
 
19
22
  bundle install
20
23
 
24
+ ### Rails 4
25
+
26
+ Just include it in your Gemfile as:
27
+
28
+ gem 'acts_as_shopping_cart', '~> 0.2.0'
29
+
30
+ And run bundle install
31
+
32
+ bundle install
33
+
21
34
  ## Usage
22
35
 
23
36
  You need two models, one to hold the Shopping Carts and another to hold the Items
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_dependency 'activerecord', '~> 3.0'
21
+ s.add_dependency 'activerecord', '~> 4.0.0'
22
22
  s.add_development_dependency "cucumber", "~> 1.2.1"
23
23
  s.add_development_dependency "database_cleaner"
24
24
  s.add_development_dependency "rspec", "~> 2.12.0"
@@ -0,0 +1,88 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module ShoppingCart
4
+ module Collection
5
+ #
6
+ # Adds a product to the cart
7
+ #
8
+ def add(object, price, quantity = 1, cumulative = true)
9
+ cart_item = item_for(object)
10
+
11
+ unless cart_item
12
+ shopping_cart_items.create(:item => object, :price => price, :quantity => quantity)
13
+ else
14
+ cumulative = cumulative == true ? cart_item.quantity : 0
15
+ cart_item.quantity = (cumulative + quantity)
16
+ cart_item.save
17
+ end
18
+ end
19
+
20
+ #
21
+ # Deletes all shopping_cart_items in the shopping_cart
22
+ #
23
+ def clear
24
+ shopping_cart_items.clear
25
+ end
26
+
27
+ #
28
+ # Returns true when the cart is empty
29
+ #
30
+ def empty?
31
+ shopping_cart_items.empty?
32
+ end
33
+
34
+ #
35
+ # Remove an item from the cart
36
+ #
37
+ def remove(object, quantity = 1)
38
+ if cart_item = item_for(object)
39
+ if cart_item.quantity <= quantity
40
+ cart_item.delete
41
+ else
42
+ cart_item.quantity = (cart_item.quantity - quantity)
43
+ cart_item.save
44
+ end
45
+ end
46
+ end
47
+
48
+ #
49
+ # Returns the subtotal by summing the price times quantity for all the items in the cart
50
+ #
51
+ def subtotal
52
+ ("%.2f" % shopping_cart_items.inject(0) { |sum, item| sum += (item.price * item.quantity) }).to_f
53
+ end
54
+
55
+ def shipping_cost
56
+ 0
57
+ end
58
+
59
+ def taxes
60
+ subtotal * self.tax_pct * 0.01
61
+ end
62
+
63
+ def tax_pct
64
+ 8.25
65
+ end
66
+
67
+ #
68
+ # Returns the total by summing the subtotal, taxes and shipping_cost
69
+ #
70
+ def total
71
+ ("%.2f" % (self.subtotal + self.taxes + self.shipping_cost)).to_f
72
+ end
73
+
74
+ #
75
+ # Return the number of unique items in the cart
76
+ #
77
+ def total_unique_items
78
+ shopping_cart_items.inject(0) { |sum, item| sum += item.quantity }
79
+ end
80
+
81
+ def cart_items
82
+ warn "ShoppingCart#cart_items WILL BE DEPRECATED IN LATER VERSIONS OF acts_as_shopping_cart, please use ShoppingCart#shopping_cart_items instead"
83
+ self.shopping_cart_items
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,56 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module ShoppingCart
4
+ module Item
5
+
6
+ #
7
+ # Returns the cart item for the specified object
8
+ #
9
+ def item_for(object)
10
+ shopping_cart_items.where(:item => object).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
+ item ? item.subtotal : 0
20
+ end
21
+
22
+ #
23
+ # Returns the quantity of the specified object
24
+ #
25
+ def quantity_for(object)
26
+ item = item_for(object)
27
+ item ? item.quantity : 0
28
+ end
29
+
30
+ #
31
+ # Updates the quantity of the specified object
32
+ #
33
+ def update_quantity_for(object, new_quantity)
34
+ item = item_for(object)
35
+ item.update_quantity(new_quantity) if item
36
+ end
37
+
38
+ #
39
+ # Returns the price of the specified object
40
+ #
41
+ def price_for(object)
42
+ item = item_for(object)
43
+ item ? item.price : 0
44
+ end
45
+
46
+ #
47
+ # Updates the price of the specified object
48
+ #
49
+ def update_price_for(object, new_price)
50
+ item = item_for(object)
51
+ item.update_price(new_price) if item
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -17,8 +17,8 @@ module ActiveRecord
17
17
  #
18
18
  #
19
19
  def acts_as_shopping_cart_using(item_class)
20
- self.send :include, ActiveRecord::Acts::ShoppingCart::Cart::InstanceMethods
21
- self.send :include, ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods
20
+ self.send :include, ActiveRecord::Acts::ShoppingCart::Collection
21
+ self.send :include, ActiveRecord::Acts::ShoppingCart::Item
22
22
  has_many :shopping_cart_items, :class_name => item_class.to_s.classify,
23
23
  :as => :owner, :dependent => :destroy
24
24
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsShoppingCart
2
- VERSION = "0.1.6"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,25 +1,13 @@
1
1
  require 'acts_as_shopping_cart/version'
2
2
  require 'active_record/acts/shopping_cart'
3
3
  require 'active_record/acts/shopping_cart_item'
4
-
5
- # require 'active_record/acts/shopping_cart/cart_instance_methods'
6
- # require 'active_record/acts/shopping_cart/item_instance_methods'
7
-
8
- # require 'active_record/acts/shopping_cart_item'
9
- # require 'active_record/acts/shopping_cart_item/cart_item_instance_methods'
10
-
11
4
  require 'acts_as_shopping_cart/schema'
12
5
 
13
6
  module ActiveRecord
14
7
  module Acts
15
8
  module ShoppingCart
16
- module Cart
17
- autoload :InstanceMethods, 'active_record/acts/shopping_cart/cart/instance_methods'
18
- end
19
-
20
- module Item
21
- autoload :InstanceMethods, 'active_record/acts/shopping_cart/item/instance_methods'
22
- end
9
+ autoload :Collection , 'active_record/acts/shopping_cart/collection'
10
+ autoload :Item , 'active_record/acts/shopping_cart/item'
23
11
  end
24
12
 
25
13
  module ShoppingCartItem
@@ -1,10 +1,10 @@
1
- require File.expand_path(File.dirname(__FILE__) + '../../../../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '../../../../spec_helper')
2
2
  # require 'spec_helper'
3
3
 
4
- describe ActiveRecord::Acts::ShoppingCart::Cart::InstanceMethods do
4
+ describe ActiveRecord::Acts::ShoppingCart::Collection do
5
5
  let(:klass) do
6
6
  klass = Class.new
7
- klass.send(:include, ActiveRecord::Acts::ShoppingCart::Cart::InstanceMethods)
7
+ klass.send(:include, ActiveRecord::Acts::ShoppingCart::Collection)
8
8
  end
9
9
 
10
10
  let(:subject) do
@@ -1,9 +1,9 @@
1
- require File.expand_path(File.dirname(__FILE__) + '../../../../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '../../../../spec_helper')
2
2
 
3
- describe ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods do
3
+ describe ActiveRecord::Acts::ShoppingCart::Item do
4
4
  let(:klass) do
5
5
  klass = Class.new
6
- klass.send :include, ActiveRecord::Acts::ShoppingCart::Item::InstanceMethods
6
+ klass.send :include, ActiveRecord::Acts::ShoppingCart::Item
7
7
  klass
8
8
  end
9
9
 
@@ -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
- shopping_cart_items.should_receive(:where).with(:item_id => object.id, :item_type => object.class.name).and_return([])
24
+ shopping_cart_items.should_receive(:where).with(:item => object).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
- shopping_cart_items.should_receive(:where).with(:item_id => object.id, :item_type => object.class.name).and_return([ item ])
34
+ shopping_cart_items.should_receive(:where).with(:item => object).and_return([ item ])
35
35
  end
36
36
 
37
37
  it "returns that item" do
metadata CHANGED
@@ -1,36 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_shopping_cart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - David Padilla
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-03-05 00:00:00.000000000 Z
11
+ date: 2013-12-12 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activerecord
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
21
- version: '3.0'
19
+ version: 4.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
29
- version: '3.0'
26
+ version: 4.0.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: cucumber
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -46,23 +41,20 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: database_cleaner
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rspec
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ~>
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ~>
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: sqlite3
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ~>
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ~>
92
81
  - !ruby/object:Gem::Version
@@ -94,23 +83,20 @@ dependencies:
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: simplecov
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - '>='
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - '>='
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: rake
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
101
  - - ~>
116
102
  - !ruby/object:Gem::Version
@@ -118,7 +104,6 @@ dependencies:
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
108
  - - ~>
124
109
  - !ruby/object:Gem::Version
@@ -144,8 +129,8 @@ files:
144
129
  - features/support/env.rb
145
130
  - features/support/rails_env.rb
146
131
  - lib/active_record/acts/shopping_cart.rb
147
- - lib/active_record/acts/shopping_cart/cart/instance_methods.rb
148
- - lib/active_record/acts/shopping_cart/item/instance_methods.rb
132
+ - lib/active_record/acts/shopping_cart/collection.rb
133
+ - lib/active_record/acts/shopping_cart/item.rb
149
134
  - lib/active_record/acts/shopping_cart_item.rb
150
135
  - lib/active_record/acts/shopping_cart_item/instance_methods.rb
151
136
  - lib/acts_as_shopping_cart.rb
@@ -153,39 +138,32 @@ files:
153
138
  - lib/acts_as_shopping_cart/version.rb
154
139
  - script/build.sh
155
140
  - spec/.rspec
156
- - spec/active_record/acts/shopping_cart/cart/instance_methods_spec.rb
157
- - spec/active_record/acts/shopping_cart/item/instance_methods_spec.rb
141
+ - spec/active_record/acts/shopping_cart/collection_spec.rb
142
+ - spec/active_record/acts/shopping_cart/item_spec.rb
158
143
  - spec/active_record/acts/shopping_cart_item/instance_methods_spec.rb
159
144
  - spec/spec_helper.rb
160
145
  homepage: ''
161
146
  licenses: []
147
+ metadata: {}
162
148
  post_install_message:
163
149
  rdoc_options: []
164
150
  require_paths:
165
151
  - lib
166
152
  required_ruby_version: !ruby/object:Gem::Requirement
167
- none: false
168
153
  requirements:
169
- - - ! '>='
154
+ - - '>='
170
155
  - !ruby/object:Gem::Version
171
156
  version: '0'
172
- segments:
173
- - 0
174
- hash: 3103435119654864797
175
157
  required_rubygems_version: !ruby/object:Gem::Requirement
176
- none: false
177
158
  requirements:
178
- - - ! '>='
159
+ - - '>='
179
160
  - !ruby/object:Gem::Version
180
161
  version: '0'
181
- segments:
182
- - 0
183
- hash: 3103435119654864797
184
162
  requirements: []
185
163
  rubyforge_project: acts_as_shopping_cart
186
- rubygems_version: 1.8.23
164
+ rubygems_version: 2.0.3
187
165
  signing_key:
188
- specification_version: 3
166
+ specification_version: 4
189
167
  summary: Simple Shopping Cart implementation
190
168
  test_files:
191
169
  - features/shopping_cart.feature
@@ -193,7 +171,7 @@ test_files:
193
171
  - features/step_definitions/shopping_cart_steps.rb
194
172
  - features/support/env.rb
195
173
  - features/support/rails_env.rb
196
- - spec/active_record/acts/shopping_cart/cart/instance_methods_spec.rb
197
- - spec/active_record/acts/shopping_cart/item/instance_methods_spec.rb
174
+ - spec/active_record/acts/shopping_cart/collection_spec.rb
175
+ - spec/active_record/acts/shopping_cart/item_spec.rb
198
176
  - spec/active_record/acts/shopping_cart_item/instance_methods_spec.rb
199
177
  - spec/spec_helper.rb
@@ -1,90 +0,0 @@
1
- module ActiveRecord
2
- module Acts
3
- module ShoppingCart
4
- module Cart
5
- module InstanceMethods
6
- #
7
- # Adds a product to the cart
8
- #
9
- def add(object, price, quantity = 1, cumulative = true)
10
- cart_item = item_for(object)
11
-
12
- unless cart_item
13
- shopping_cart_items.create(:item => object, :price => price, :quantity => quantity)
14
- else
15
- cumulative = cumulative == true ? cart_item.quantity : 0
16
- cart_item.quantity = (cumulative + quantity)
17
- cart_item.save
18
- end
19
- end
20
-
21
- #
22
- # Deletes all shopping_cart_items in the shopping_cart
23
- #
24
- def clear
25
- shopping_cart_items.clear
26
- end
27
-
28
- #
29
- # Returns true when the cart is empty
30
- #
31
- def empty?
32
- shopping_cart_items.empty?
33
- end
34
-
35
- #
36
- # Remove an item from the cart
37
- #
38
- def remove(object, quantity = 1)
39
- if cart_item = item_for(object)
40
- if cart_item.quantity <= quantity
41
- cart_item.delete
42
- else
43
- cart_item.quantity = (cart_item.quantity - quantity)
44
- cart_item.save
45
- end
46
- end
47
- end
48
-
49
- #
50
- # Returns the subtotal by summing the price times quantity for all the items in the cart
51
- #
52
- def subtotal
53
- ("%.2f" % shopping_cart_items.inject(0) { |sum, item| sum += (item.price * item.quantity) }).to_f
54
- end
55
-
56
- def shipping_cost
57
- 0
58
- end
59
-
60
- def taxes
61
- subtotal * self.tax_pct * 0.01
62
- end
63
-
64
- def tax_pct
65
- 8.25
66
- end
67
-
68
- #
69
- # Returns the total by summing the subtotal, taxes and shipping_cost
70
- #
71
- def total
72
- ("%.2f" % (self.subtotal + self.taxes + self.shipping_cost)).to_f
73
- end
74
-
75
- #
76
- # Return the number of unique items in the cart
77
- #
78
- def total_unique_items
79
- shopping_cart_items.inject(0) { |sum, item| sum += item.quantity }
80
- end
81
-
82
- def cart_items
83
- warn "ShoppingCart#cart_items WILL BE DEPRECATED IN LATER VERSIONS OF acts_as_shopping_cart, please use ShoppingCart#shopping_cart_items instead"
84
- self.shopping_cart_items
85
- end
86
- end
87
- end
88
- end
89
- end
90
- end
@@ -1,58 +0,0 @@
1
- module ActiveRecord
2
- module Acts
3
- module ShoppingCart
4
- module Item
5
- module InstanceMethods
6
-
7
- #
8
- # Returns the cart item for the specified object
9
- #
10
- def item_for(object)
11
- shopping_cart_items.where(:item_id => object.id, :item_type => object.class.name).first
12
- end
13
-
14
- #
15
- # Returns the subtotal of a specified item by multiplying the quantity times
16
- # the price of the item.
17
- #
18
- def subtotal_for(object)
19
- item = item_for(object)
20
- item ? item.subtotal : 0
21
- end
22
-
23
- #
24
- # Returns the quantity of the specified object
25
- #
26
- def quantity_for(object)
27
- item = item_for(object)
28
- item ? item.quantity : 0
29
- end
30
-
31
- #
32
- # Updates the quantity of the specified object
33
- #
34
- def update_quantity_for(object, new_quantity)
35
- item = item_for(object)
36
- item.update_quantity(new_quantity) if item
37
- end
38
-
39
- #
40
- # Returns the price of the specified object
41
- #
42
- def price_for(object)
43
- item = item_for(object)
44
- item ? item.price : 0
45
- end
46
-
47
- #
48
- # Updates the price of the specified object
49
- #
50
- def update_price_for(object, new_price)
51
- item = item_for(object)
52
- item.update_price(new_price) if item
53
- end
54
- end
55
- end
56
- end
57
- end
58
- end