From 85e4b3970c06b3f6fefa645d674e6fbba9cdebc0 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 13 Mar 2015 14:29:55 +1100 Subject: [PATCH] Adding an 'involved' scope to exchanges --- app/models/exchange.rb | 1 + spec/models/exchange_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/app/models/exchange.rb b/app/models/exchange.rb index c491748d5d..1b226d269a 100644 --- a/app/models/exchange.rb +++ b/app/models/exchange.rb @@ -22,6 +22,7 @@ class Exchange < ActiveRecord::Base scope :to_enterprise, lambda { |enterprise| where(receiver_id: enterprise) } scope :from_enterprises, lambda { |enterprises| where('exchanges.sender_id IN (?)', enterprises) } scope :to_enterprises, lambda { |enterprises| where('exchanges.receiver_id IN (?)', enterprises) } + scope :involving, lambda { |enterprises| where('exchanges.receiver_id IN (?) OR exchanges.sender_id IN (?)', enterprises, enterprises).select('DISTINCT exchanges.*') } scope :supplying_to, lambda { |distributor| where('exchanges.incoming OR exchanges.receiver_id = ?', distributor) } scope :with_variant, lambda { |variant| joins(:exchange_variants).where('exchange_variants.variant_id = ?', variant) } scope :with_any_variant, lambda { |variants| joins(:exchange_variants).where('exchange_variants.variant_id IN (?)', variants).select('DISTINCT exchanges.*') } diff --git a/spec/models/exchange_spec.rb b/spec/models/exchange_spec.rb index 0dfceadbad..82f360aa16 100644 --- a/spec/models/exchange_spec.rb +++ b/spec/models/exchange_spec.rb @@ -177,6 +177,12 @@ describe Exchange do Exchange.to_enterprises([coordinator]).should == [incoming_exchange] Exchange.to_enterprises([coordinator, distributor]).sort.should == [incoming_exchange, outgoing_exchange].sort end + + it "finds exchanges involving any of a number of enterprises" do + Exchange.involving([supplier]).should == [incoming_exchange] + Exchange.involving([coordinator]).sort.should == [incoming_exchange, outgoing_exchange].sort + Exchange.involving([distributor]).should == [outgoing_exchange] + end end describe "finding exchanges supplying to a distributor" do