diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 54b08aed8b..7d221ddbeb 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -224,22 +224,27 @@ Spree::OrdersController.class_eval do n == Float::INFINITY ? 2147483647 : n end + # If a specific order is mentioned, check that it is COMPLETE, that changes + # are allowed and that the user has access. Return nil if not. def order_to_update - order = Spree::Order.complete.find_by_number(params[:id]) - return order if order.andand.changes_allowed? && can?(:update, order) - current_order + return @order_to_update if defined? @order_to_update + return @order_to_update = current_order unless params[:id] + @order_to_update = begin + order = Spree::Order.complete.find_by_number(params[:id]) + order = nil unless order.andand.changes_allowed? && can?(:update, order) + order + end end def check_at_least_one_line_item - order = order_to_update - return unless order.complete? + return unless order_to_update.andand.complete? items = params[:order][:line_items_attributes] .andand.select{ |k,attrs| attrs["quantity"].to_i > 0 } if items.empty? flash[:error] = I18n.t(:orders_cannot_remove_the_final_item) - redirect_to order_path(order) + redirect_to order_path(order_to_update) end end end diff --git a/spec/controllers/spree/orders_controller_spec.rb b/spec/controllers/spree/orders_controller_spec.rb index 4ec0c63eed..e7b5a7e12a 100644 --- a/spec/controllers/spree/orders_controller_spec.rb +++ b/spec/controllers/spree/orders_controller_spec.rb @@ -323,8 +323,8 @@ describe Spree::OrdersController do context "and the order is not complete" do let!(:order) { create(:order) } - it "returns the current_order" do - expect(controller.send(:order_to_update)).to eq current_order + it "returns nil" do + expect(controller.send(:order_to_update)).to eq nil end end @@ -334,8 +334,8 @@ describe Spree::OrdersController do context "and the user doesn't have permisson to 'update' the order" do before { allow(controller).to receive(:can?).with(:update, order) { false } } - it "returns the current_order" do - expect(controller.send(:order_to_update)).to eq current_order + it "returns nil" do + expect(controller.send(:order_to_update)).to eq nil end end @@ -344,8 +344,8 @@ describe Spree::OrdersController do context "and the order is not editable" do - it "returns the current_order" do - expect(controller.send(:order_to_update)).to eq current_order + it "returns nil" do + expect(controller.send(:order_to_update)).to eq nil end end