From 9f013f0e375cbcb65fb0a771c0d4b2f5f9842e61 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sat, 4 Aug 2012 17:51:08 +1000 Subject: [PATCH] When loading distributors for sidebar, only show those that have active products on hand --- app/controllers/application_controller.rb | 2 +- app/models/spree/distributor.rb | 2 +- spec/models/distributors_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 32d33ac410..52ac215a6d 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_on_hand.by_name + @distributors = Spree::Distributor.with_active_products_on_hand.by_name end end diff --git a/app/models/spree/distributor.rb b/app/models/spree/distributor.rb index 7be94e8a83..ad6c5a0da8 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_on_hand, joins(:products).where('spree_products.count_on_hand > 0').select('distinct(distributors.*)') + scope :with_active_products_on_hand, lambda { joins(:products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).select('distinct(distributors.*)') } after_initialize :initialize_country before_validation :set_unused_address_fields diff --git a/spec/models/distributors_spec.rb b/spec/models/distributors_spec.rb index 50657a36a3..64fa93b585 100644 --- a/spec/models/distributors_spec.rb +++ b/spec/models/distributors_spec.rb @@ -28,7 +28,7 @@ module Spree create(:product, :distributors => [d1], :on_hand => 5) create(:product, :distributors => [d3], :on_hand => 0) - Distributor.with_products_on_hand.sort.should == [d1, d2] + Distributor.with_active_products_on_hand.sort.should == [d1, d2] end end