shop_bunny 0.7.4.16 → 0.7.4.17
Sign up to get free protection for your applications and to get access to all the features.
data/app/models/cart_item.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
class CartItem < ActiveRecord::Base
|
2
2
|
belongs_to :cart
|
3
|
-
belongs_to :item, :class_name => ShopBunny.item_model_class_name
|
3
|
+
# belongs_to :item, :class_name => ShopBunny.item_model_class_name
|
4
4
|
|
5
5
|
validates_presence_of :cart_id
|
6
|
-
validates_presence_of :item_id
|
6
|
+
# validates_presence_of :item_id
|
7
7
|
validates_numericality_of :quantity
|
8
8
|
|
9
9
|
before_validation :set_default_quantity
|
@@ -17,6 +17,24 @@ class CartItem < ActiveRecord::Base
|
|
17
17
|
def total_price
|
18
18
|
quantity * item.price
|
19
19
|
end
|
20
|
+
|
21
|
+
def item=(new_item)
|
22
|
+
write_attribute(:raw_item, new_item.to_json)
|
23
|
+
@parsed_raw_item = nil
|
24
|
+
new_item
|
25
|
+
end
|
26
|
+
|
27
|
+
def item
|
28
|
+
return nil unless raw_item
|
29
|
+
unless @parsed_raw_item
|
30
|
+
json = JSON.parse(raw_item).values.first
|
31
|
+
if json
|
32
|
+
@parsed_raw_item = ShopBunny.item_model_class_name.constantize.new(json)
|
33
|
+
@parsed_raw_item.id = json['id']
|
34
|
+
end
|
35
|
+
end
|
36
|
+
@parsed_raw_item
|
37
|
+
end
|
20
38
|
|
21
39
|
private
|
22
40
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class AddRawItemToCartItems < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
add_column :cart_items, :raw_item, :text
|
4
|
+
|
5
|
+
CartItem.all.each do |e|
|
6
|
+
if e.item
|
7
|
+
e.raw_item = e.item.to_json
|
8
|
+
e.save!
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
remove_column :cart_items, :raw_item
|
15
|
+
end
|
16
|
+
end
|
@@ -56,7 +56,8 @@ module ShopBunny
|
|
56
56
|
#increases the quantity of an article. creates a new one if it doesn't exist
|
57
57
|
def add_item(item,options = {})
|
58
58
|
options[:quantity] ||= 1
|
59
|
-
cart_item = self.cart_items.
|
59
|
+
cart_item = self.cart_items.select {|e| e.item.id == item.id}.first
|
60
|
+
cart_item ||= self.cart_items.build(:item => item)
|
60
61
|
cart_item.quantity += options[:quantity]
|
61
62
|
cart_item.save!
|
62
63
|
|
@@ -69,7 +70,7 @@ module ShopBunny
|
|
69
70
|
|
70
71
|
#removes a quantity of an article specified by :article_id, returns nil if no article has been found
|
71
72
|
def remove_item(item,options = {})
|
72
|
-
cart_item = self.cart_items.
|
73
|
+
cart_item = self.cart_items.select {|e| e.item.id == item.id}.first
|
73
74
|
if cart_item
|
74
75
|
options[:quantity] ||= cart_item.quantity
|
75
76
|
if cart_item
|
@@ -86,7 +87,7 @@ module ShopBunny
|
|
86
87
|
|
87
88
|
#sets the quantity of an article specified by :article_id, returns nil if no article has been found
|
88
89
|
def update_item(item,options)
|
89
|
-
cart_item = self.cart_items.
|
90
|
+
cart_item = self.cart_items.select {|e| e.item.id == item.id}.first
|
90
91
|
if cart_item
|
91
92
|
cart_item.quantity = options[:quantity]
|
92
93
|
cart_item.save!
|
@@ -143,7 +144,7 @@ module ShopBunny
|
|
143
144
|
use.destroy if use.coupon.value_of_automatic_add
|
144
145
|
end
|
145
146
|
|
146
|
-
save
|
147
|
+
save
|
147
148
|
reload
|
148
149
|
|
149
150
|
Coupon.valid.automatically_added_over(item_sum).each do |coupon|
|
data/lib/shop_bunny.rb
CHANGED
@@ -18,5 +18,28 @@ module ShopBunny
|
|
18
18
|
# initializer which gets generated by `rails generate shop_bunny:install`
|
19
19
|
def self.setup
|
20
20
|
yield self
|
21
|
+
|
22
|
+
item_class = ShopBunny.item_model_class_name.constantize
|
23
|
+
|
24
|
+
unless item_class.method_defined?(:as_json_with_included_id)
|
25
|
+
item_class.class_eval do
|
26
|
+
def shop_bunny_json_options(options)
|
27
|
+
options ||= {}
|
28
|
+
options[:methods] ||= []
|
29
|
+
if options[:methods].kind_of?(Array)
|
30
|
+
options[:methods] += [:id]
|
31
|
+
else
|
32
|
+
options[:methods] = [options[:methods], :id]
|
33
|
+
end
|
34
|
+
options[:methods].uniq!
|
35
|
+
options
|
36
|
+
end
|
37
|
+
|
38
|
+
def as_json_with_included_id(options = {})
|
39
|
+
as_json_without_included_id(shop_bunny_json_options(options))
|
40
|
+
end
|
41
|
+
alias_method_chain :as_json, :included_id
|
42
|
+
end
|
43
|
+
end
|
21
44
|
end
|
22
45
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shop_bunny
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 69
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 7
|
9
9
|
- 4
|
10
|
-
-
|
11
|
-
version: 0.7.4.
|
10
|
+
- 17
|
11
|
+
version: 0.7.4.17
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- kopfmaschine.com
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date:
|
19
|
+
date: 2011-02-03 00:00:00 +01:00
|
20
20
|
default_executable:
|
21
21
|
dependencies: []
|
22
22
|
|
@@ -30,34 +30,36 @@ extensions: []
|
|
30
30
|
extra_rdoc_files: []
|
31
31
|
|
32
32
|
files:
|
33
|
-
- lib/generators/shop_bunny/templates/shopbunny_model_template.rb
|
34
|
-
- lib/generators/shop_bunny/templates/shopbunny_controller_template.rb
|
35
33
|
- lib/generators/shop_bunny/install_generator.rb
|
36
|
-
- lib/shop_bunny.rb
|
34
|
+
- lib/generators/shop_bunny/templates/shopbunny_controller_template.rb
|
35
|
+
- lib/generators/shop_bunny/templates/shopbunny_model_template.rb
|
37
36
|
- lib/shop_bunny/cart_controller_module.rb
|
38
37
|
- lib/shop_bunny/cart_module.rb
|
39
|
-
- lib/shop_bunny/shipping_cost_calculator.rb
|
40
38
|
- lib/shop_bunny/engine.rb
|
41
|
-
-
|
39
|
+
- lib/shop_bunny/shipping_cost_calculator.rb
|
40
|
+
- lib/shop_bunny.rb
|
41
|
+
- app/controllers/application_controller.rb
|
42
|
+
- app/controllers/carts_controller.rb
|
42
43
|
- app/models/cart.rb
|
43
|
-
- app/models/coupon_use.rb
|
44
44
|
- app/models/cart_item.rb
|
45
45
|
- app/models/coupon.rb
|
46
|
+
- app/models/coupon_use.rb
|
46
47
|
- app/models/item.rb
|
47
|
-
- app/
|
48
|
-
- app/controllers/carts_controller.rb
|
49
|
-
- db/migrate/20101125151017_change_valid_from_and_until_to_datetime_on_coupons.rb
|
50
|
-
- db/migrate/20100915162029_remove_owner_id_from_carts.rb
|
51
|
-
- db/migrate/20100915091059_create_coupons.rb
|
52
|
-
- db/migrate/20101125140137_add_value_of_automatic_add_to_coupons.rb
|
53
|
-
- db/migrate/20100915073016_create_items.rb
|
48
|
+
- app/views/carts/show.html.erb
|
54
49
|
- db/migrate/20100902115627_create_carts.rb
|
50
|
+
- db/migrate/20100902115757_create_cart_items.rb
|
51
|
+
- db/migrate/20100915073016_create_items.rb
|
52
|
+
- db/migrate/20100915091059_create_coupons.rb
|
55
53
|
- db/migrate/20100915093821_create_coupon_uses.rb
|
54
|
+
- db/migrate/20100915162029_remove_owner_id_from_carts.rb
|
56
55
|
- db/migrate/20100916194407_set_default_column_value_for_cart_items_quantity.rb
|
57
|
-
- db/migrate/
|
56
|
+
- db/migrate/20101125140137_add_value_of_automatic_add_to_coupons.rb
|
57
|
+
- db/migrate/20101125151017_change_valid_from_and_until_to_datetime_on_coupons.rb
|
58
|
+
- db/migrate/20110202235446_add_raw_item_to_cart_items.rb
|
59
|
+
- db/migrate/20110203005514_remove_item_id_from_cart_items.rb
|
58
60
|
- config/initializers/add_cart_finder_to_controllers.rb
|
59
|
-
- config/initializers/shop_bunny.rb
|
60
61
|
- config/initializers/machinist.rb
|
62
|
+
- config/initializers/shop_bunny.rb
|
61
63
|
- config/routes.rb
|
62
64
|
has_rdoc: true
|
63
65
|
homepage: http://kopfmaschine.com
|