Skip to content

Commit

Permalink
Spec, fix for not filling backorders on incomplete orders [Fixes #4056]
Browse files Browse the repository at this point in the history
  • Loading branch information
sohara authored and GeekOnCoffee committed Dec 5, 2013
1 parent 4044bef commit 6c87593
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
7 changes: 4 additions & 3 deletions core/app/models/spree/inventory_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class InventoryUnit < ActiveRecord::Base
scope :backordered, -> { where state: 'backordered' }
scope :shipped, -> { where state: 'shipped' }
scope :backordered_per_variant, ->(stock_item) do
includes(:shipment)
includes(:shipment, :order)
.where("spree_shipments.state != 'canceled'")
.where(variant_id: stock_item.variant_id)
.backordered.order("#{self.table_name}.created_at ASC")
.where('spree_orders.completed_at is not null')
.backordered.order("spree_orders.completed_at ASC")
end

attr_accessible :shipment, :variant_id
Expand All @@ -36,7 +37,7 @@ class InventoryUnit < ActiveRecord::Base
# lead to issues once users tried to modify the objects returned. That's due
# to ActiveRecord `joins(shipment: :stock_location)` only return readonly
# objects
#
#
# 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|
Expand Down
41 changes: 40 additions & 1 deletion core/spec/models/spree/inventory_unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
let(:stock_item) { stock_location.stock_items.order(:id).first }

context "#backordered_for_stock_item" do
let(:order) { create(:order) }
let(:order) do
order = create(:order)
order.state = 'complete'
order.completed_at = Time.now
order.tap(&:save!)
end

let(:shipment) do
shipment = Spree::Shipment.new
Expand All @@ -21,6 +26,7 @@
unit = shipment.inventory_units.build
unit.state = 'backordered'
unit.variant_id = stock_item.variant.id
unit.order_id = order.id
unit.tap(&:save!)
end

Expand Down Expand Up @@ -50,6 +56,39 @@

Spree::InventoryUnit.backordered_for_stock_item(stock_item).should_not include(other_variant_unit)
end

context "other shipments" do
let(:other_order) do
order = create(:order)
order.state = 'payment'
order.completed_at = nil
order.tap(&:save!)
end

let(:other_shipment) do
shipment = Spree::Shipment.new
shipment.stock_location = stock_location
shipment.shipping_methods << create(:shipping_method)
shipment.order = other_order
# We don't care about this in this test
shipment.stub(:ensure_correct_adjustment)
shipment.tap(&:save!)
end

let!(:other_unit) do
unit = other_shipment.inventory_units.build
unit.state = 'backordered'
unit.variant_id = stock_item.variant.id
unit.order_id = other_order.id
unit.tap(&:save!)
end

it "does not find inventory units belonging to incomplete orders" do
Spree::InventoryUnit.backordered_for_stock_item(stock_item).should_not include(other_unit)
end

end

end

context "variants deleted" do
Expand Down

0 comments on commit 6c87593

Please sign in to comment.