From 7812d5f58a7055a1c64ecbf27a291b771b273ece Mon Sep 17 00:00:00 2001 From: cyrillefr Date: Wed, 23 Apr 2025 14:27:35 +0200 Subject: [PATCH] Replace set_methods in order model --- .rubocop_todo.yml | 3 +-- app/controllers/base_controller.rb | 2 +- app/controllers/concerns/order_completion.rb | 2 +- app/controllers/concerns/order_stock_check.rb | 2 +- app/controllers/shop_controller.rb | 2 +- app/models/spree/order.rb | 4 ++-- app/services/orders/cart_reset_service.rb | 2 +- .../enterprises_controller_spec.rb | 2 +- .../stripe_controller_spec.rb | 2 +- spec/models/spree/order_spec.rb | 24 +++++++++---------- 10 files changed, 22 insertions(+), 23 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index cec87cdfe6..dcc59a6a62 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -221,10 +221,9 @@ Metrics/PerceivedComplexity: - 'app/models/spree/ability.rb' - 'app/models/spree/order/checkout.rb' -# Offense count: 6 +# Offense count: 4 Naming/AccessorMethodName: Exclude: - - 'app/models/spree/order.rb' - 'app/services/checkout/post_checkout_actions.rb' - 'lib/spree/core/controller_helpers/common.rb' - 'spec/support/request/shop_workflow.rb' diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb index f235694085..5690c88805 100644 --- a/app/controllers/base_controller.rb +++ b/app/controllers/base_controller.rb @@ -25,6 +25,6 @@ class BaseController < ApplicationController def set_order_cycle return if @order_cycles.count != 1 - current_order(true).set_order_cycle! @order_cycles.first + current_order(true).assign_order_cycle! @order_cycles.first end end diff --git a/app/controllers/concerns/order_completion.rb b/app/controllers/concerns/order_completion.rb index ca67d4a84d..a99054f960 100644 --- a/app/controllers/concerns/order_completion.rb +++ b/app/controllers/concerns/order_completion.rb @@ -26,7 +26,7 @@ module OrderCompletion # Builds an order setting the token and distributor of the one specified def build_new_order(distributor, token) new_order = current_order(true) - new_order.set_distributor!(distributor) + new_order.assign_distributor!(distributor) new_order.tokenized_permission.token = token new_order.tokenized_permission.save! end diff --git a/app/controllers/concerns/order_stock_check.rb b/app/controllers/concerns/order_stock_check.rb index e6ad851b8c..33a0a59a90 100644 --- a/app/controllers/concerns/order_stock_check.rb +++ b/app/controllers/concerns/order_stock_check.rb @@ -25,7 +25,7 @@ module OrderStockCheck Alert.raise_with_record("Notice: order cycle closed during checkout completion", current_order) current_order.empty! - current_order.set_order_cycle! nil + current_order.assign_order_cycle! nil flash[:info] = I18n.t('order_cycle_closed') respond_to do |format| diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index 100acc7fc6..18e6183bc3 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -11,7 +11,7 @@ class ShopController < BaseController def order_cycle if request.post? if oc = OrderCycle.with_distributor(@distributor).active.find_by(id: params[:order_cycle_id]) - current_order(true).set_order_cycle! oc + current_order(true).assign_order_cycle! oc @current_order_cycle = oc render json: @current_order_cycle, serializer: Api::OrderCycleSerializer else diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 0199156469..909cb1a529 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -560,7 +560,7 @@ module Spree end end - def set_order_cycle!(order_cycle) + def assign_order_cycle!(order_cycle) return if self.order_cycle == order_cycle self.order_cycle = order_cycle @@ -573,7 +573,7 @@ module Spree line_items.includes(variant: :stock_items).find_each(&:cap_quantity_at_stock!) end - def set_distributor!(distributor) + def assign_distributor!(distributor) self.distributor = distributor self.order_cycle = nil unless order_cycle&.has_distributor? distributor save! diff --git a/app/services/orders/cart_reset_service.rb b/app/services/orders/cart_reset_service.rb index 418b386aa7..bf2d77215a 100644 --- a/app/services/orders/cart_reset_service.rb +++ b/app/services/orders/cart_reset_service.rb @@ -13,7 +13,7 @@ module Orders def reset_distributor if order.distributor && order.distributor != distributor order.empty! - order.set_order_cycle! nil + order.assign_order_cycle! nil end order.distributor = distributor end diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb index 014d21706b..1a088c6f56 100644 --- a/spec/controllers/enterprises_controller_spec.rb +++ b/spec/controllers/enterprises_controller_spec.rb @@ -19,7 +19,7 @@ RSpec.describe EnterprisesController, type: :controller do } before do - order.set_distributor! current_distributor + order.assign_distributor! current_distributor order.line_items << line_item end diff --git a/spec/controllers/payment_gateways/stripe_controller_spec.rb b/spec/controllers/payment_gateways/stripe_controller_spec.rb index c107cf6eee..28c2c0b595 100644 --- a/spec/controllers/payment_gateways/stripe_controller_spec.rb +++ b/spec/controllers/payment_gateways/stripe_controller_spec.rb @@ -64,7 +64,7 @@ RSpec.describe PaymentGateways::StripeController, type: :controller do expect(controller).to receive(:current_order).and_return(order).at_least(:once) expect(order_cycle).to receive(:closed?).and_return(true) expect(order).to receive(:empty!) - expect(order).to receive(:set_order_cycle!).with(nil) + expect(order).to receive(:assign_order_cycle!).with(nil) get :confirm, params: { payment_intent: "pi_123" } diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index b3f26520ef..1baabc5878 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -796,7 +796,7 @@ RSpec.describe Spree::Order do describe "setting the distributor" do it "sets the distributor when no order cycle is set" do d = create(:distributor_enterprise) - subject.set_distributor! d + subject.assign_distributor! d expect(subject.distributor).to eq(d) end @@ -806,7 +806,7 @@ RSpec.describe Spree::Order do create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d, incoming: false) subject.order_cycle = oc - subject.set_distributor! d + subject.assign_distributor! d expect(subject.distributor).to eq(d) expect(subject.order_cycle).to eq(oc) @@ -817,7 +817,7 @@ RSpec.describe Spree::Order do oc = create(:simple_order_cycle) subject.order_cycle = oc - subject.set_distributor! d + subject.assign_distributor! d expect(subject.distributor).to eq(d) expect(subject.order_cycle).to be_nil @@ -825,8 +825,8 @@ RSpec.describe Spree::Order do it "clears the distributor when setting to nil" do d = create(:distributor_enterprise) - subject.set_distributor! d - subject.set_distributor! nil + subject.assign_distributor! d + subject.assign_distributor! nil expect(subject.distributor).to be_nil end @@ -853,16 +853,16 @@ RSpec.describe Spree::Order do it "empties the cart when changing the order cycle" do expect(subject).to receive(:empty!) - subject.set_order_cycle! oc + subject.assign_order_cycle! oc end it "doesn't empty the cart if the order cycle is not different" do expect(subject).not_to receive(:empty!) - subject.set_order_cycle! subject.order_cycle + subject.assign_order_cycle! subject.order_cycle end it "sets the order cycle when no distributor is set" do - subject.set_order_cycle! oc + subject.assign_order_cycle! oc expect(subject.order_cycle).to eq(oc) end @@ -871,7 +871,7 @@ RSpec.describe Spree::Order do create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d, incoming: false) subject.distributor = d - subject.set_order_cycle! oc + subject.assign_order_cycle! oc expect(subject.order_cycle).to eq(oc) expect(subject.distributor).to eq(d) @@ -881,7 +881,7 @@ RSpec.describe Spree::Order do d = create(:distributor_enterprise) subject.distributor = d - subject.set_order_cycle! oc + subject.assign_order_cycle! oc expect(subject.order_cycle).to eq(oc) expect(subject.distributor).to be_nil @@ -889,10 +889,10 @@ RSpec.describe Spree::Order do it "clears the order cycle when setting to nil" do d = create(:distributor_enterprise) - subject.set_order_cycle! oc + subject.assign_order_cycle! oc subject.distributor = d - subject.set_order_cycle! nil + subject.assign_order_cycle! nil expect(subject.order_cycle).to be_nil expect(subject.distributor).to eq(d)