mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-05 22:26:07 +00:00
Patching a bug in which we'd 404 if deleting items from a cart twice in parallel
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user