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,10 +7,10 @@ class ShoppingCartPluginProfileController < ProfileController | ||
7 | before_filter :login_required, :only => [] | 7 | before_filter :login_required, :only => [] |
8 | 8 | ||
9 | def add | 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 | if validate_same_enterprise && product = validate_enterprise_has_product(params[:id]) | 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 | render :text => { | 14 | render :text => { |
15 | :ok => true, | 15 | :ok => true, |
16 | :error => {:code => 0}, | 16 | :error => {:code => 0}, |
@@ -20,7 +20,7 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -20,7 +20,7 @@ class ShoppingCartPluginProfileController < ProfileController | ||
20 | :price => get_price(product, profile.environment), | 20 | :price => get_price(product, profile.environment), |
21 | :description => product.description, | 21 | :description => product.description, |
22 | :picture => product.default_image(:minor), | 22 | :picture => product.default_image(:minor), |
23 | - :quantity => session[:cart][:items][product.id] | 23 | + :quantity => self.cart[:items][product.id] |
24 | }] | 24 | }] |
25 | }.to_json | 25 | }.to_json |
26 | end | 26 | end |
@@ -29,8 +29,8 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -29,8 +29,8 @@ class ShoppingCartPluginProfileController < ProfileController | ||
29 | def remove | 29 | def remove |
30 | id = params[:id].to_i | 30 | id = params[:id].to_i |
31 | if validate_cart_presence && validate_cart_has_product(id) | 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 | render :text => { | 34 | render :text => { |
35 | :ok => true, | 35 | :ok => true, |
36 | :error => {:code => 0}, | 36 | :error => {:code => 0}, |
@@ -41,7 +41,7 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -41,7 +41,7 @@ class ShoppingCartPluginProfileController < ProfileController | ||
41 | 41 | ||
42 | def list | 42 | def list |
43 | if validate_cart_presence | 43 | if validate_cart_presence |
44 | - products = session[:cart][:items].collect do |id, quantity| | 44 | + products = self.cart[:items].collect do |id, quantity| |
45 | product = Product.find(id) | 45 | product = Product.find(id) |
46 | { :id => product.id, | 46 | { :id => product.id, |
47 | :name => product.name, | 47 | :name => product.name, |
@@ -54,7 +54,7 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -54,7 +54,7 @@ class ShoppingCartPluginProfileController < ProfileController | ||
54 | render :text => { | 54 | render :text => { |
55 | :ok => true, | 55 | :ok => true, |
56 | :error => {:code => 0}, | 56 | :error => {:code => 0}, |
57 | - :enterprise => Enterprise.find(session[:cart][:enterprise_id]).identifier, | 57 | + :enterprise => Enterprise.find(self.cart[:enterprise_id]).identifier, |
58 | :products => products | 58 | :products => products |
59 | }.to_json | 59 | }.to_json |
60 | end | 60 | end |
@@ -65,7 +65,7 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -65,7 +65,7 @@ class ShoppingCartPluginProfileController < ProfileController | ||
65 | id = params[:id].to_i | 65 | id = params[:id].to_i |
66 | if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) | 66 | if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) |
67 | product = Product.find(id) | 67 | product = Product.find(id) |
68 | - session[:cart][:items][product.id] = quantity | 68 | + self.cart[:items][product.id] = quantity |
69 | render :text => { | 69 | render :text => { |
70 | :ok => true, | 70 | :ok => true, |
71 | :error => {:code => 0}, | 71 | :error => {:code => 0}, |
@@ -76,7 +76,7 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -76,7 +76,7 @@ class ShoppingCartPluginProfileController < ProfileController | ||
76 | end | 76 | end |
77 | 77 | ||
78 | def clean | 78 | def clean |
79 | - session[:cart] = nil | 79 | + self.cart = nil |
80 | render :text => { | 80 | render :text => { |
81 | :ok => true, | 81 | :ok => true, |
82 | :error => {:code => 0} | 82 | :error => {:code => 0} |
@@ -89,10 +89,10 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -89,10 +89,10 @@ class ShoppingCartPluginProfileController < ProfileController | ||
89 | end | 89 | end |
90 | 90 | ||
91 | def send_request | 91 | def send_request |
92 | - register_order(params[:customer], session[:cart][:items]) | 92 | + register_order(params[:customer], self.cart[:items]) |
93 | begin | 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 | render :text => { | 96 | render :text => { |
97 | :ok => true, | 97 | :ok => true, |
98 | :message => _('Request sent successfully. Check your email.'), | 98 | :message => _('Request sent successfully. Check your email.'), |
@@ -110,12 +110,12 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -110,12 +110,12 @@ class ShoppingCartPluginProfileController < ProfileController | ||
110 | end | 110 | end |
111 | 111 | ||
112 | def visibility | 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 | end | 114 | end |
115 | 115 | ||
116 | def show | 116 | def show |
117 | begin | 117 | begin |
118 | - session[:cart][:visibility] = true | 118 | + self.cart[:visibility] = true |
119 | render :text => { | 119 | render :text => { |
120 | :ok => true, | 120 | :ok => true, |
121 | :message => _('Basket displayed.'), | 121 | :message => _('Basket displayed.'), |
@@ -134,7 +134,7 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -134,7 +134,7 @@ class ShoppingCartPluginProfileController < ProfileController | ||
134 | 134 | ||
135 | def hide | 135 | def hide |
136 | begin | 136 | begin |
137 | - session[:cart][:visibility] = false | 137 | + self.cart[:visibility] = false |
138 | render :text => { | 138 | render :text => { |
139 | :ok => true, | 139 | :ok => true, |
140 | :message => _('Basket hidden.'), | 140 | :message => _('Basket hidden.'), |
@@ -154,7 +154,7 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -154,7 +154,7 @@ class ShoppingCartPluginProfileController < ProfileController | ||
154 | private | 154 | private |
155 | 155 | ||
156 | def validate_same_enterprise | 156 | def validate_same_enterprise |
157 | - if profile.id != session[:cart][:enterprise_id] | 157 | + if profile.id != self.cart[:enterprise_id] |
158 | render :text => { | 158 | render :text => { |
159 | :ok => false, | 159 | :ok => false, |
160 | :error => { | 160 | :error => { |
@@ -168,7 +168,7 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -168,7 +168,7 @@ class ShoppingCartPluginProfileController < ProfileController | ||
168 | end | 168 | end |
169 | 169 | ||
170 | def validate_cart_presence | 170 | def validate_cart_presence |
171 | - if session[:cart].nil? | 171 | + if self.cart.nil? |
172 | render :text => { | 172 | render :text => { |
173 | :ok => false, | 173 | :ok => false, |
174 | :error => { | 174 | :error => { |
@@ -198,7 +198,7 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -198,7 +198,7 @@ class ShoppingCartPluginProfileController < ProfileController | ||
198 | end | 198 | end |
199 | 199 | ||
200 | def validate_cart_has_product(id) | 200 | def validate_cart_has_product(id) |
201 | - if !session[:cart][:items].has_key?(id) | 201 | + if !self.cart[:items].has_key?(id) |
202 | render :text => { | 202 | render :text => { |
203 | :ok => false, | 203 | :ok => false, |
204 | :error => { | 204 | :error => { |
@@ -245,4 +245,14 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -245,4 +245,14 @@ class ShoppingCartPluginProfileController < ProfileController | ||
245 | :customer_zip_code => params[:customer][:zip_code] | 245 | :customer_zip_code => params[:customer][:zip_code] |
246 | ) | 246 | ) |
247 | end | 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 | end | 258 | end |