From 00d7fcbfb660d67416ae0bca0c8bb0cfab4451ef Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 8 Mar 2013 16:39:42 +1100 Subject: [PATCH] Enterprise.active_distributors finds active distributors by order cycles --- app/models/enterprise.rb | 7 ++++++- spec/models/enterprises_spec.rb | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 9f58cc2368..7e582e8401 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -27,8 +27,13 @@ class Enterprise < ActiveRecord::Base select('DISTINCT enterprises.*') scope :with_distributed_products_outer, joins('LEFT OUTER JOIN product_distributions ON product_distributions.distributor_id = enterprises.id') + scope :with_order_cycles_outer, + 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.where('product_distributions.product_id IS NOT NULL') + scope :active_distributors, with_distributed_products_outer.with_order_cycles_outer. + where('product_distributions.product_id IS NOT NULL OR order_cycles.id IS NOT NULL'). + select('DISTINCT enterprises.*') def has_supplied_products_on_hand? diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index 18eedec60a..11875f8461 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -25,7 +25,14 @@ describe Enterprise do Enterprise.active_distributors.should == [d] end - it "finds active distributors by order cycles" + it "finds active distributors by order cycles" do + s = create(:supplier_enterprise) + d = create(:distributor_enterprise) + p = create(:product) + create(:simple_order_cycle, suppliers: [s], distributors: [d], variants: [p.master]) + Enterprise.active_distributors.should == [d] + end + it "doesn't show distributors of deleted products" it "doesn't show distributors of unavailable products" it "doesn't show distributors of out of stock products"