From 87d20877ade68ee8a598a19fad6d079d501845f9 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 21 Jan 2025 15:03:06 +1100 Subject: [PATCH] Remove Shipment#stock_location --- app/controllers/api/v0/shipments_controller.rb | 6 +----- app/models/spree/inventory_unit.rb | 4 +--- app/models/spree/shipment.rb | 9 +-------- app/serializers/api/shipment_serializer.rb | 6 +----- app/views/spree/admin/orders/_form.html.haml | 2 +- .../api/v0/shipments_controller_spec.rb | 14 +++++--------- spec/factories/shipment_factory.rb | 2 -- spec/factories/stock_factory.rb | 1 - spec/models/spree/inventory_unit_spec.rb | 1 - spec/models/spree/shipment_spec.rb | 2 -- 10 files changed, 10 insertions(+), 37 deletions(-) diff --git a/app/controllers/api/v0/shipments_controller.rb b/app/controllers/api/v0/shipments_controller.rb index b041eacd97..1580e1f89f 100644 --- a/app/controllers/api/v0/shipments_controller.rb +++ b/app/controllers/api/v0/shipments_controller.rb @@ -14,7 +14,7 @@ module Api def create variant = scoped_variant(params[:variant_id]) quantity = params[:quantity].to_i - @shipment = get_or_create_shipment(params[:stock_location_id]) + @shipment = @order.shipment || @order.shipments.create @order.contents.add(variant, quantity, @shipment) @@ -116,10 +116,6 @@ module Api variant end - def get_or_create_shipment(stock_location_id) - @order.shipment || @order.shipments.create(stock_location_id:) - end - def shipment_params return {} unless params.has_key? :shipment diff --git a/app/models/spree/inventory_unit.rb b/app/models/spree/inventory_unit.rb index 7fb69152e6..0f66dbb8dd 100644 --- a/app/models/spree/inventory_unit.rb +++ b/app/models/spree/inventory_unit.rb @@ -42,9 +42,7 @@ module Spree # # Returns an array of backordered inventory units as per a given stock item def self.backordered_for_stock_item(stock_item) - backordered_per_variant(stock_item).select do |unit| - unit.shipment.stock_location == stock_item.stock_location - end + backordered_per_variant(stock_item) end def self.finalize_units!(inventory_units) diff --git a/app/models/spree/shipment.rb b/app/models/spree/shipment.rb index 711eedcb48..5b9692912c 100644 --- a/app/models/spree/shipment.rb +++ b/app/models/spree/shipment.rb @@ -5,13 +5,11 @@ require 'ostruct' module Spree class Shipment < ApplicationRecord self.belongs_to_required_by_default = false + self.ignored_columns += [:stock_location_id] belongs_to :order, class_name: 'Spree::Order' belongs_to :address, class_name: 'Spree::Address' - # WIP: phasing out stock location, it's not used. - belongs_to :stock_location, class_name: 'Spree::StockLocation', optional: true - has_many :shipping_rates, dependent: :delete_all has_many :shipping_methods, through: :shipping_rates has_many :state_changes, as: :stateful, dependent: :destroy @@ -303,11 +301,6 @@ module Spree !shipped? && !order.canceled? end - # Other code still calls this for convenience. - def stock_location - @stock_location ||= DefaultStockLocation.find_or_create - end - private def line_items diff --git a/app/serializers/api/shipment_serializer.rb b/app/serializers/api/shipment_serializer.rb index 1c03d9eb5e..278112bbf8 100644 --- a/app/serializers/api/shipment_serializer.rb +++ b/app/serializers/api/shipment_serializer.rb @@ -2,14 +2,10 @@ module Api class ShipmentSerializer < ActiveModel::Serializer - attributes :id, :tracking, :number, :order_id, :cost, :shipped_at, :stock_location_name, :state + attributes :id, :tracking, :number, :order_id, :cost, :shipped_at, :state def order_id object.order.number end - - def stock_location_name - object.stock_location.name - end end end diff --git a/app/views/spree/admin/orders/_form.html.haml b/app/views/spree/admin/orders/_form.html.haml index 4d43acf8dd..02b1acf841 100644 --- a/app/views/spree/admin/orders/_form.html.haml +++ b/app/views/spree/admin/orders/_form.html.haml @@ -30,5 +30,5 @@ var order_number = '#{@order.number}'; var shipments = []; - @order.shipments.each do |shipment| - shipments.push(#{shipment.to_json(:root => false, :include => [:inventory_units, :stock_location]).html_safe}); + shipments.push(#{shipment.to_json(:root => false, :include => [:inventory_units]).html_safe}); = render :partial => 'spree/admin/shared/update_order_state', :handlers => [:erb], :formats => [:js] diff --git a/spec/controllers/api/v0/shipments_controller_spec.rb b/spec/controllers/api/v0/shipments_controller_spec.rb index ad70950001..1c2196b36b 100644 --- a/spec/controllers/api/v0/shipments_controller_spec.rb +++ b/spec/controllers/api/v0/shipments_controller_spec.rb @@ -6,9 +6,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do render_views let!(:shipment) { create(:shipment) } - let!(:attributes) do - [:id, :tracking, :number, :cost, :shipped_at, :stock_location_name, :order_id] - end + let(:attributes) { %w[id tracking number cost shipped_at order_id] } let(:current_api_user) { build(:user) } before do @@ -31,13 +29,11 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do let(:current_api_user) { build(:admin_user) } let!(:order) { shipment.order } let(:order_ship_address) { create(:address) } - let!(:stock_location) { DefaultStockLocation.find_or_create } let!(:variant) { create(:variant) } let(:params) do { quantity: 2, variant_id: variant.to_param, order_id: order.number, - stock_location_id: stock_location.to_param, format: :json } end let(:error_message) { "broken shipments creation" } @@ -109,7 +105,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do allow_any_instance_of(Spree::Order).to receive_messages(paid?: true, complete?: true) api_put :ready, order_id: shipment.order.to_param, id: shipment.to_param - expect(attributes.all?{ |attr| json_response.key? attr.to_s }).to be_truthy + expect(json_response.keys).to include(*attributes) expect(json_response["state"]).to eq("ready") expect(shipment.reload.state).to eq("ready") end @@ -120,7 +116,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do api_put :ready, order_id: shipment.order.to_param, id: shipment.to_param - expect(attributes.all?{ |attr| json_response.key? attr.to_s }).to be_truthy + expect(json_response.keys).to include(*attributes) expect(json_response["state"]).to eq("ready") expect(shipment.reload.state).to eq("ready") end @@ -324,7 +320,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do id: shipment.to_param, shipment: { tracking: "123123" } - expect(attributes.all?{ |attr| json_response.key? attr.to_s }).to be_truthy + expect(json_response.keys).to include(*attributes) expect(json_response["state"]).to eq("shipped") end end @@ -424,7 +420,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do def expect_valid_response expect(response.status).to eq 200 - attributes.all?{ |attr| json_response.key? attr.to_s } + expect(json_response.keys).to include(*attributes) end def make_order_contents_fail diff --git a/spec/factories/shipment_factory.rb b/spec/factories/shipment_factory.rb index f168880861..26a665dd9d 100644 --- a/spec/factories/shipment_factory.rb +++ b/spec/factories/shipment_factory.rb @@ -11,7 +11,6 @@ FactoryBot.define do state { 'pending' } order address - stock_location { DefaultStockLocation.find_or_create } after(:create) do |shipment, _evalulator| shipment.add_shipping_method(create(:shipping_method), true) @@ -31,7 +30,6 @@ FactoryBot.define do state { 'pending' } order address - stock_location { DefaultStockLocation.find_or_create } trait :shipping_method do transient do diff --git a/spec/factories/stock_factory.rb b/spec/factories/stock_factory.rb index 254eff8ee1..84e9d35405 100644 --- a/spec/factories/stock_factory.rb +++ b/spec/factories/stock_factory.rb @@ -3,7 +3,6 @@ FactoryBot.define do factory :stock_package, class: OrderManagement::Stock::Package do transient do - stock_location { build(:stock_location) } order { create(:order_with_line_items, line_items_count: 2) } contents { [] } end diff --git a/spec/models/spree/inventory_unit_spec.rb b/spec/models/spree/inventory_unit_spec.rb index d4ea6b3622..3c1f620191 100644 --- a/spec/models/spree/inventory_unit_spec.rb +++ b/spec/models/spree/inventory_unit_spec.rb @@ -11,7 +11,6 @@ RSpec.describe Spree::InventoryUnit do let(:shipment) do shipment = Spree::Shipment.new - shipment.stock_location = stock_location shipment.shipping_methods << create(:shipping_method) shipment.order = order # We don't care about this in this test diff --git a/spec/models/spree/shipment_spec.rb b/spec/models/spree/shipment_spec.rb index 840b293282..6f420aabff 100644 --- a/spec/models/spree/shipment_spec.rb +++ b/spec/models/spree/shipment_spec.rb @@ -324,7 +324,6 @@ RSpec.describe Spree::Shipment do allow(shipment).to receive_message_chain(:inventory_units, :group_by, map: [unit]) - shipment.stock_location = build(:stock_location) expect(variant).to receive(:move).with(1, shipment) shipment.after_cancel end @@ -348,7 +347,6 @@ RSpec.describe Spree::Shipment do allow(shipment).to receive_message_chain(:inventory_units, :group_by, map: [unit]) - shipment.stock_location = create(:stock_location) expect(variant).to receive(:move).with(-1, shipment) shipment.after_resume end