From 552cecb2e0ee65c84f91061e9a95f4cbf28a07c7 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 16 Aug 2013 10:05:04 +1000 Subject: [PATCH] Find incoming and outgoing exchanges --- app/models/exchange.rb | 3 +++ spec/models/exchange_spec.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/models/exchange.rb b/app/models/exchange.rb index 362edaf102..fe7f04933c 100644 --- a/app/models/exchange.rb +++ b/app/models/exchange.rb @@ -14,4 +14,7 @@ class Exchange < ActiveRecord::Base validates_uniqueness_of :sender_id, :scope => [:order_cycle_id, :receiver_id] accepts_nested_attributes_for :variants + + scope :incoming, joins(:order_cycle).where('exchanges.receiver_id = order_cycles.coordinator_id') + scope :outgoing, joins(:order_cycle).where('exchanges.sender_id = order_cycles.coordinator_id') end diff --git a/spec/models/exchange_spec.rb b/spec/models/exchange_spec.rb index 0b87d69c50..523ee23e5e 100644 --- a/spec/models/exchange_spec.rb +++ b/spec/models/exchange_spec.rb @@ -43,4 +43,22 @@ describe Exchange do e.exchange_fees.create(:enterprise_fee => f) e.enterprise_fees.count.should == 1 end + + describe "scopes" do + let(:supplier) { create(:supplier_enterprise) } + let(:coordinator) { create(:distributor_enterprise) } + let(:distributor) { create(:distributor_enterprise) } + let(:oc) { create(:simple_order_cycle, coordinator: coordinator) } + + let!(:incoming_exchange) { oc.exchanges.create! sender: supplier, receiver: coordinator } + let!(:outgoing_exchange) { oc.exchanges.create! sender: coordinator, receiver: distributor } + + it "finds incoming exchanges" do + Exchange.incoming.should == [incoming_exchange] + end + + it "finds outgoing exchanges" do + Exchange.outgoing.should == [outgoing_exchange] + end + end end