From b7c7730278512b347bc7e1cb5be7bcb66ae63bb5 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Thu, 7 Nov 2013 15:12:28 +1100 Subject: [PATCH] Patching a bug in which we'd 404 if deleting items from a cart twice in parallel --- .../spree/orders_controller_decorator.rb | 14 +++++++++ spec/controllers/orders_controller_spec.rb | 29 ++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index f13a21fb47..308aa55046 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -3,6 +3,7 @@ require 'spree/core/controller_helpers/order_decorator' Spree::OrdersController.class_eval do after_filter :populate_variant_attributes, :only => :populate before_filter :update_distribution, :only => :update + before_filter :filter_order_params, :only => :update # Patch Orders#populate to provide distributor_id and order_cycle_id to OrderPopulator def populate @@ -62,6 +63,18 @@ Spree::OrdersController.class_eval do end end + def filter_order_params + if params[:order] and params[:order][:line_items_attributes] + params[:order][:line_items_attributes] = remove_missing_line_items(params[:order][:line_items_attributes]) + end + end + + def remove_missing_line_items(attrs) + attrs.select do |i, line_item| + Spree::LineItem.find_by_id(line_item[:id]) + end + end + def clear @order = current_order(true) @order.empty! @@ -107,4 +120,5 @@ Spree::OrdersController.class_eval do spree_current_user.cart.add_variant hash[:variants].keys.first, hash[:variants].values.first, distributor, order_cycle, current_currency end end + end diff --git a/spec/controllers/orders_controller_spec.rb b/spec/controllers/orders_controller_spec.rb index e3241c4509..2d5dd64576 100644 --- a/spec/controllers/orders_controller_spec.rb +++ b/spec/controllers/orders_controller_spec.rb @@ -112,11 +112,38 @@ describe Spree::OrdersController do end end + context "removing line items from cart" do + describe "when I pass params that includes a line item no longer in our cart" do + it "should silently ignore the missing line item" do + order = subject.current_order(true) + li = order.add_variant(create(:simple_product).master) + spree_get :update, order: { line_items_attributes: { + "0" => {quantity: "0", id: "9999"}, + "1" => {quantity: "99", id: li.id} + }} + response.status.should == 302 + li.reload.quantity.should == 99 + end + end + + it "filters line items that are missing from params" do + order = subject.current_order(true) + li = order.add_variant(create(:simple_product).master) + + attrs = { + "0" => {quantity: "0", id: "9999"}, + "1" => {quantity: "99", id: li.id} + } + + controller.remove_missing_line_items(attrs).should == { + "1" => {quantity: "99", id: li.id} + } + end + end private def num_items_in_cart Spree::Order.last.andand.line_items.andand.count || 0 end - end