Commit 912190c8645e9a3f537d72cf62d3ae6098076b55
1 parent
be629de2
Exists in
master
and in
29 other branches
Extract Method to encapsulate access to the cart
Showing
1 changed file
with
29 additions
and
19 deletions
Show diff stats
plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb
... | ... | @@ -7,10 +7,10 @@ class ShoppingCartPluginProfileController < ProfileController |
7 | 7 | before_filter :login_required, :only => [] |
8 | 8 | |
9 | 9 | def add |
10 | - session[:cart] = { :enterprise_id => profile.id, :items => {} } if session[:cart].nil? | |
10 | + self.cart = { :enterprise_id => profile.id, :items => {} } if self.cart.nil? | |
11 | 11 | if validate_same_enterprise && product = validate_enterprise_has_product(params[:id]) |
12 | - session[:cart][:items][product.id] = 0 if session[:cart][:items][product.id].nil? | |
13 | - session[:cart][:items][product.id] += 1 | |
12 | + self.cart[:items][product.id] = 0 if self.cart[:items][product.id].nil? | |
13 | + self.cart[:items][product.id] += 1 | |
14 | 14 | render :text => { |
15 | 15 | :ok => true, |
16 | 16 | :error => {:code => 0}, |
... | ... | @@ -20,7 +20,7 @@ class ShoppingCartPluginProfileController < ProfileController |
20 | 20 | :price => get_price(product, profile.environment), |
21 | 21 | :description => product.description, |
22 | 22 | :picture => product.default_image(:minor), |
23 | - :quantity => session[:cart][:items][product.id] | |
23 | + :quantity => self.cart[:items][product.id] | |
24 | 24 | }] |
25 | 25 | }.to_json |
26 | 26 | end |
... | ... | @@ -29,8 +29,8 @@ class ShoppingCartPluginProfileController < ProfileController |
29 | 29 | def remove |
30 | 30 | id = params[:id].to_i |
31 | 31 | if validate_cart_presence && validate_cart_has_product(id) |
32 | - session[:cart][:items].delete(id) | |
33 | - session[:cart] = nil if session[:cart][:items].empty? | |
32 | + self.cart[:items].delete(id) | |
33 | + self.cart = nil if self.cart[:items].empty? | |
34 | 34 | render :text => { |
35 | 35 | :ok => true, |
36 | 36 | :error => {:code => 0}, |
... | ... | @@ -41,7 +41,7 @@ class ShoppingCartPluginProfileController < ProfileController |
41 | 41 | |
42 | 42 | def list |
43 | 43 | if validate_cart_presence |
44 | - products = session[:cart][:items].collect do |id, quantity| | |
44 | + products = self.cart[:items].collect do |id, quantity| | |
45 | 45 | product = Product.find(id) |
46 | 46 | { :id => product.id, |
47 | 47 | :name => product.name, |
... | ... | @@ -54,7 +54,7 @@ class ShoppingCartPluginProfileController < ProfileController |
54 | 54 | render :text => { |
55 | 55 | :ok => true, |
56 | 56 | :error => {:code => 0}, |
57 | - :enterprise => Enterprise.find(session[:cart][:enterprise_id]).identifier, | |
57 | + :enterprise => Enterprise.find(self.cart[:enterprise_id]).identifier, | |
58 | 58 | :products => products |
59 | 59 | }.to_json |
60 | 60 | end |
... | ... | @@ -65,7 +65,7 @@ class ShoppingCartPluginProfileController < ProfileController |
65 | 65 | id = params[:id].to_i |
66 | 66 | if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) |
67 | 67 | product = Product.find(id) |
68 | - session[:cart][:items][product.id] = quantity | |
68 | + self.cart[:items][product.id] = quantity | |
69 | 69 | render :text => { |
70 | 70 | :ok => true, |
71 | 71 | :error => {:code => 0}, |
... | ... | @@ -76,7 +76,7 @@ class ShoppingCartPluginProfileController < ProfileController |
76 | 76 | end |
77 | 77 | |
78 | 78 | def clean |
79 | - session[:cart] = nil | |
79 | + self.cart = nil | |
80 | 80 | render :text => { |
81 | 81 | :ok => true, |
82 | 82 | :error => {:code => 0} |
... | ... | @@ -89,10 +89,10 @@ class ShoppingCartPluginProfileController < ProfileController |
89 | 89 | end |
90 | 90 | |
91 | 91 | def send_request |
92 | - register_order(params[:customer], session[:cart][:items]) | |
92 | + register_order(params[:customer], self.cart[:items]) | |
93 | 93 | begin |
94 | - ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, session[:cart][:items]) | |
95 | - ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, session[:cart][:items]) | |
94 | + ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, self.cart[:items]) | |
95 | + ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, self.cart[:items]) | |
96 | 96 | render :text => { |
97 | 97 | :ok => true, |
98 | 98 | :message => _('Request sent successfully. Check your email.'), |
... | ... | @@ -110,12 +110,12 @@ class ShoppingCartPluginProfileController < ProfileController |
110 | 110 | end |
111 | 111 | |
112 | 112 | def visibility |
113 | - render :text => session[:cart].has_key?(:visibility) ? session[:cart][:visibility].to_json : true.to_json | |
113 | + render :text => self.cart.has_key?(:visibility) ? self.cart[:visibility].to_json : true.to_json | |
114 | 114 | end |
115 | 115 | |
116 | 116 | def show |
117 | 117 | begin |
118 | - session[:cart][:visibility] = true | |
118 | + self.cart[:visibility] = true | |
119 | 119 | render :text => { |
120 | 120 | :ok => true, |
121 | 121 | :message => _('Basket displayed.'), |
... | ... | @@ -134,7 +134,7 @@ class ShoppingCartPluginProfileController < ProfileController |
134 | 134 | |
135 | 135 | def hide |
136 | 136 | begin |
137 | - session[:cart][:visibility] = false | |
137 | + self.cart[:visibility] = false | |
138 | 138 | render :text => { |
139 | 139 | :ok => true, |
140 | 140 | :message => _('Basket hidden.'), |
... | ... | @@ -154,7 +154,7 @@ class ShoppingCartPluginProfileController < ProfileController |
154 | 154 | private |
155 | 155 | |
156 | 156 | def validate_same_enterprise |
157 | - if profile.id != session[:cart][:enterprise_id] | |
157 | + if profile.id != self.cart[:enterprise_id] | |
158 | 158 | render :text => { |
159 | 159 | :ok => false, |
160 | 160 | :error => { |
... | ... | @@ -168,7 +168,7 @@ class ShoppingCartPluginProfileController < ProfileController |
168 | 168 | end |
169 | 169 | |
170 | 170 | def validate_cart_presence |
171 | - if session[:cart].nil? | |
171 | + if self.cart.nil? | |
172 | 172 | render :text => { |
173 | 173 | :ok => false, |
174 | 174 | :error => { |
... | ... | @@ -198,7 +198,7 @@ class ShoppingCartPluginProfileController < ProfileController |
198 | 198 | end |
199 | 199 | |
200 | 200 | def validate_cart_has_product(id) |
201 | - if !session[:cart][:items].has_key?(id) | |
201 | + if !self.cart[:items].has_key?(id) | |
202 | 202 | render :text => { |
203 | 203 | :ok => false, |
204 | 204 | :error => { |
... | ... | @@ -245,4 +245,14 @@ class ShoppingCartPluginProfileController < ProfileController |
245 | 245 | :customer_zip_code => params[:customer][:zip_code] |
246 | 246 | ) |
247 | 247 | end |
248 | + | |
249 | + protected | |
250 | + | |
251 | + def cart | |
252 | + session[:cart] | |
253 | + end | |
254 | + | |
255 | + def cart=(data) | |
256 | + session[:cart] = data | |
257 | + end | |
248 | 258 | end | ... | ... |