From 13abf9ada790f678497af92841b1349e2d4db5ce Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 8 Mar 2013 16:46:25 +1100 Subject: [PATCH] Enterprise.active_distributors doesn't show distributors of unavailable products --- app/models/enterprise.rb | 6 ++++-- spec/models/enterprises_spec.rb | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index c7dd435b7c..ce14997734 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -33,9 +33,11 @@ class Enterprise < ActiveRecord::Base joins('LEFT OUTER JOIN exchanges ON (exchanges.receiver_id = enterprises.id)'). joins('LEFT OUTER JOIN order_cycles ON (order_cycles.id = exchanges.order_cycle_id)') - scope :active_distributors, with_distributed_products_outer.with_order_cycles_outer. - where('(product_distributions.product_id IS NOT NULL AND spree_products.deleted_at IS NULL) OR order_cycles.id IS NOT NULL'). + scope :active_distributors, lambda { + with_distributed_products_outer.with_order_cycles_outer. + where('(product_distributions.product_id IS NOT NULL AND spree_products.deleted_at IS NULL AND spree_products.available_on <= ?) OR order_cycles.id IS NOT NULL', Time.now). select('DISTINCT enterprises.*') + } def has_supplied_products_on_hand? diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index 4db9f592bc..03c63261c9 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -31,7 +31,12 @@ describe Enterprise do Enterprise.active_distributors.should be_empty end - it "doesn't show distributors of unavailable products" + it "doesn't show distributors of unavailable products" do + d = create(:distributor_enterprise) + create(:product, :distributors => [d], :available_on => 1.week.from_now) + Enterprise.active_distributors.should be_empty + end + it "doesn't show distributors of out of stock products" it "finds active distributors by order cycles" do