Skip to content

Commit

Permalink
Merge pull request solidusio#1061 from eric1234/better-support-for-gl…
Browse files Browse the repository at this point in the history
…obal-inventory-tracking-flag

Fix case where `track_inventory_levels` global flag not being honored
  • Loading branch information
cbrunsdon committed Apr 14, 2016
2 parents 0aab02b + e7053fe commit 6ee2e98
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Variant < Spree::Base
# @param stock_locations [Array<Spree::StockLocation>] the stock locations to check
# @return [ActiveRecord::Relation]
def self.in_stock(stock_locations = nil)
return all unless Spree::Config.track_inventory_levels
in_stock_variants = joins(:stock_items).where(Spree::StockItem.arel_table[:count_on_hand].gt(0).or(arel_table[:track_inventory].eq(false)))
if stock_locations.present?
in_stock_variants = in_stock_variants.where(spree_stock_items: { stock_location_id: stock_locations.map(&:id) })
Expand Down
12 changes: 10 additions & 2 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@
end

describe "in_stock scope" do
subject { Spree::Variant.in_stock }
let!(:in_stock_variant) { create(:variant) }
let!(:out_of_stock_variant) { create(:variant) }
let!(:stock_location) { create(:stock_location) }
Expand Down Expand Up @@ -534,8 +535,6 @@
end

context "a stock location is not provided" do
subject { Spree::Variant.in_stock }

before do
in_stock_variant.stock_items.first.update_column(:count_on_hand, 10)
end
Expand All @@ -544,6 +543,15 @@
expect(subject).to eq [in_stock_variant]
end
end

context "inventory levels globally not tracked" do
before { Spree::Config.track_inventory_levels = false }
after { Spree::Config.track_inventory_levels = true }

it 'includes items without inventory' do
expect( subject ).to include out_of_stock_variant
end
end
end

describe "#display_image" do
Expand Down

0 comments on commit 6ee2e98

Please sign in to comment.