diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index fad8e785cd..ce006287fc 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,7 +6,7 @@ class ApplicationController < ActionController::Base private def load_data_for_sidebar @suppliers = Spree::Supplier.all - @distributors = Spree::Distributor.with_products + @distributors = Spree::Distributor.with_products_on_hand end end diff --git a/app/models/spree/distributor.rb b/app/models/spree/distributor.rb index fe920c7820..b5d033275e 100644 --- a/app/models/spree/distributor.rb +++ b/app/models/spree/distributor.rb @@ -13,7 +13,7 @@ module Spree validates_associated :pickup_address scope :by_name, order('name') - scope :with_products, joins(:products).group('distributors.id') + scope :with_products_on_hand, joins(:products).where('spree_products.count_on_hand > 0').group('distributors.id') after_initialize :initialize_country before_validation :set_unused_address_fields diff --git a/spec/factories.rb b/spec/factories.rb index 6143b7110e..a4011f51d6 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -40,6 +40,7 @@ end FactoryGirl.modify do factory :simple_product do supplier { Spree::Supplier.first || FactoryGirl.create(:supplier) } + on_hand 3 # before(:create) do |product, evaluator| # product.product_distributions = [FactoryGirl.create(:product_distribution, :product => product)] diff --git a/spec/models/distributors_spec.rb b/spec/models/distributors_spec.rb index 7614e53cc5..50657a36a3 100644 --- a/spec/models/distributors_spec.rb +++ b/spec/models/distributors_spec.rb @@ -19,14 +19,16 @@ module Spree end describe "scopes" do - it "returns distributors with products" do + it "returns distributors with products in stock" do d1 = create(:distributor) d2 = create(:distributor) d3 = create(:distributor) - create(:product, :distributors => [d1, d2]) - create(:product, :distributors => [d1]) + d4 = create(:distributor) + create(:product, :distributors => [d1, d2], :on_hand => 5) + create(:product, :distributors => [d1], :on_hand => 5) + create(:product, :distributors => [d3], :on_hand => 0) - Distributor.with_products.sort.should == [d1, d2] + Distributor.with_products_on_hand.sort.should == [d1, d2] end end