Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Remove order association from inventory units" #2124

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions core/app/models/spree/inventory_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class InventoryUnit < Spree::Base
CANCELABLE_STATES = ['on_hand', 'backordered', 'shipped']

belongs_to :variant, -> { with_deleted }, class_name: "Spree::Variant", inverse_of: :inventory_units
belongs_to :order, class_name: "Spree::Order", inverse_of: :inventory_units
belongs_to :shipment, class_name: "Spree::Shipment", touch: true, inverse_of: :inventory_units
belongs_to :return_authorization, class_name: "Spree::ReturnAuthorization", inverse_of: :inventory_units
belongs_to :carton, class_name: "Spree::Carton", inverse_of: :inventory_units
Expand All @@ -15,11 +16,8 @@ class InventoryUnit < Spree::Base
has_many :return_items, inverse_of: :inventory_unit, dependent: :destroy
has_one :original_return_item, class_name: "Spree::ReturnItem", foreign_key: :exchange_inventory_unit_id, dependent: :destroy
has_one :unit_cancel, class_name: "Spree::UnitCancel"
has_one :order, through: :shipment

delegate :order_id, to: :shipment

validates_presence_of :shipment, :line_item, :variant
validates_presence_of :order, :shipment, :line_item, :variant

before_destroy :ensure_can_destroy

Expand Down
5 changes: 2 additions & 3 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ class CannotRebuildShipments < StandardError; end
has_many :products, through: :variants

# Shipping
has_many :inventory_units, inverse_of: :order
has_many :cartons, -> { distinct }, through: :inventory_units
has_many :shipments, dependent: :destroy, inverse_of: :order do
def states
pluck(:state).uniq
end
end
has_many :inventory_units, through: :shipments
has_many :cartons, -> { distinct }, through: :inventory_units

has_many :order_stock_locations, class_name: "Spree::OrderStockLocation"
has_many :stock_locations, through: :order_stock_locations

Expand Down
3 changes: 2 additions & 1 deletion core/app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,11 @@ def determine_state(order)
end
end

def set_up_inventory(state, variant, _order, line_item)
def set_up_inventory(state, variant, order, line_item)
inventory_units.create(
state: state,
variant_id: variant.id,
order_id: order.id,
line_item_id: line_item.id
)
end
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
FactoryGirl.define do
factory :inventory_unit, class: Spree::InventoryUnit do
variant
line_item { build(:line_item, variant: variant) }
order
line_item { build(:line_item, order: order, variant: variant) }
state 'on_hand'
shipment { build(:shipment, state: 'pending', order: line_item.order) }
shipment { build(:shipment, state: 'pending', order: order) }
# return_authorization
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
shipment.order.line_items.each do |line_item|
line_item.quantity.times do
shipment.inventory_units.create!(
order_id: shipment.order_id,
variant_id: line_item.variant_id,
line_item_id: line_item.id
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

let(:shipped_order_without_units) do
create(:shipped_order, line_items_count: 1) do |order|
order.shipments.each { |s| s.inventory_units.delete_all }
order.inventory_units.delete_all
end
end

Expand Down
14 changes: 4 additions & 10 deletions core/spec/models/spree/customer_return_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@
describe "#return_items_belong_to_same_order" do
let(:customer_return) { build(:customer_return) }

let(:first_order) { create(:order_with_line_items) }
let(:second_order) { first_order }

let(:first_shipment) { first_order.shipments.first }
let(:second_shipment) { second_order.shipments.first }

let(:first_inventory_unit) { build(:inventory_unit, shipment: first_shipment) }
let(:first_inventory_unit) { build(:inventory_unit) }
let(:first_return_item) { build(:return_item, inventory_unit: first_inventory_unit) }

let(:second_inventory_unit) { build(:inventory_unit, shipment: second_shipment) }
let(:second_inventory_unit) { build(:inventory_unit, order: second_order) }
let(:second_return_item) { build(:return_item, inventory_unit: second_inventory_unit) }

subject { customer_return.valid? }
Expand All @@ -29,7 +23,7 @@
end

context "return items are part of different orders" do
let(:second_order) { create(:order_with_line_items) }
let(:second_order) { create(:order) }

it "is not valid" do
expect(subject).to eq false
Expand All @@ -42,7 +36,7 @@
end

context "return items are part of the same order" do
let(:second_order) { first_order }
let(:second_order) { first_inventory_unit.order }

it "is valid" do
expect(subject).to eq true
Expand Down
4 changes: 4 additions & 0 deletions core/spec/models/spree/inventory_unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
unit = shipment.inventory_units.first
unit.state = 'backordered'
unit.variant_id = stock_item.variant.id
unit.order_id = order.id
unit.line_item = line_item
unit.tap(&:save!)
end
Expand All @@ -58,6 +59,7 @@
it "does not find inventory units that aren't backordered" do
on_hand_unit = shipment.inventory_units.build
on_hand_unit.state = 'on_hand'
on_hand_unit.order_id = order.id
on_hand_unit.line_item = line_item
on_hand_unit.variant = stock_item.variant
on_hand_unit.save!
Expand All @@ -68,6 +70,7 @@
it "does not find inventory units that don't match the stock item's variant" do
other_variant_unit = shipment.inventory_units.build
other_variant_unit.state = 'backordered'
other_variant_unit.order_id = order.id
other_variant_unit.line_item = line_item
other_variant_unit.variant = create(:variant)
other_variant_unit.save!
Expand Down Expand Up @@ -104,6 +107,7 @@
unit = other_shipment.inventory_units.build
unit.state = 'backordered'
unit.variant_id = stock_item.variant.id
unit.order_id = other_order.id
unit.line_item = line_item
unit.tap(&:save!)
end
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/order_contents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
end

context 'given a shipment' do
let!(:shipment) { create(:shipment, order: order) }
let!(:shipment) { create(:shipment) }

it "ensure shipment calls update_amounts instead of order calling ensure_updated_shipments" do
expect(subject.order).to_not receive(:ensure_updated_shipments)
Expand Down
3 changes: 1 addition & 2 deletions core/spec/models/spree/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@
end

before do
allow(line_item).to receive(:order) { order }
allow(shipment).to receive(:inventory_units) { inventory_units }
allow(inventory_units).to receive_message_chain(:includes, :joins).and_return inventory_units
end
Expand Down Expand Up @@ -644,7 +643,7 @@
let(:inventory_units) { double }

let(:params) do
{ variant_id: variant.id, state: 'on_hand', line_item_id: line_item.id }
{ variant_id: variant.id, state: 'on_hand', order_id: order.id, line_item_id: line_item.id }
end

before { allow(shipment).to receive_messages inventory_units: inventory_units }
Expand Down
31 changes: 9 additions & 22 deletions core/spec/models/spree/shipping_manifest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ module Spree
let(:order) { Order.create! }
let(:variant) { create :variant }
let!(:shipment) { create(:shipment, state: 'pending', order: order) }
subject(:manifest) { described_class.new(inventory_units: inventory_units) }
let(:manifest) { described_class.new(inventory_units: inventory_units) }

def build_unit(variant, attrs = {})
attrs = { variant: variant, shipment: shipment }.merge(attrs)
attrs[:line_item] = order.contents.add(variant)
attrs = { order: order, variant: variant, shipment: shipment }.merge(attrs)
attrs[:line_item] = attrs[:order].contents.add(attrs[:variant])
InventoryUnit.new(attrs)
end

subject{ manifest }

describe "#items" do
context 'empty' do
let(:inventory_units) { [] }
Expand Down Expand Up @@ -66,15 +68,9 @@ def build_unit(variant, attrs = {})
end

describe "#for_order" do
let!(:order2) { create(:order_with_line_items) }
let!(:order2) { Order.create! }
context 'single unit' do
let(:inventory_units) { [inventory_unit] }
let(:inventory_unit) { build_unit(variant) }

before do
allow(inventory_unit).to receive(:order_id) { order.id }
end

let(:inventory_units) { [build_unit(variant)] }
it "has single ManifestItem in correct order" do
expect(manifest.for_order(order).items.count).to eq 1
end
Expand All @@ -85,22 +81,13 @@ def build_unit(variant, attrs = {})
end

context 'one units in each order' do
let(:order_2) { build_stubbed(:order) }
let(:inventory_units) { [inventory_unit_one, inventory_unit_two] }
let(:inventory_unit_one) { build_unit(variant) }
let(:inventory_unit_two) { build_unit(variant) }

before do
allow(inventory_unit_one).to receive(:order_id) { order.id }
allow(inventory_unit_two).to receive(:order_id) { order_2.id }
end

let(:inventory_units) { [build_unit(variant), build_unit(variant, order: order2)] }
it "has single ManifestItem in first order" do
expect(manifest.for_order(order).items.count).to eq 1
end

it "has single ManifestItem in second order" do
expect(manifest.for_order(order_2).items.count).to eq 1
expect(manifest.for_order(order2).items.count).to eq 1
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/stock/packer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module Stock
Array.new(30) do
build(
:inventory_unit,
shipment: order.shipments.first,
order: order,
line_item: line_item,
variant: variant
)
Expand Down