diff --git a/app/controllers/cart_controller.rb b/app/controllers/cart_controller.rb index f3a0de59ea..6e6fb22775 100644 --- a/app/controllers/cart_controller.rb +++ b/app/controllers/cart_controller.rb @@ -24,12 +24,12 @@ class CartController < BaseController populate_variant_attributes end + private + def variant_ids_in(variants_h) variants_h.map { |v| v[:variant_id].to_i } end - private - def check_authorization session[:access_token] ||= params[:token] order = Spree::Order.find_by(number: params[:id]) || current_order diff --git a/app/controllers/spree/orders_controller.rb b/app/controllers/spree/orders_controller.rb index b519f3043f..4343ff87cb 100644 --- a/app/controllers/spree/orders_controller.rb +++ b/app/controllers/spree/orders_controller.rb @@ -38,17 +38,6 @@ module Spree redirect_to main_app.cart_path end - def check_authorization - session[:access_token] ||= params[:token] - order = Spree::Order.find_by(number: params[:id]) || current_order - - if order - authorize! :edit, order, session[:access_token] - else - authorize! :create, Spree::Order - end - end - # Patching to redirect to shop if order is empty def edit @order = current_order(true) @@ -109,23 +98,6 @@ module Spree end end - def set_current_order - @order = current_order(true) - end - - def filter_order_params - if params[:order] && 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 cancel @order = Spree::Order.find_by!(number: params[:id]) authorize! :cancel, @order @@ -140,6 +112,21 @@ module Spree private + def set_current_order + @order = current_order(true) + end + + def check_authorization + session[:access_token] ||= params[:token] + order = Spree::Order.find_by(number: params[:id]) || current_order + + if order + authorize! :edit, order, session[:access_token] + else + authorize! :create, Spree::Order + end + end + # Stripe can redirect here after a payment is processed in the backoffice. # We verify if it was successful here and persist the changes. def handle_stripe_response @@ -153,6 +140,19 @@ module Spree @order.reload end + def filter_order_params + if params[:order] && 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 discard_empty_line_items @order.line_items = @order.line_items.select { |li| li.quantity > 0 } end diff --git a/spec/controllers/cart_controller_spec.rb b/spec/controllers/cart_controller_spec.rb index 585c96a37e..5f830c583b 100644 --- a/spec/controllers/cart_controller_spec.rb +++ b/spec/controllers/cart_controller_spec.rb @@ -54,7 +54,7 @@ describe CartController, type: :controller do variants_h = [{ variant_id: "900", quantity: 2, max_quantity: nil }, { variant_id: "940", quantity: 3, max_quantity: 3 }] - expect(controller.variant_ids_in(variants_h)).to eq([900, 940]) + expect(controller.__send__(:variant_ids_in, variants_h)).to eq([900, 940]) end end diff --git a/spec/controllers/spree/orders_controller_spec.rb b/spec/controllers/spree/orders_controller_spec.rb index 78f0459110..34c8b31a52 100644 --- a/spec/controllers/spree/orders_controller_spec.rb +++ b/spec/controllers/spree/orders_controller_spec.rb @@ -315,7 +315,7 @@ describe Spree::OrdersController, type: :controller do "1" => { quantity: "99", id: li.id } } - expect(controller.remove_missing_line_items(attrs)).to eq( + expect(controller.__send__(:remove_missing_line_items, attrs)).to eq( "1" => { quantity: "99", id: li.id } ) end