Adding an 'involved' scope to exchanges

This commit is contained in:
Rob Harrington
2015-03-13 14:29:55 +11:00
parent 75a37e16e3
commit 85e4b3970c
2 changed files with 7 additions and 0 deletions

View File

@@ -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.*') }

View File

@@ -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