Replace set_methods in order model

This commit is contained in:
cyrillefr
2025-04-23 14:27:35 +02:00
parent 9996fcb54d
commit 7812d5f58a
10 changed files with 22 additions and 23 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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