Find incoming and outgoing exchanges

This commit is contained in:
Rohan Mitchell
2013-08-16 10:05:04 +10:00
parent 715ec9b21a
commit 552cecb2e0
2 changed files with 21 additions and 0 deletions

View File

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

View File

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