Patching a bug in which we'd 404 if deleting items from a cart twice in parallel

This commit is contained in:
Will Marshall
2013-11-07 15:12:28 +11:00
parent f0d335a454
commit b7c7730278
2 changed files with 42 additions and 1 deletions

View File

@@ -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

View File

@@ -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