Commit f8bd7f1b7b9deab613e21aeaf4885a30c096551e
Exists in
master
and in
29 other branches
Merge branch 'merge-requests/261' into stable
Showing
20 changed files
with
621 additions
and
547 deletions
Show diff stats
etc/noosfero/varnish-noosfero.vcl
1 | sub vcl_recv { | 1 | sub vcl_recv { |
2 | if (req.request == "GET" || req.request == "HEAD") { | 2 | if (req.request == "GET" || req.request == "HEAD") { |
3 | if (req.http.Cookie) { | 3 | if (req.http.Cookie) { |
4 | - # We only care about the "_noosfero_session.*" cookie, used for | ||
5 | - # authentication. | ||
6 | - if (req.http.Cookie !~ "_noosfero_session.*" ) { | 4 | + # We only care about the "_noosfero_.*" cookies, used by Noosfero |
5 | + if (req.http.Cookie !~ "_noosfero_.*" ) { | ||
7 | # strip all cookies | 6 | # strip all cookies |
8 | unset req.http.Cookie; | 7 | unset req.http.Cookie; |
9 | } | 8 | } |
gitignore.example
@@ -21,8 +21,8 @@ public/user_themes | @@ -21,8 +21,8 @@ public/user_themes | ||
21 | public/designs/themes/default | 21 | public/designs/themes/default |
22 | public/designs/themes/* | 22 | public/designs/themes/* |
23 | public/designs/icons/default | 23 | public/designs/icons/default |
24 | -public/javascripts/cache*.js | ||
25 | -public/stylesheets/cache*.css | 24 | +public/javascripts/cache* |
25 | +public/stylesheets/cache* | ||
26 | public/plugins | 26 | public/plugins |
27 | db/development.db | 27 | db/development.db |
28 | db/production.db | 28 | db/production.db |
plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb
0 → 100644
@@ -0,0 +1,296 @@ | @@ -0,0 +1,296 @@ | ||
1 | +require 'base64' | ||
2 | + | ||
3 | +class ShoppingCartPluginController < PublicController | ||
4 | + | ||
5 | + include ShoppingCartPlugin::CartHelper | ||
6 | + helper ShoppingCartPlugin::CartHelper | ||
7 | + | ||
8 | + append_view_path File.join(File.dirname(__FILE__) + '/../views') | ||
9 | + before_filter :login_required, :only => [] | ||
10 | + | ||
11 | + before_filter :login_required, :only => [] | ||
12 | + | ||
13 | + def get | ||
14 | + config = | ||
15 | + if cart.nil? | ||
16 | + { 'enterprise_id' => nil, 'hasProducts' => false } | ||
17 | + else | ||
18 | + { 'enterprise_id' => cart[:enterprise_id], 'hasProducts' => (cart[:items].keys.size > 0) } | ||
19 | + end | ||
20 | + render :text => config.to_json | ||
21 | + end | ||
22 | + | ||
23 | + def add | ||
24 | + product = find_product(params[:id]) | ||
25 | + if product && enterprise = validate_same_enterprise(product) | ||
26 | + self.cart = { :enterprise_id => enterprise.id, :items => {} } if self.cart.nil? | ||
27 | + self.cart[:items][product.id] = 0 if self.cart[:items][product.id].nil? | ||
28 | + self.cart[:items][product.id] += 1 | ||
29 | + render :text => { | ||
30 | + :ok => true, | ||
31 | + :error => {:code => 0}, | ||
32 | + :products => [{ | ||
33 | + :id => product.id, | ||
34 | + :name => product.name, | ||
35 | + :price => get_price(product, enterprise.environment), | ||
36 | + :description => product.description, | ||
37 | + :picture => product.default_image(:minor), | ||
38 | + :quantity => self.cart[:items][product.id] | ||
39 | + }] | ||
40 | + }.to_json | ||
41 | + end | ||
42 | + end | ||
43 | + | ||
44 | + def remove | ||
45 | + id = params[:id].to_i | ||
46 | + if validate_cart_presence && validate_cart_has_product(id) | ||
47 | + self.cart[:items].delete(id) | ||
48 | + self.cart = nil if self.cart[:items].empty? | ||
49 | + render :text => { | ||
50 | + :ok => true, | ||
51 | + :error => {:code => 0}, | ||
52 | + :product_id => id | ||
53 | + }.to_json | ||
54 | + end | ||
55 | + end | ||
56 | + | ||
57 | + def list | ||
58 | + if validate_cart_presence | ||
59 | + products = self.cart[:items].collect do |id, quantity| | ||
60 | + product = Product.find(id) | ||
61 | + { :id => product.id, | ||
62 | + :name => product.name, | ||
63 | + :price => get_price(product, product.enterprise.environment), | ||
64 | + :description => product.description, | ||
65 | + :picture => product.default_image(:minor), | ||
66 | + :quantity => quantity | ||
67 | + } | ||
68 | + end | ||
69 | + render :text => { | ||
70 | + :ok => true, | ||
71 | + :error => {:code => 0}, | ||
72 | + :products => products | ||
73 | + }.to_json | ||
74 | + end | ||
75 | + end | ||
76 | + | ||
77 | + def update_quantity | ||
78 | + quantity = params[:quantity].to_i | ||
79 | + id = params[:id].to_i | ||
80 | + if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) | ||
81 | + product = Product.find(id) | ||
82 | + self.cart[:items][product.id] = quantity | ||
83 | + render :text => { | ||
84 | + :ok => true, | ||
85 | + :error => {:code => 0}, | ||
86 | + :product_id => id, | ||
87 | + :quantity => quantity | ||
88 | + }.to_json | ||
89 | + end | ||
90 | + end | ||
91 | + | ||
92 | + def clean | ||
93 | + self.cart = nil | ||
94 | + render :text => { | ||
95 | + :ok => true, | ||
96 | + :error => {:code => 0} | ||
97 | + }.to_json | ||
98 | + end | ||
99 | + | ||
100 | + def buy | ||
101 | + @cart = cart | ||
102 | + @enterprise = environment.enterprises.find(cart[:enterprise_id]) | ||
103 | + render :layout => false | ||
104 | + end | ||
105 | + | ||
106 | + def send_request | ||
107 | + register_order(params[:customer], self.cart[:items]) | ||
108 | + begin | ||
109 | + enterprise = Enterprise.find(cart[:enterprise_id]) | ||
110 | + ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], enterprise, self.cart[:items]) | ||
111 | + ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], enterprise, self.cart[:items]) | ||
112 | + self.cart = nil | ||
113 | + render :text => { | ||
114 | + :ok => true, | ||
115 | + :message => _('Request sent successfully. Check your email.'), | ||
116 | + :error => {:code => 0} | ||
117 | + }.to_json | ||
118 | + rescue ActiveRecord::ActiveRecordError | ||
119 | + render :text => { | ||
120 | + :ok => false, | ||
121 | + :error => { | ||
122 | + :code => 6, | ||
123 | + :message => exception.message | ||
124 | + } | ||
125 | + }.to_json | ||
126 | + end | ||
127 | + end | ||
128 | + | ||
129 | + def visibility | ||
130 | + render :text => self.cart.has_key?(:visibility) ? self.cart[:visibility].to_json : true.to_json | ||
131 | + end | ||
132 | + | ||
133 | + def show | ||
134 | + begin | ||
135 | + self.cart[:visibility] = true | ||
136 | + render :text => { | ||
137 | + :ok => true, | ||
138 | + :message => _('Basket displayed.'), | ||
139 | + :error => {:code => 0} | ||
140 | + }.to_json | ||
141 | + rescue Exception => exception | ||
142 | + render :text => { | ||
143 | + :ok => false, | ||
144 | + :error => { | ||
145 | + :code => 7, | ||
146 | + :message => exception.message | ||
147 | + } | ||
148 | + }.to_json | ||
149 | + end | ||
150 | + end | ||
151 | + | ||
152 | + def hide | ||
153 | + begin | ||
154 | + self.cart[:visibility] = false | ||
155 | + render :text => { | ||
156 | + :ok => true, | ||
157 | + :message => _('Basket hidden.'), | ||
158 | + :error => {:code => 0} | ||
159 | + }.to_json | ||
160 | + rescue Exception => exception | ||
161 | + render :text => { | ||
162 | + :ok => false, | ||
163 | + :error => { | ||
164 | + :code => 8, | ||
165 | + :message => exception.message | ||
166 | + } | ||
167 | + }.to_json | ||
168 | + end | ||
169 | + end | ||
170 | + | ||
171 | + private | ||
172 | + | ||
173 | + def validate_same_enterprise(product) | ||
174 | + if self.cart && self.cart[:enterprise_id] && product.enterprise_id != self.cart[:enterprise_id] | ||
175 | + render :text => { | ||
176 | + :ok => false, | ||
177 | + :error => { | ||
178 | + :code => 1, | ||
179 | + :message => _("Can't join items from different enterprises.") | ||
180 | + } | ||
181 | + }.to_json | ||
182 | + return nil | ||
183 | + end | ||
184 | + product.enterprise | ||
185 | + end | ||
186 | + | ||
187 | + def validate_cart_presence | ||
188 | + if self.cart.nil? | ||
189 | + render :text => { | ||
190 | + :ok => false, | ||
191 | + :error => { | ||
192 | + :code => 2, | ||
193 | + :message => _("There is no basket.") | ||
194 | + } | ||
195 | + }.to_json | ||
196 | + return false | ||
197 | + end | ||
198 | + true | ||
199 | + end | ||
200 | + | ||
201 | + def find_product(id) | ||
202 | + begin | ||
203 | + product = Product.find(id) | ||
204 | + rescue ActiveRecord::RecordNotFound | ||
205 | + render :text => { | ||
206 | + :ok => false, | ||
207 | + :error => { | ||
208 | + :code => 3, | ||
209 | + :message => _("This enterprise doesn't have this product.") | ||
210 | + } | ||
211 | + }.to_json | ||
212 | + return nil | ||
213 | + end | ||
214 | + product | ||
215 | + end | ||
216 | + | ||
217 | + def validate_cart_has_product(id) | ||
218 | + if !self.cart[:items].has_key?(id) | ||
219 | + render :text => { | ||
220 | + :ok => false, | ||
221 | + :error => { | ||
222 | + :code => 4, | ||
223 | + :message => _("The basket doesn't have this product.") | ||
224 | + } | ||
225 | + }.to_json | ||
226 | + return false | ||
227 | + end | ||
228 | + true | ||
229 | + end | ||
230 | + | ||
231 | + def validate_item_quantity(quantity) | ||
232 | + if quantity.to_i < 1 | ||
233 | + render :text => { | ||
234 | + :ok => false, | ||
235 | + :error => { | ||
236 | + :code => 5, | ||
237 | + :message => _("Invalid quantity.") | ||
238 | + } | ||
239 | + }.to_json | ||
240 | + return false | ||
241 | + end | ||
242 | + true | ||
243 | + end | ||
244 | + | ||
245 | + def register_order(custumer, items) | ||
246 | + new_items = {} | ||
247 | + items.each do |id, quantity| | ||
248 | + product = Product.find(id) | ||
249 | + price = product.price || 0 | ||
250 | + new_items[id] = {:quantity => quantity, :price => price, :name => product.name} | ||
251 | + end | ||
252 | + ShoppingCartPlugin::PurchaseOrder.create!( | ||
253 | + :seller => Enterprise.find(cart[:enterprise_id]), | ||
254 | + :customer => user, | ||
255 | + :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED, | ||
256 | + :products_list => new_items, | ||
257 | + :customer_name => params[:customer][:name], | ||
258 | + :customer_email => params[:customer][:email], | ||
259 | + :customer_contact_phone => params[:customer][:contact_phone], | ||
260 | + :customer_address => params[:customer][:address], | ||
261 | + :customer_city => params[:customer][:city], | ||
262 | + :customer_zip_code => params[:customer][:zip_code] | ||
263 | + ) | ||
264 | + end | ||
265 | + | ||
266 | + protected | ||
267 | + | ||
268 | + def cart | ||
269 | + @cart ||= | ||
270 | + begin | ||
271 | + cookies[cookie_key] && YAML.load(Base64.decode64(cookies[cookie_key])) || nil | ||
272 | + end | ||
273 | + @cart | ||
274 | + end | ||
275 | + | ||
276 | + def cart=(data) | ||
277 | + @cart = data | ||
278 | + end | ||
279 | + | ||
280 | + after_filter :save_cookie | ||
281 | + def save_cookie | ||
282 | + if @cart.nil? | ||
283 | + cookies.delete(cookie_key, :path => '/plugin/shopping_cart') | ||
284 | + else | ||
285 | + cookies[cookie_key] = { | ||
286 | + :value => Base64.encode64(@cart.to_yaml), | ||
287 | + :path => "/plugin/shopping_cart" | ||
288 | + } | ||
289 | + end | ||
290 | + end | ||
291 | + | ||
292 | + def cookie_key | ||
293 | + :_noosfero_plugin_shopping_cart | ||
294 | + end | ||
295 | + | ||
296 | +end |
plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb
@@ -1,248 +0,0 @@ | @@ -1,248 +0,0 @@ | ||
1 | -include ShoppingCartPlugin::CartHelper | ||
2 | - | ||
3 | -class ShoppingCartPluginProfileController < ProfileController | ||
4 | - append_view_path File.join(File.dirname(__FILE__) + '/../views') | ||
5 | - before_filter :login_required, :only => [] | ||
6 | - | ||
7 | - before_filter :login_required, :only => [] | ||
8 | - | ||
9 | - def add | ||
10 | - session[:cart] = { :enterprise_id => profile.id, :items => {} } if session[:cart].nil? | ||
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 | ||
14 | - render :text => { | ||
15 | - :ok => true, | ||
16 | - :error => {:code => 0}, | ||
17 | - :products => [{ | ||
18 | - :id => product.id, | ||
19 | - :name => product.name, | ||
20 | - :price => get_price(product, profile.environment), | ||
21 | - :description => product.description, | ||
22 | - :picture => product.default_image(:minor), | ||
23 | - :quantity => session[:cart][:items][product.id] | ||
24 | - }] | ||
25 | - }.to_json | ||
26 | - end | ||
27 | - end | ||
28 | - | ||
29 | - def remove | ||
30 | - id = params[:id].to_i | ||
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? | ||
34 | - render :text => { | ||
35 | - :ok => true, | ||
36 | - :error => {:code => 0}, | ||
37 | - :product_id => id | ||
38 | - }.to_json | ||
39 | - end | ||
40 | - end | ||
41 | - | ||
42 | - def list | ||
43 | - if validate_cart_presence | ||
44 | - products = session[:cart][:items].collect do |id, quantity| | ||
45 | - product = Product.find(id) | ||
46 | - { :id => product.id, | ||
47 | - :name => product.name, | ||
48 | - :price => get_price(product, profile.environment), | ||
49 | - :description => product.description, | ||
50 | - :picture => product.default_image(:minor), | ||
51 | - :quantity => quantity | ||
52 | - } | ||
53 | - end | ||
54 | - render :text => { | ||
55 | - :ok => true, | ||
56 | - :error => {:code => 0}, | ||
57 | - :enterprise => Enterprise.find(session[:cart][:enterprise_id]).identifier, | ||
58 | - :products => products | ||
59 | - }.to_json | ||
60 | - end | ||
61 | - end | ||
62 | - | ||
63 | - def update_quantity | ||
64 | - quantity = params[:quantity].to_i | ||
65 | - id = params[:id].to_i | ||
66 | - if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) | ||
67 | - product = Product.find(id) | ||
68 | - session[:cart][:items][product.id] = quantity | ||
69 | - render :text => { | ||
70 | - :ok => true, | ||
71 | - :error => {:code => 0}, | ||
72 | - :product_id => id, | ||
73 | - :quantity => quantity | ||
74 | - }.to_json | ||
75 | - end | ||
76 | - end | ||
77 | - | ||
78 | - def clean | ||
79 | - session[:cart] = nil | ||
80 | - render :text => { | ||
81 | - :ok => true, | ||
82 | - :error => {:code => 0} | ||
83 | - }.to_json | ||
84 | - end | ||
85 | - | ||
86 | - def buy | ||
87 | - @environment = profile.environment | ||
88 | - render :layout => false | ||
89 | - end | ||
90 | - | ||
91 | - def send_request | ||
92 | - register_order(params[:customer], session[:cart][:items]) | ||
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]) | ||
96 | - render :text => { | ||
97 | - :ok => true, | ||
98 | - :message => _('Request sent successfully. Check your email.'), | ||
99 | - :error => {:code => 0} | ||
100 | - }.to_json | ||
101 | - rescue Exception => exception | ||
102 | - render :text => { | ||
103 | - :ok => false, | ||
104 | - :error => { | ||
105 | - :code => 6, | ||
106 | - :message => exception.message | ||
107 | - } | ||
108 | - }.to_json | ||
109 | - end | ||
110 | - end | ||
111 | - | ||
112 | - def visibility | ||
113 | - render :text => session[:cart].has_key?(:visibility) ? session[:cart][:visibility].to_json : true.to_json | ||
114 | - end | ||
115 | - | ||
116 | - def show | ||
117 | - begin | ||
118 | - session[:cart][:visibility] = true | ||
119 | - render :text => { | ||
120 | - :ok => true, | ||
121 | - :message => _('Basket displayed.'), | ||
122 | - :error => {:code => 0} | ||
123 | - }.to_json | ||
124 | - rescue Exception => exception | ||
125 | - render :text => { | ||
126 | - :ok => false, | ||
127 | - :error => { | ||
128 | - :code => 7, | ||
129 | - :message => exception.message | ||
130 | - } | ||
131 | - }.to_json | ||
132 | - end | ||
133 | - end | ||
134 | - | ||
135 | - def hide | ||
136 | - begin | ||
137 | - session[:cart][:visibility] = false | ||
138 | - render :text => { | ||
139 | - :ok => true, | ||
140 | - :message => _('Basket hidden.'), | ||
141 | - :error => {:code => 0} | ||
142 | - }.to_json | ||
143 | - rescue Exception => exception | ||
144 | - render :text => { | ||
145 | - :ok => false, | ||
146 | - :error => { | ||
147 | - :code => 8, | ||
148 | - :message => exception.message | ||
149 | - } | ||
150 | - }.to_json | ||
151 | - end | ||
152 | - end | ||
153 | - | ||
154 | - private | ||
155 | - | ||
156 | - def validate_same_enterprise | ||
157 | - if profile.id != session[:cart][:enterprise_id] | ||
158 | - render :text => { | ||
159 | - :ok => false, | ||
160 | - :error => { | ||
161 | - :code => 1, | ||
162 | - :message => _("Can't join items from different enterprises.") | ||
163 | - } | ||
164 | - }.to_json | ||
165 | - return false | ||
166 | - end | ||
167 | - true | ||
168 | - end | ||
169 | - | ||
170 | - def validate_cart_presence | ||
171 | - if session[:cart].nil? | ||
172 | - render :text => { | ||
173 | - :ok => false, | ||
174 | - :error => { | ||
175 | - :code => 2, | ||
176 | - :message => _("There is no basket.") | ||
177 | - } | ||
178 | - }.to_json | ||
179 | - return false | ||
180 | - end | ||
181 | - true | ||
182 | - end | ||
183 | - | ||
184 | - def validate_enterprise_has_product(id) | ||
185 | - begin | ||
186 | - product = profile.products.find(id) | ||
187 | - rescue | ||
188 | - render :text => { | ||
189 | - :ok => false, | ||
190 | - :error => { | ||
191 | - :code => 3, | ||
192 | - :message => _("This enterprise doesn't have this product.") | ||
193 | - } | ||
194 | - }.to_json | ||
195 | - return nil | ||
196 | - end | ||
197 | - product | ||
198 | - end | ||
199 | - | ||
200 | - def validate_cart_has_product(id) | ||
201 | - if !session[:cart][:items].has_key?(id) | ||
202 | - render :text => { | ||
203 | - :ok => false, | ||
204 | - :error => { | ||
205 | - :code => 4, | ||
206 | - :message => _("The basket doesn't have this product.") | ||
207 | - } | ||
208 | - }.to_json | ||
209 | - return false | ||
210 | - end | ||
211 | - true | ||
212 | - end | ||
213 | - | ||
214 | - def validate_item_quantity(quantity) | ||
215 | - if quantity.to_i < 1 | ||
216 | - render :text => { | ||
217 | - :ok => false, | ||
218 | - :error => { | ||
219 | - :code => 5, | ||
220 | - :message => _("Invalid quantity.") | ||
221 | - } | ||
222 | - }.to_json | ||
223 | - return false | ||
224 | - end | ||
225 | - true | ||
226 | - end | ||
227 | - | ||
228 | - def register_order(custumer, items) | ||
229 | - new_items = {} | ||
230 | - items.each do |id, quantity| | ||
231 | - product = Product.find(id) | ||
232 | - price = product.price || 0 | ||
233 | - new_items[id] = {:quantity => quantity, :price => price, :name => product.name} | ||
234 | - end | ||
235 | - ShoppingCartPlugin::PurchaseOrder.create!( | ||
236 | - :seller => profile, | ||
237 | - :customer => user, | ||
238 | - :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED, | ||
239 | - :products_list => new_items, | ||
240 | - :customer_name => params[:customer][:name], | ||
241 | - :customer_email => params[:customer][:email], | ||
242 | - :customer_contact_phone => params[:customer][:contact_phone], | ||
243 | - :customer_address => params[:customer][:address], | ||
244 | - :customer_city => params[:customer][:city], | ||
245 | - :customer_zip_code => params[:customer][:zip_code] | ||
246 | - ) | ||
247 | - end | ||
248 | -end |
plugins/shopping_cart/lib/shopping_cart_plugin.rb
@@ -11,12 +11,13 @@ class ShoppingCartPlugin < Noosfero::Plugin | @@ -11,12 +11,13 @@ class ShoppingCartPlugin < Noosfero::Plugin | ||
11 | _("A shopping basket feature for enterprises") | 11 | _("A shopping basket feature for enterprises") |
12 | end | 12 | end |
13 | 13 | ||
14 | - def add_to_cart_button(item, enterprise = context.profile) | 14 | + def add_to_cart_button(item) |
15 | + enterprise = item.enterprise | ||
15 | if enterprise.shopping_cart && item.available | 16 | if enterprise.shopping_cart && item.available |
16 | lambda { | 17 | lambda { |
17 | link_to(_('Add to basket'), "add:#{item.name}", | 18 | link_to(_('Add to basket'), "add:#{item.name}", |
18 | :class => 'cart-add-item', | 19 | :class => 'cart-add-item', |
19 | - :onclick => "Cart.addItem('#{enterprise.identifier}', #{item.id}, this); return false" | 20 | + :onclick => "Cart.addItem(#{item.id}, this); return false" |
20 | ) | 21 | ) |
21 | } | 22 | } |
22 | end | 23 | end |
@@ -35,7 +36,7 @@ class ShoppingCartPlugin < Noosfero::Plugin | @@ -35,7 +36,7 @@ class ShoppingCartPlugin < Noosfero::Plugin | ||
35 | end | 36 | end |
36 | 37 | ||
37 | def body_beginning | 38 | def body_beginning |
38 | - expanded_template('cart.html.erb',{:cart => context.session[:cart]}) | 39 | + expanded_template('cart.html.erb') |
39 | end | 40 | end |
40 | 41 | ||
41 | def control_panel_buttons | 42 | def control_panel_buttons |
plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb
1 | module ShoppingCartPlugin::CartHelper | 1 | module ShoppingCartPlugin::CartHelper |
2 | 2 | ||
3 | include ActionView::Helpers::NumberHelper | 3 | include ActionView::Helpers::NumberHelper |
4 | + include ActionView::Helpers::TagHelper | ||
4 | 5 | ||
5 | def sell_price(product) | 6 | def sell_price(product) |
6 | return 0 if product.price.nil? | 7 | return 0 if product.price.nil? |
plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb
1 | class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase | 1 | class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase |
2 | 2 | ||
3 | + include ShoppingCartPlugin::CartHelper | ||
4 | + | ||
3 | def customer_notification(customer, supplier, items) | 5 | def customer_notification(customer, supplier, items) |
4 | domain = supplier.hostname || supplier.environment.default_hostname | 6 | domain = supplier.hostname || supplier.environment.default_hostname |
5 | recipients customer[:email] | 7 | recipients customer[:email] |
@@ -10,7 +12,8 @@ class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase | @@ -10,7 +12,8 @@ class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase | ||
10 | body :customer => customer, | 12 | body :customer => customer, |
11 | :supplier => supplier, | 13 | :supplier => supplier, |
12 | :items => items, | 14 | :items => items, |
13 | - :environment => supplier.environment | 15 | + :environment => supplier.environment, |
16 | + :helper => self | ||
14 | end | 17 | end |
15 | 18 | ||
16 | def supplier_notification(customer, supplier, items) | 19 | def supplier_notification(customer, supplier, items) |
@@ -23,6 +26,8 @@ class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase | @@ -23,6 +26,8 @@ class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase | ||
23 | body :customer => customer, | 26 | body :customer => customer, |
24 | :supplier => supplier, | 27 | :supplier => supplier, |
25 | :items => items, | 28 | :items => items, |
26 | - :environment => supplier.environment | 29 | + :environment => supplier.environment, |
30 | + :helper => self | ||
31 | + | ||
27 | end | 32 | end |
28 | end | 33 | end |
plugins/shopping_cart/public/cart.js
@@ -11,10 +11,9 @@ function Cart(config) { | @@ -11,10 +11,9 @@ function Cart(config) { | ||
11 | $(".cart-buy", this.cartElem).button({ icons: { primary: 'ui-icon-cart'} }); | 11 | $(".cart-buy", this.cartElem).button({ icons: { primary: 'ui-icon-cart'} }); |
12 | if (!this.empty) { | 12 | if (!this.empty) { |
13 | $(this.cartElem).show(); | 13 | $(this.cartElem).show(); |
14 | - this.enterprise = config.enterprise; | ||
15 | me = this; | 14 | me = this; |
16 | $.ajax({ | 15 | $.ajax({ |
17 | - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/visibility', | 16 | + url: '/plugin/shopping_cart/visibility', |
18 | dataType: 'json', | 17 | dataType: 'json', |
19 | success: function(data, status, ajax){ | 18 | success: function(data, status, ajax){ |
20 | me.visible = /^true$/i.test(data); | 19 | me.visible = /^true$/i.test(data); |
@@ -25,7 +24,7 @@ function Cart(config) { | @@ -25,7 +24,7 @@ function Cart(config) { | ||
25 | alert('Visibility - HTTP '+status+': '+errorThrown); | 24 | alert('Visibility - HTTP '+status+': '+errorThrown); |
26 | } | 25 | } |
27 | }); | 26 | }); |
28 | - $(".cart-buy", this.cartElem).colorbox({href: '/profile/' + this.enterprise + '/plugin/shopping_cart/buy'}); | 27 | + $(".cart-buy", this.cartElem).colorbox({ href: '/plugin/shopping_cart/buy' }); |
29 | } | 28 | } |
30 | } | 29 | } |
31 | 30 | ||
@@ -34,7 +33,7 @@ function Cart(config) { | @@ -34,7 +33,7 @@ function Cart(config) { | ||
34 | Cart.prototype.listProducts = function() { | 33 | Cart.prototype.listProducts = function() { |
35 | var me = this; | 34 | var me = this; |
36 | $.ajax({ | 35 | $.ajax({ |
37 | - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/list', | 36 | + url: '/plugin/shopping_cart/list', |
38 | dataType: 'json', | 37 | dataType: 'json', |
39 | success: function(data, ststus, ajax){ | 38 | success: function(data, ststus, ajax){ |
40 | if ( !data.ok ) alert(data.error.message); | 39 | if ( !data.ok ) alert(data.error.message); |
@@ -61,7 +60,7 @@ function Cart(config) { | @@ -61,7 +60,7 @@ function Cart(config) { | ||
61 | '<span class="item-name">'+ item.name +'</span>' + | 60 | '<span class="item-name">'+ item.name +'</span>' + |
62 | '<div class="item-price">' + | 61 | '<div class="item-price">' + |
63 | '<input size="1" value="'+item.quantity+'" />'+ (item.price ? '× '+ item.price : '') +'</div>' + | 62 | '<input size="1" value="'+item.quantity+'" />'+ (item.price ? '× '+ item.price : '') +'</div>' + |
64 | - ' <a href="remove:'+item.name+'" onclick="Cart.removeItem(\''+this.enterprise+'\', '+item.id+'); return false"' + | 63 | + ' <a href="remove:'+item.name+'" onclick="Cart.removeItem('+item.id+'); return false"' + |
65 | ' class="button icon-remove"><span>remove</span></a>' | 64 | ' class="button icon-remove"><span>remove</span></a>' |
66 | ).appendTo(li); | 65 | ).appendTo(li); |
67 | var input = $("input", li)[0]; | 66 | var input = $("input", li)[0]; |
@@ -100,7 +99,7 @@ function Cart(config) { | @@ -100,7 +99,7 @@ function Cart(config) { | ||
100 | var me = this; | 99 | var me = this; |
101 | if( quantity == NaN ) return input.value = input.lastValue; | 100 | if( quantity == NaN ) return input.value = input.lastValue; |
102 | $.ajax({ | 101 | $.ajax({ |
103 | - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/update_quantity/'+ itemId +'?quantity='+ quantity, | 102 | + url: '/plugin/shopping_cart/update_quantity/'+ itemId +'?quantity='+ quantity, |
104 | dataType: 'json', | 103 | dataType: 'json', |
105 | success: function(data, status, ajax){ | 104 | success: function(data, status, ajax){ |
106 | if ( !data.ok ) { | 105 | if ( !data.ok ) { |
@@ -132,8 +131,7 @@ function Cart(config) { | @@ -132,8 +131,7 @@ function Cart(config) { | ||
132 | this.updateTotal(); | 131 | this.updateTotal(); |
133 | } | 132 | } |
134 | 133 | ||
135 | - Cart.addItem = function(enterprise, itemId, link) { | ||
136 | - // on the future, the instance may be found by the enterprise identifier. | 134 | + Cart.addItem = function(itemId, link) { |
137 | link.intervalId = setInterval(function() { | 135 | link.intervalId = setInterval(function() { |
138 | steps = ['w', 'n', 'e', 's']; | 136 | steps = ['w', 'n', 'e', 's']; |
139 | if( !link.step || link.step==3 ) link.step = 0; | 137 | if( !link.step || link.step==3 ) link.step = 0; |
@@ -144,18 +142,13 @@ function Cart(config) { | @@ -144,18 +142,13 @@ function Cart(config) { | ||
144 | clearInterval(link.intervalId); | 142 | clearInterval(link.intervalId); |
145 | $(link).button({ icons: { primary: 'ui-icon-cart'}, disable: false }); | 143 | $(link).button({ icons: { primary: 'ui-icon-cart'}, disable: false }); |
146 | }; | 144 | }; |
147 | - this.instance.addItem(enterprise, itemId, stopBtLoading); | 145 | + this.instance.addItem(itemId, stopBtLoading); |
148 | } | 146 | } |
149 | 147 | ||
150 | - Cart.prototype.addItem = function(enterprise, itemId, callback) { | ||
151 | - if(!this.enterprise) { | ||
152 | - this.enterprise = enterprise; | ||
153 | - $(".cart-buy", this.cartElem).colorbox({href: '/profile/' + this.enterprise + '/plugin/shopping_cart/buy'}); | ||
154 | -// $(this.cartElem).show(); | ||
155 | - } | 148 | + Cart.prototype.addItem = function(itemId, callback) { |
156 | var me = this; | 149 | var me = this; |
157 | $.ajax({ | 150 | $.ajax({ |
158 | - url: '/profile/'+ enterprise +'/plugin/shopping_cart/add/'+ itemId, | 151 | + url: '/plugin/shopping_cart/add/'+ itemId, |
159 | dataType: 'json', | 152 | dataType: 'json', |
160 | success: function(data, status, ajax){ | 153 | success: function(data, status, ajax){ |
161 | if ( !data.ok ) alert(data.error.message); | 154 | if ( !data.ok ) alert(data.error.message); |
@@ -169,16 +162,16 @@ function Cart(config) { | @@ -169,16 +162,16 @@ function Cart(config) { | ||
169 | }); | 162 | }); |
170 | } | 163 | } |
171 | 164 | ||
172 | - Cart.removeItem = function(enterprise, itemId) { | 165 | + Cart.removeItem = function(itemId) { |
173 | var message = this.instance.cartElem.getAttribute('data-l10nRemoveItem'); | 166 | var message = this.instance.cartElem.getAttribute('data-l10nRemoveItem'); |
174 | - if( confirm(message) ) this.instance.removeItem(enterprise, itemId); | 167 | + if( confirm(message) ) this.instance.removeItem(itemId); |
175 | } | 168 | } |
176 | 169 | ||
177 | - Cart.prototype.removeItem = function(enterprise, itemId) { | 170 | + Cart.prototype.removeItem = function(itemId) { |
178 | if ($("li", this.itemsBox).size() < 2) return this.clean(); | 171 | if ($("li", this.itemsBox).size() < 2) return this.clean(); |
179 | var me = this; | 172 | var me = this; |
180 | $.ajax({ | 173 | $.ajax({ |
181 | - url: '/profile/'+ enterprise +'/plugin/shopping_cart/remove/'+ itemId, | 174 | + url: '/plugin/shopping_cart/remove/'+ itemId, |
182 | dataType: 'json', | 175 | dataType: 'json', |
183 | success: function(data, status, ajax){ | 176 | success: function(data, status, ajax){ |
184 | if ( !data.ok ) alert(data.error.message); | 177 | if ( !data.ok ) alert(data.error.message); |
@@ -200,7 +193,7 @@ function Cart(config) { | @@ -200,7 +193,7 @@ function Cart(config) { | ||
200 | 193 | ||
201 | Cart.prototype.show = function() { | 194 | Cart.prototype.show = function() { |
202 | $.ajax({ | 195 | $.ajax({ |
203 | - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/show', | 196 | + url: '/plugin/shopping_cart/show', |
204 | dataType: 'json', | 197 | dataType: 'json', |
205 | cache: false, | 198 | cache: false, |
206 | error: function(ajax, status, errorThrown) { | 199 | error: function(ajax, status, errorThrown) { |
@@ -215,7 +208,7 @@ function Cart(config) { | @@ -215,7 +208,7 @@ function Cart(config) { | ||
215 | } | 208 | } |
216 | Cart.prototype.hide = function() { | 209 | Cart.prototype.hide = function() { |
217 | $.ajax({ | 210 | $.ajax({ |
218 | - url: '/profile/'+ this.enterprise +'/plugin/shopping_cart/hide', | 211 | + url: '/plugin/shopping_cart/hide', |
219 | dataType: 'json', | 212 | dataType: 'json', |
220 | cache: false, | 213 | cache: false, |
221 | error: function(ajax, status, errorThrown) { | 214 | error: function(ajax, status, errorThrown) { |
@@ -252,7 +245,7 @@ function Cart(config) { | @@ -252,7 +245,7 @@ function Cart(config) { | ||
252 | Cart.prototype.clean = function() { | 245 | Cart.prototype.clean = function() { |
253 | var me = this; | 246 | var me = this; |
254 | $.ajax({ | 247 | $.ajax({ |
255 | - url: '/profile/'+ me.enterprise +'/plugin/shopping_cart/clean', | 248 | + url: '/plugin/shopping_cart/clean', |
256 | dataType: 'json', | 249 | dataType: 'json', |
257 | success: function(data, status, ajax){ | 250 | success: function(data, status, ajax){ |
258 | if ( !data.ok ) alert(data.error.message); | 251 | if ( !data.ok ) alert(data.error.message); |
@@ -261,7 +254,6 @@ function Cart(config) { | @@ -261,7 +254,6 @@ function Cart(config) { | ||
261 | $(me.cartElem).slideUp(500, function() { | 254 | $(me.cartElem).slideUp(500, function() { |
262 | $(me.itemsBox).empty(); | 255 | $(me.itemsBox).empty(); |
263 | me.hide(); | 256 | me.hide(); |
264 | - me.enterprise = null; | ||
265 | me.updateTotal(); | 257 | me.updateTotal(); |
266 | me.empty = true; | 258 | me.empty = true; |
267 | }); | 259 | }); |
@@ -284,7 +276,7 @@ function Cart(config) { | @@ -284,7 +276,7 @@ function Cart(config) { | ||
284 | var me = this; | 276 | var me = this; |
285 | $.ajax({ | 277 | $.ajax({ |
286 | type: 'POST', | 278 | type: 'POST', |
287 | - url: '/profile/'+ me.enterprise +'/plugin/shopping_cart/send_request', | 279 | + url: '/plugin/shopping_cart/send_request', |
288 | data: params, | 280 | data: params, |
289 | dataType: 'json', | 281 | dataType: 'json', |
290 | success: function(data, status, ajax){ | 282 | success: function(data, status, ajax){ |
@@ -309,7 +301,19 @@ function Cart(config) { | @@ -309,7 +301,19 @@ function Cart(config) { | ||
309 | } | 301 | } |
310 | 302 | ||
311 | $(function(){ | 303 | $(function(){ |
312 | - $('.cart-add-item').button({ icons: { primary: 'ui-icon-cart'} }) | 304 | + |
305 | + $.ajax({ | ||
306 | + url: "/plugin/shopping_cart/get", | ||
307 | + dataType: 'json', | ||
308 | + success: function(data) { | ||
309 | + new Cart(data); | ||
310 | + $('.cart-add-item').button({ icons: { primary: 'ui-icon-cart'} }) | ||
311 | + }, | ||
312 | + cache: false, | ||
313 | + error: function(ajax, status, errorThrown) { | ||
314 | + alert('Error getting shopping cart - HTTP '+status+': '+errorThrown); | ||
315 | + } | ||
316 | + }); | ||
313 | }); | 317 | }); |
314 | 318 | ||
315 | })(jQuery); | 319 | })(jQuery); |
plugins/shopping_cart/public/style.css
1 | -@import url(colorbox/colorbox.css); | ||
2 | - | ||
3 | .cart-add-item .ui-icon-cart { | 1 | .cart-add-item .ui-icon-cart { |
4 | background: url("/plugins/shopping_cart/images/button-icon.png") no-repeat scroll left center transparent; | 2 | background: url("/plugins/shopping_cart/images/button-icon.png") no-repeat scroll left center transparent; |
5 | width: 22px; | 3 | width: 22px; |
plugins/shopping_cart/test/functional/shopping_cart_plugin_controller_test.rb
0 → 100644
@@ -0,0 +1,225 @@ | @@ -0,0 +1,225 @@ | ||
1 | +require File.dirname(__FILE__) + '/../../../../test/test_helper' | ||
2 | +require File.dirname(__FILE__) + '/../../controllers/shopping_cart_plugin_controller' | ||
3 | + | ||
4 | +# Re-raise errors caught by the controller. | ||
5 | +class ShoppingCartPluginController; def rescue_action(e) raise e end; end | ||
6 | + | ||
7 | +class ShoppingCartPluginControllerTest < ActionController::TestCase | ||
8 | + | ||
9 | + def setup | ||
10 | + @controller = ShoppingCartPluginController.new | ||
11 | + @request = ActionController::TestRequest.new | ||
12 | + @response = ActionController::TestResponse.new | ||
13 | + @enterprise = fast_create(Enterprise) | ||
14 | + @product = fast_create(Product, :enterprise_id => @enterprise.id) | ||
15 | + end | ||
16 | + attr_reader :enterprise | ||
17 | + attr_reader :product | ||
18 | + | ||
19 | + should 'force cookie expiration with explicit path for an empty cart' do | ||
20 | + get :get | ||
21 | + assert @response.headers['Set-Cookie'].any? { |c| c =~ /_noosfero_plugin_shopping_cart=; path=\/plugin\/shopping_cart; expires=.*-1970/} | ||
22 | + end | ||
23 | + | ||
24 | + should 'add a new product to cart' do | ||
25 | + get :add, :id => product.id | ||
26 | + | ||
27 | + assert product_in_cart?(product) | ||
28 | + assert_equal 1, product_quantity(product) | ||
29 | + end | ||
30 | + | ||
31 | + should 'grow quantity through add' do | ||
32 | + get :add, :id => product.id | ||
33 | + assert_equal 1, product_quantity(product) | ||
34 | + | ||
35 | + get :add, :id => product.id | ||
36 | + assert_equal 2, product_quantity(product) | ||
37 | + end | ||
38 | + | ||
39 | + should 'not add product to cart if it does not exists' do | ||
40 | + assert_nothing_raised { get :add, :id => 9999 } | ||
41 | + | ||
42 | + assert !product_in_cart?(product) | ||
43 | + assert !response_ok? | ||
44 | + assert 3, reponse_error_code | ||
45 | + end | ||
46 | + | ||
47 | + should 'remove cart if the product being removed is the last one' do | ||
48 | + get :add, :id => product.id | ||
49 | + assert cart? | ||
50 | + | ||
51 | + get :remove, :id => product.id | ||
52 | + assert !cart? | ||
53 | + end | ||
54 | + | ||
55 | + should 'not try to remove a product if there is no cart' do | ||
56 | + instantiate_cart | ||
57 | + assert !cart? | ||
58 | + | ||
59 | + assert_nothing_raised { get :remove, :id => 9999 } | ||
60 | + assert !response_ok? | ||
61 | + assert_equal 2, reponse_error_code | ||
62 | + end | ||
63 | + | ||
64 | + should 'just remove product if there are other products on cart' do | ||
65 | + another_product = fast_create(Product, :enterprise_id => enterprise.id) | ||
66 | + get :add, :id => product.id | ||
67 | + get :add, :id => another_product.id | ||
68 | + | ||
69 | + get :remove, :id => product.id | ||
70 | + assert cart? | ||
71 | + assert !product_in_cart?(product) | ||
72 | + end | ||
73 | + | ||
74 | + should 'not try to remove a product that is not in the cart' do | ||
75 | + get :add, :id => product.id | ||
76 | + assert cart? | ||
77 | + assert_nothing_raised { get :remove, :id => 9999 } | ||
78 | + | ||
79 | + assert !response_ok? | ||
80 | + assert_equal 4, reponse_error_code | ||
81 | + end | ||
82 | + | ||
83 | + should 'not try to list the cart if there is no cart' do | ||
84 | + instantiate_cart | ||
85 | + assert !cart? | ||
86 | + | ||
87 | + assert_nothing_raised { get :list } | ||
88 | + assert !response_ok? | ||
89 | + assert_equal 2, reponse_error_code | ||
90 | + end | ||
91 | + | ||
92 | + should 'list products without errors' do | ||
93 | + get :add, :id => product.id | ||
94 | + | ||
95 | + assert_nothing_raised { get :list } | ||
96 | + assert response_ok? | ||
97 | + end | ||
98 | + | ||
99 | + should 'update the quantity of a product' do | ||
100 | + get :add, :id => product.id | ||
101 | + assert 1, product_quantity(product) | ||
102 | + | ||
103 | + get :update_quantity, :id => product.id, :quantity => 3 | ||
104 | + assert 3, product_quantity(product) | ||
105 | + end | ||
106 | + | ||
107 | + should 'not try to update quantity the quantity of a product if there is no cart' do | ||
108 | + instantiate_cart | ||
109 | + assert !cart? | ||
110 | + | ||
111 | + assert_nothing_raised { get :update_quantity, :id => 9999, :quantity => 3 } | ||
112 | + assert !response_ok? | ||
113 | + assert_equal 2, reponse_error_code | ||
114 | + end | ||
115 | + | ||
116 | + should 'not try to update the quantity of a product that is not in the cart' do | ||
117 | + get :add, :id => product.id | ||
118 | + assert cart? | ||
119 | + assert_nothing_raised { get :update_quantity, :id => 9999, :quantity => 3 } | ||
120 | + | ||
121 | + assert !response_ok? | ||
122 | + assert_equal 4, reponse_error_code | ||
123 | + end | ||
124 | + | ||
125 | + should 'not update the quantity of a product with a invalid value' do | ||
126 | + get :add, :id => product.id | ||
127 | + | ||
128 | + assert_nothing_raised { get :update_quantity, :id => product.id, :quantity => -1} | ||
129 | + assert !response_ok? | ||
130 | + assert_equal 5, reponse_error_code | ||
131 | + | ||
132 | + assert_nothing_raised { get :update_quantity, :id => product.id, :quantity => 'asdf'} | ||
133 | + assert !response_ok? | ||
134 | + assert_equal 5, reponse_error_code | ||
135 | + end | ||
136 | + | ||
137 | + should 'clean the cart' do | ||
138 | + another_product = fast_create(Product, :enterprise_id => enterprise.id) | ||
139 | + get :add, :id => product.id | ||
140 | + get :add, :id => another_product.id | ||
141 | + | ||
142 | + assert_nothing_raised { get :clean } | ||
143 | + assert !cart? | ||
144 | + end | ||
145 | + | ||
146 | + should 'not crash if there is no cart' do | ||
147 | + instantiate_cart | ||
148 | + assert !cart? | ||
149 | + assert_nothing_raised { get :clean } | ||
150 | + end | ||
151 | + | ||
152 | + should 'register order on send request' do | ||
153 | + product1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1.99) | ||
154 | + product2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2.23) | ||
155 | + @controller.stubs(:cart).returns({ :enterprise_id => enterprise.id, :items => {product1.id => 1, product2.id => 2}}) | ||
156 | + assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do | ||
157 | + post :send_request, | ||
158 | + :customer => {:name => "Manuel", :email => "manuel@ceu.com"} | ||
159 | + end | ||
160 | + | ||
161 | + order = ShoppingCartPlugin::PurchaseOrder.last | ||
162 | + | ||
163 | + assert_equal 1.99, order.products_list[product1.id][:price] | ||
164 | + assert_equal 1, order.products_list[product1.id][:quantity] | ||
165 | + assert_equal 2.23, order.products_list[product2.id][:price] | ||
166 | + assert_equal 2, order.products_list[product2.id][:quantity] | ||
167 | + assert_equal ShoppingCartPlugin::PurchaseOrder::Status::OPENED, order.status | ||
168 | + end | ||
169 | + | ||
170 | + should 'register order on send request and not crash if product is not defined' do | ||
171 | + product1 = fast_create(Product, :enterprise_id => enterprise.id) | ||
172 | + @controller.stubs(:cart).returns({ :enterprise_id => enterprise.id, :items => {product1.id => 1}}) | ||
173 | + assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do | ||
174 | + post :send_request, | ||
175 | + :customer => {:name => "Manuel", :email => "manuel@ceu.com"} | ||
176 | + end | ||
177 | + | ||
178 | + order = ShoppingCartPlugin::PurchaseOrder.last | ||
179 | + | ||
180 | + assert_equal 0, order.products_list[product1.id][:price] | ||
181 | + end | ||
182 | + | ||
183 | + should 'clean the cart after placing the order' do | ||
184 | + product1 = fast_create(Product, :enterprise_id => enterprise.id) | ||
185 | + post :add, :id => product1.id | ||
186 | + post :send_request, :customer => { :name => "Manuel", :email => "manuel@ceu.com" } | ||
187 | + assert !cart?, "cart expected to be empty!" | ||
188 | + end | ||
189 | + | ||
190 | + private | ||
191 | + | ||
192 | + def json_response | ||
193 | + ActiveSupport::JSON.decode @response.body | ||
194 | + end | ||
195 | + | ||
196 | + def cart? | ||
197 | + !@controller.send(:cart).nil? | ||
198 | + end | ||
199 | + | ||
200 | + def product_in_cart?(product) | ||
201 | + @controller.send(:cart) && | ||
202 | + @controller.send(:cart)[:items] && | ||
203 | + @controller.send(:cart)[:items].has_key?(product.id) | ||
204 | + end | ||
205 | + | ||
206 | + def product_quantity(product) | ||
207 | + @controller.send(:cart)[:items][product.id] | ||
208 | + end | ||
209 | + | ||
210 | + def response_ok? | ||
211 | + json_response['ok'] | ||
212 | + end | ||
213 | + | ||
214 | + def reponse_error_code | ||
215 | + json_response['error']['code'] | ||
216 | + end | ||
217 | + | ||
218 | + # temporary hack...if I don't do this the session stays as an Array instead | ||
219 | + # of a TestSession | ||
220 | + def instantiate_cart | ||
221 | + get :add, :id => product.id | ||
222 | + get :remove, :id => product.id | ||
223 | + end | ||
224 | + | ||
225 | +end |
plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb
@@ -1,213 +0,0 @@ | @@ -1,213 +0,0 @@ | ||
1 | -require File.dirname(__FILE__) + '/../../../../test/test_helper' | ||
2 | -require File.dirname(__FILE__) + '/../../controllers/shopping_cart_plugin_profile_controller' | ||
3 | - | ||
4 | -# Re-raise errors caught by the controller. | ||
5 | -class ShoppingCartPluginProfileController; def rescue_action(e) raise e end; end | ||
6 | - | ||
7 | -class ShoppingCartPluginProfileControllerTest < ActionController::TestCase | ||
8 | - | ||
9 | - def setup | ||
10 | - @controller = ShoppingCartPluginProfileController.new | ||
11 | - @request = ActionController::TestRequest.new | ||
12 | - @response = ActionController::TestResponse.new | ||
13 | - @enterprise = fast_create(Enterprise) | ||
14 | - @product = fast_create(Product, :enterprise_id => @enterprise.id) | ||
15 | - end | ||
16 | - attr_reader :enterprise | ||
17 | - attr_reader :product | ||
18 | - | ||
19 | - should 'add a new product to cart' do | ||
20 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
21 | - | ||
22 | - assert product_in_cart?(product) | ||
23 | - assert_equal 1, product_quantity(product) | ||
24 | - end | ||
25 | - | ||
26 | - should 'grow quantity through add' do | ||
27 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
28 | - assert_equal 1, product_quantity(product) | ||
29 | - | ||
30 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
31 | - assert_equal 2, product_quantity(product) | ||
32 | - end | ||
33 | - | ||
34 | - should 'not add product to cart if it does not exists' do | ||
35 | - assert_nothing_raised { get :add, :profile => enterprise.identifier, :id => 9999 } | ||
36 | - | ||
37 | - assert !product_in_cart?(product) | ||
38 | - assert !response_ok? | ||
39 | - assert 3, reponse_error_code | ||
40 | - end | ||
41 | - | ||
42 | - should 'remove cart if the product being removed is the last one' do | ||
43 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
44 | - assert cart? | ||
45 | - | ||
46 | - get :remove, :profile => enterprise.identifier, :id => product.id | ||
47 | - assert !cart? | ||
48 | - end | ||
49 | - | ||
50 | - should 'not try to remove a product if there is no cart' do | ||
51 | - instantiate_session | ||
52 | - assert !cart? | ||
53 | - | ||
54 | - assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } | ||
55 | - assert !response_ok? | ||
56 | - assert_equal 2, reponse_error_code | ||
57 | - end | ||
58 | - | ||
59 | - should 'just remove product if there are other products on cart' do | ||
60 | - another_product = fast_create(Product, :enterprise_id => enterprise.id) | ||
61 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
62 | - get :add, :profile => enterprise.identifier, :id => another_product.id | ||
63 | - | ||
64 | - get :remove, :profile => enterprise.identifier, :id => product.id | ||
65 | - assert cart? | ||
66 | - assert !product_in_cart?(product) | ||
67 | - end | ||
68 | - | ||
69 | - should 'not try to remove a product that is not in the cart' do | ||
70 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
71 | - assert cart? | ||
72 | - assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } | ||
73 | - | ||
74 | - assert !response_ok? | ||
75 | - assert_equal 4, reponse_error_code | ||
76 | - end | ||
77 | - | ||
78 | - should 'not try to list the cart if there is no cart' do | ||
79 | - instantiate_session | ||
80 | - assert !cart? | ||
81 | - | ||
82 | - assert_nothing_raised { get :list, :profile => enterprise.identifier } | ||
83 | - assert !response_ok? | ||
84 | - assert_equal 2, reponse_error_code | ||
85 | - end | ||
86 | - | ||
87 | - should 'list products without errors' do | ||
88 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
89 | - | ||
90 | - assert_nothing_raised { get :list, :profile => enterprise.identifier } | ||
91 | - assert response_ok? | ||
92 | - end | ||
93 | - | ||
94 | - should 'update the quantity of a product' do | ||
95 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
96 | - assert 1, product_quantity(product) | ||
97 | - | ||
98 | - get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 3 | ||
99 | - assert 3, product_quantity(product) | ||
100 | - end | ||
101 | - | ||
102 | - should 'not try to update quantity the quantity of a product if there is no cart' do | ||
103 | - instantiate_session | ||
104 | - assert !cart? | ||
105 | - | ||
106 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } | ||
107 | - assert !response_ok? | ||
108 | - assert_equal 2, reponse_error_code | ||
109 | - end | ||
110 | - | ||
111 | - should 'not try to update the quantity of a product that is not in the cart' do | ||
112 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
113 | - assert cart? | ||
114 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } | ||
115 | - | ||
116 | - assert !response_ok? | ||
117 | - assert_equal 4, reponse_error_code | ||
118 | - end | ||
119 | - | ||
120 | - should 'not update the quantity of a product with a invalid value' do | ||
121 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
122 | - | ||
123 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => -1} | ||
124 | - assert !response_ok? | ||
125 | - assert_equal 5, reponse_error_code | ||
126 | - | ||
127 | - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 'asdf'} | ||
128 | - assert !response_ok? | ||
129 | - assert_equal 5, reponse_error_code | ||
130 | - end | ||
131 | - | ||
132 | - should 'clean the cart' do | ||
133 | - another_product = fast_create(Product, :enterprise_id => enterprise.id) | ||
134 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
135 | - get :add, :profile => enterprise.identifier, :id => another_product.id | ||
136 | - | ||
137 | - assert_nothing_raised { get :clean, :profile => enterprise.identifier } | ||
138 | - assert !cart? | ||
139 | - end | ||
140 | - | ||
141 | - should 'not crash if there is no cart' do | ||
142 | - instantiate_session | ||
143 | - assert !cart? | ||
144 | - assert_nothing_raised { get :clean, :profile => enterprise.identifier } | ||
145 | - end | ||
146 | - | ||
147 | - should 'register order on send request' do | ||
148 | - product1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1.99) | ||
149 | - product2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2.23) | ||
150 | - @controller.stubs(:session).returns({:cart => {:items => {product1.id => 1, product2.id => 2}}}) | ||
151 | - assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do | ||
152 | - post :send_request, | ||
153 | - :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, | ||
154 | - :profile => enterprise.identifier | ||
155 | - end | ||
156 | - | ||
157 | - order = ShoppingCartPlugin::PurchaseOrder.last | ||
158 | - | ||
159 | - assert_equal 1.99, order.products_list[product1.id][:price] | ||
160 | - assert_equal 1, order.products_list[product1.id][:quantity] | ||
161 | - assert_equal 2.23, order.products_list[product2.id][:price] | ||
162 | - assert_equal 2, order.products_list[product2.id][:quantity] | ||
163 | - assert_equal ShoppingCartPlugin::PurchaseOrder::Status::OPENED, order.status | ||
164 | - end | ||
165 | - | ||
166 | - should 'register order on send request and not crash if product is not defined' do | ||
167 | - product1 = fast_create(Product, :enterprise_id => enterprise.id) | ||
168 | - @controller.stubs(:session).returns({:cart => {:items => {product1.id => 1}}}) | ||
169 | - assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do | ||
170 | - post :send_request, | ||
171 | - :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, | ||
172 | - :profile => enterprise.identifier | ||
173 | - end | ||
174 | - | ||
175 | - order = ShoppingCartPlugin::PurchaseOrder.last | ||
176 | - | ||
177 | - assert_equal 0, order.products_list[product1.id][:price] | ||
178 | - end | ||
179 | - | ||
180 | - private | ||
181 | - | ||
182 | - def json_response | ||
183 | - ActiveSupport::JSON.decode @response.body | ||
184 | - end | ||
185 | - | ||
186 | - def cart? | ||
187 | - !session[:cart].nil? | ||
188 | - end | ||
189 | - | ||
190 | - def product_in_cart?(product) | ||
191 | - session[:cart][:items].has_key?(product.id) | ||
192 | - end | ||
193 | - | ||
194 | - def product_quantity(product) | ||
195 | - session[:cart][:items][product.id] | ||
196 | - end | ||
197 | - | ||
198 | - def response_ok? | ||
199 | - json_response['ok'] | ||
200 | - end | ||
201 | - | ||
202 | - def reponse_error_code | ||
203 | - json_response['error']['code'] | ||
204 | - end | ||
205 | - | ||
206 | - # temporary hack...if I don't do this the session stays as an Array instead | ||
207 | - # of a TestSession | ||
208 | - def instantiate_session | ||
209 | - get :add, :profile => enterprise.identifier, :id => product.id | ||
210 | - get :remove, :profile => enterprise.identifier, :id => product.id | ||
211 | - end | ||
212 | - | ||
213 | -end |
plugins/shopping_cart/test/unit/shopping_cart_plugin_test.rb
@@ -7,7 +7,6 @@ class ShoppingCartPluginTest < ActiveSupport::TestCase | @@ -7,7 +7,6 @@ class ShoppingCartPluginTest < ActiveSupport::TestCase | ||
7 | @context = mock() | 7 | @context = mock() |
8 | @profile = mock() | 8 | @profile = mock() |
9 | @profile.stubs(:identifier).returns('random-user') | 9 | @profile.stubs(:identifier).returns('random-user') |
10 | - @context.stubs(:profile).returns(@profile) | ||
11 | @shopping_cart.context = @context | 10 | @shopping_cart.context = @context |
12 | @shopping_cart.stubs(:profile).returns(@profile) | 11 | @shopping_cart.stubs(:profile).returns(@profile) |
13 | end | 12 | end |
@@ -23,7 +22,8 @@ class ShoppingCartPluginTest < ActiveSupport::TestCase | @@ -23,7 +22,8 @@ class ShoppingCartPluginTest < ActiveSupport::TestCase | ||
23 | product = fast_create(Product, :available => false) | 22 | product = fast_create(Product, :available => false) |
24 | enterprise = mock() | 23 | enterprise = mock() |
25 | enterprise.stubs(:shopping_cart).returns(true) | 24 | enterprise.stubs(:shopping_cart).returns(true) |
25 | + product.stubs(:enterprise).returns(enterprise) | ||
26 | 26 | ||
27 | - assert_nil shopping_cart.add_to_cart_button(product, enterprise) | 27 | + assert_nil shopping_cart.add_to_cart_button(product) |
28 | end | 28 | end |
29 | end | 29 | end |
plugins/shopping_cart/views/cart.html.erb
@@ -7,7 +7,7 @@ | @@ -7,7 +7,7 @@ | ||
7 | <a href="cart:clean" onclick="Cart.clean(this); return false" class="cart-clean"><%=_('Clean basket')%></a> | 7 | <a href="cart:clean" onclick="Cart.clean(this); return false" class="cart-clean"><%=_('Clean basket')%></a> |
8 | <ul class="cart-items"></ul> | 8 | <ul class="cart-items"></ul> |
9 | <div class="cart-total"><%=_('Total:')%> <b></b></div> | 9 | <div class="cart-total"><%=_('Total:')%> <b></b></div> |
10 | - <a href="cart:buy" class="cart-buy"><%=_('Shopping checkout')%></a> | 10 | + <a href="/plugin/shopping_cart/buy" class="cart-buy"><%=_('Shopping checkout')%></a> |
11 | </div> | 11 | </div> |
12 | <a href="#" onclick="Cart.toggle(this); return false" class="cart-toggle"> | 12 | <a href="#" onclick="Cart.toggle(this); return false" class="cart-toggle"> |
13 | <span class="str-show"><%=_('Show basket')%></span> | 13 | <span class="str-show"><%=_('Show basket')%></span> |
@@ -15,9 +15,3 @@ | @@ -15,9 +15,3 @@ | ||
15 | </a> | 15 | </a> |
16 | </div> | 16 | </div> |
17 | </div> | 17 | </div> |
18 | - | ||
19 | -<script type="text/javascript"> | ||
20 | -//<![CDATA[ | ||
21 | - new Cart({hasProducts:<%= !locals[:cart].nil? ? "true, enterprise:'#{Enterprise.find(locals[:cart][:enterprise_id]).identifier}'" : "false" %>}); | ||
22 | -//]]> | ||
23 | -</script> |
plugins/shopping_cart/views/shopping_cart_plugin/buy.html.erb
0 → 100644
@@ -0,0 +1,35 @@ | @@ -0,0 +1,35 @@ | ||
1 | +<% person = user.nil? ? Person.new : user %> | ||
2 | +<div id='cart-request-box'> | ||
3 | + <% form_for(:customer, person, :url => {:action => 'send_request'}, | ||
4 | + :html => {:onsubmit => "return Cart.send_request(this)", :id => 'cart-request-form' }) do |f| %> | ||
5 | + <div id="cart-form-main"> | ||
6 | + <%= labelled_form_field('* ' + _("Name"), f.text_field(:name, :class => 'required') ) %> | ||
7 | + <%= labelled_form_field('* ' + _("Email"), f.text_field(:email, :class => 'required email') ) %> | ||
8 | + <%= labelled_form_field('* ' + _("Contact phone"), f.text_field(:contact_phone, :class => 'required') ) %> | ||
9 | + </div> | ||
10 | + <fieldset><legend><%=_('Delivery Address')%></legend> | ||
11 | + <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %> | ||
12 | + <%= labelled_form_field( _("City"), f.text_field(:city)) %> | ||
13 | + <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %> | ||
14 | + </fieldset> | ||
15 | + <div id="cart-form-actions"> | ||
16 | + <%= submit_button(:send, _('Send buy request')) %> | ||
17 | + </div> | ||
18 | + <% end %> | ||
19 | + <%= items_table(@cart[:items], @enterprise) %> | ||
20 | + <%= link_to '', '#', :onclick => "Cart.colorbox_close(this);", :class => 'cart-box-close icon-cancel' %> | ||
21 | +</div> | ||
22 | + | ||
23 | +<script type="text/javascript"> | ||
24 | +//<![CDATA[ | ||
25 | + jQuery(document).ready(function(){ | ||
26 | + jQuery("#cart-request-form").validate({ | ||
27 | + submitHandler: function(form) { | ||
28 | + jQuery(form).find('input.submit').attr('disabled', true); | ||
29 | + jQuery('#cboxLoadingOverlay').show().addClass('loading'); | ||
30 | + jQuery('#cboxLoadingGraphic').show().addClass('loading'); | ||
31 | + } | ||
32 | + }); | ||
33 | + }); | ||
34 | +//]]> | ||
35 | +</script> |
plugins/shopping_cart/views/shopping_cart_plugin/mailer/customer_notification.html.erb
@@ -35,7 +35,7 @@ | @@ -35,7 +35,7 @@ | ||
35 | </ul> | 35 | </ul> |
36 | 36 | ||
37 | <p><%=_('Here are the products you bought:')%></p> | 37 | <p><%=_('Here are the products you bought:')%></p> |
38 | - <%= items_table(@items, @supplier, true) %> | 38 | + <%= @helper.items_table(@items, @supplier, true) %> |
39 | 39 | ||
40 | <p> | 40 | <p> |
41 | --<br/> | 41 | --<br/> |
plugins/shopping_cart/views/shopping_cart_plugin/mailer/supplier_notification.html.erb
@@ -33,7 +33,7 @@ | @@ -33,7 +33,7 @@ | ||
33 | </ul> | 33 | </ul> |
34 | 34 | ||
35 | <p><%=_('And here are the items bought by this customer:')%></p> | 35 | <p><%=_('And here are the items bought by this customer:')%></p> |
36 | - <%= items_table(@items, @supplier, true) %> | 36 | + <%= @helper.items_table(@items, @supplier, true) %> |
37 | 37 | ||
38 | <p> | 38 | <p> |
39 | --<br/> | 39 | --<br/> |
plugins/shopping_cart/views/shopping_cart_plugin/send_request.html.erb
0 → 100644
@@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
1 | +<%= _("Request sent successfully check your email.")%> |
plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb
@@ -1,35 +0,0 @@ | @@ -1,35 +0,0 @@ | ||
1 | -<% person = user.nil? ? Person.new : user %> | ||
2 | -<div id='cart-request-box'> | ||
3 | - <% form_for(:customer, person, :url => {:action => 'send_request'}, | ||
4 | - :html => {:onsubmit => "return Cart.send_request(this)", :id => 'cart-request-form' }) do |f| %> | ||
5 | - <div id="cart-form-main"> | ||
6 | - <%= labelled_form_field('* ' + _("Name"), f.text_field(:name, :class => 'required') ) %> | ||
7 | - <%= labelled_form_field('* ' + _("Email"), f.text_field(:email, :class => 'required email') ) %> | ||
8 | - <%= labelled_form_field('* ' + _("Contact phone"), f.text_field(:contact_phone, :class => 'required') ) %> | ||
9 | - </div> | ||
10 | - <fieldset><legend><%=_('Delivery Address')%></legend> | ||
11 | - <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %> | ||
12 | - <%= labelled_form_field( _("City"), f.text_field(:city)) %> | ||
13 | - <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %> | ||
14 | - </fieldset> | ||
15 | - <div id="cart-form-actions"> | ||
16 | - <%= submit_button(:send, _('Send buy request')) %> | ||
17 | - </div> | ||
18 | - <% end %> | ||
19 | - <%= items_table(session[:cart][:items], profile) %> | ||
20 | - <%= link_to '', '#', :onclick => "Cart.colorbox_close(this);", :class => 'cart-box-close icon-cancel' %> | ||
21 | -</div> | ||
22 | - | ||
23 | -<script type="text/javascript"> | ||
24 | -//<![CDATA[ | ||
25 | - jQuery(document).ready(function(){ | ||
26 | - jQuery("#cart-request-form").validate({ | ||
27 | - submitHandler: function(form) { | ||
28 | - jQuery(form).find('input.submit').attr('disabled', true); | ||
29 | - jQuery('#cboxLoadingOverlay').show().addClass('loading'); | ||
30 | - jQuery('#cboxLoadingGraphic').show().addClass('loading'); | ||
31 | - } | ||
32 | - }); | ||
33 | - }); | ||
34 | -//]]> | ||
35 | -</script> |
plugins/shopping_cart/views/shopping_cart_plugin_profile/send_request.html.erb
@@ -1 +0,0 @@ | @@ -1 +0,0 @@ | ||
1 | -<%= _("Request sent successfully check your email.")%> |
vendor/plugins/noosfero_caching/init.rb
@@ -38,11 +38,23 @@ module NoosferoHttpCaching | @@ -38,11 +38,23 @@ module NoosferoHttpCaching | ||
38 | def call(env) | 38 | def call(env) |
39 | status, headers, body = @app.call(env) | 39 | status, headers, body = @app.call(env) |
40 | if headers['X-Noosfero-Auth'] == 'false' | 40 | if headers['X-Noosfero-Auth'] == 'false' |
41 | - headers.delete('Set-Cookie') | 41 | + headers['Set-Cookie'] = remove_unwanted_cookies(headers['Set-Cookie']) |
42 | end | 42 | end |
43 | headers.delete('X-Noosfero-Auth') | 43 | headers.delete('X-Noosfero-Auth') |
44 | [status, headers, body] | 44 | [status, headers, body] |
45 | end | 45 | end |
46 | + | ||
47 | + protected | ||
48 | + | ||
49 | + # filter off all cookies except for plugin-provided ones that are | ||
50 | + # path-specific (i.e path != "/"). | ||
51 | + def remove_unwanted_cookies(cookie_list) | ||
52 | + return nil if cookie_list.nil? | ||
53 | + cookie_list.select do |c| | ||
54 | + c =~ /^_noosfero_plugin_\w+=/ && c =~ /path=\/\w+/ | ||
55 | + end | ||
56 | + end | ||
57 | + | ||
46 | end | 58 | end |
47 | 59 | ||
48 | end | 60 | end |