Commit dbf169eeeaada60f4da06e4c4fcd0144298631ad
1 parent
93103d85
Exists in
master
and in
22 other branches
Fixes crash in shopping cart reports when product is removed
Changes what's stored in PurchaseOrder so the product name is also stored, that way if the product is removed its name isn't lost. Uses a class named LineItem to encapsulate the information about an ordered product for clarification. Also fixes the views so it shows the product name but not the product url if the product was removed. (ActionItem2362)
Showing
6 changed files
with
45 additions
and
15 deletions
Show diff stats
plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb
@@ -33,9 +33,9 @@ class ShoppingCartPluginMyprofileController < MyProfileController | @@ -33,9 +33,9 @@ class ShoppingCartPluginMyprofileController < MyProfileController | ||
33 | 33 | ||
34 | @products = {} | 34 | @products = {} |
35 | @orders.each do |order| | 35 | @orders.each do |order| |
36 | - order.products_list.each do |id, qp| | ||
37 | - @products[id] ||= 0 | ||
38 | - @products[id] += qp[:quantity] | 36 | + order.products_list.each do |id, qp| |
37 | + @products[id] ||= ShoppingCartPlugin::LineItem.new(id, qp[:name]) | ||
38 | + @products[id].quantity += qp[:quantity] | ||
39 | end | 39 | end |
40 | end | 40 | end |
41 | end | 41 | end |
plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb
@@ -228,7 +228,8 @@ class ShoppingCartPluginProfileController < ProfileController | @@ -228,7 +228,8 @@ class ShoppingCartPluginProfileController < ProfileController | ||
228 | def register_order(custumer, items) | 228 | def register_order(custumer, items) |
229 | new_items = {} | 229 | new_items = {} |
230 | items.each do |id, quantity| | 230 | items.each do |id, quantity| |
231 | - new_items[id] = {:quantity => quantity, :price => Product.find(id).price} | 231 | + product = Product.find(id) |
232 | + new_items[id] = {:quantity => quantity, :price => product.price, :name => product.name} | ||
232 | end | 233 | end |
233 | ShoppingCartPlugin::PurchaseOrder.create!( | 234 | ShoppingCartPlugin::PurchaseOrder.create!( |
234 | :seller => profile, | 235 | :seller => profile, |
plugins/shopping_cart/lib/shopping_cart_plugin/line_item.rb
0 → 100644
@@ -0,0 +1,23 @@ | @@ -0,0 +1,23 @@ | ||
1 | +class ShoppingCartPlugin::LineItem | ||
2 | + | ||
3 | + attr_accessor :product_id, :quantity | ||
4 | + | ||
5 | + def initialize(product_id, name) | ||
6 | + @product_id = product_id | ||
7 | + @name = name | ||
8 | + @quantity = 0 | ||
9 | + end | ||
10 | + | ||
11 | + def product | ||
12 | + @product ||= Product.find_by_id(product_id) | ||
13 | + end | ||
14 | + | ||
15 | + def name | ||
16 | + product && product.name || @name | ||
17 | + end | ||
18 | + | ||
19 | + def ==(other) | ||
20 | + self.product == other.product && self.name == other.name && self.quantity == other.quantity | ||
21 | + end | ||
22 | + | ||
23 | +end |
plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb
@@ -77,11 +77,11 @@ class ShoppingCartPluginMyprofileControllerTest < ActionController::TestCase | @@ -77,11 +77,11 @@ class ShoppingCartPluginMyprofileControllerTest < ActionController::TestCase | ||
77 | end | 77 | end |
78 | 78 | ||
79 | should 'group filtered orders products and quantities' do | 79 | should 'group filtered orders products and quantities' do |
80 | - p1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1) | ||
81 | - p2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2) | ||
82 | - p3 = fast_create(Product, :enterprise_id => enterprise.id, :price => 3) | ||
83 | - po1_products = {p1.id => {:quantity => 1, :price => p1.price}, p2.id => {:quantity => 2, :price => p2.price }} | ||
84 | - po2_products = {p2.id => {:quantity => 1, :price => p2.price}, p3.id => {:quantity => 2, :price => p3.price }} | 80 | + p1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1, :name => 'p1') |
81 | + p2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2, :name => 'p2') | ||
82 | + p3 = fast_create(Product, :enterprise_id => enterprise.id, :price => 3, :name => 'p3') | ||
83 | + po1_products = {p1.id => {:quantity => 1, :price => p1.price, :name => p1.name}, p2.id => {:quantity => 2, :price => p2.price, :name => p2.name }} | ||
84 | + po2_products = {p2.id => {:quantity => 1, :price => p2.price, :name => p2.name }, p3.id => {:quantity => 2, :price => p3.price, :name => p3.name}} | ||
85 | po1 = ShoppingCartPlugin::PurchaseOrder.create!(:seller => enterprise, :products_list => po1_products, :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED) | 85 | po1 = ShoppingCartPlugin::PurchaseOrder.create!(:seller => enterprise, :products_list => po1_products, :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED) |
86 | po2 = ShoppingCartPlugin::PurchaseOrder.create!(:seller => enterprise, :products_list => po2_products, :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED) | 86 | po2 = ShoppingCartPlugin::PurchaseOrder.create!(:seller => enterprise, :products_list => po2_products, :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED) |
87 | 87 | ||
@@ -91,7 +91,13 @@ class ShoppingCartPluginMyprofileControllerTest < ActionController::TestCase | @@ -91,7 +91,13 @@ class ShoppingCartPluginMyprofileControllerTest < ActionController::TestCase | ||
91 | :to => (Time.now + 1.day).strftime(TIME_FORMAT), | 91 | :to => (Time.now + 1.day).strftime(TIME_FORMAT), |
92 | :filter_status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED | 92 | :filter_status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED |
93 | 93 | ||
94 | - hash = {p1.id => 1, p2.id => 3, p3.id => 2} | 94 | + lineitem1 = ShoppingCartPlugin::LineItem.new(p1.id, p1.name) |
95 | + lineitem1.quantity = 1 | ||
96 | + lineitem2 = ShoppingCartPlugin::LineItem.new(p2.id, p2.name) | ||
97 | + lineitem2.quantity = 3 | ||
98 | + lineitem3 = ShoppingCartPlugin::LineItem.new(p3.id, p3.name) | ||
99 | + lineitem3.quantity = 2 | ||
100 | + hash = {p1.id => lineitem1, p2.id => lineitem2, p3.id => lineitem3} | ||
95 | 101 | ||
96 | assert_equal hash, assigns(:products) | 102 | assert_equal hash, assigns(:products) |
97 | end | 103 | end |
plugins/shopping_cart/views/shopping_cart_plugin_myprofile/_orders_list.html.erb
@@ -38,14 +38,14 @@ | @@ -38,14 +38,14 @@ | ||
38 | </ul> | 38 | </ul> |
39 | <ul class="order-products"> | 39 | <ul class="order-products"> |
40 | <% order.products_list.each do |id, qp| %> | 40 | <% order.products_list.each do |id, qp| %> |
41 | - <% | 41 | + <% |
42 | begin | 42 | begin |
43 | product = Product.find(id) | 43 | product = Product.find(id) |
44 | rescue | 44 | rescue |
45 | product = nil | 45 | product = nil |
46 | end | 46 | end |
47 | %> | 47 | %> |
48 | - <li><%= product ? link_to(product.name, product.url) : _("Pruduct unavailable")%> | 48 | + <li><%= product ? link_to(product.name, product.url) : qp[:name]%> |
49 | × <%= qp[:quantity] %> = <%= float_to_currency_cart( qp[:quantity] * qp[:price], environment ) %></li> | 49 | × <%= qp[:quantity] %> = <%= float_to_currency_cart( qp[:quantity] * qp[:price], environment ) %></li> |
50 | <% end %> | 50 | <% end %> |
51 | </ul> | 51 | </ul> |
plugins/shopping_cart/views/shopping_cart_plugin_myprofile/_products_list.html.erb
@@ -6,10 +6,10 @@ | @@ -6,10 +6,10 @@ | ||
6 | <th><%= _('Product') %></th> | 6 | <th><%= _('Product') %></th> |
7 | <th><%= _('Quantity') %></th> | 7 | <th><%= _('Quantity') %></th> |
8 | </tr> | 8 | </tr> |
9 | - <% @products.each do |id, quantity|%> | 9 | + <% @products.each do |id, item|%> |
10 | <tr> | 10 | <tr> |
11 | - <td><%= link_to(Product.find(id).name, Product.find(id).url) %></td> | ||
12 | - <td style="text-align: center"><%= quantity %></td> | 11 | + <td><%= item.product ? link_to(item.name, item.product.url) : item.name %></td> |
12 | + <td style="text-align: center"><%= item.quantity %></td> | ||
13 | </tr> | 13 | </tr> |
14 | <% end %> | 14 | <% end %> |
15 | </table> | 15 | </table> |