Test whether exchanges are incoming

This commit is contained in:
Rohan Mitchell
2013-08-16 15:42:06 +10:00
parent 9563aad9fa
commit dfd1a89975
2 changed files with 22 additions and 0 deletions

View File

@@ -18,4 +18,8 @@ class Exchange < ActiveRecord::Base
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')
scope :with_variant, lambda { |variant| joins(:exchange_variants).where('exchange_variants.variant_id = ?', variant) }
def incoming?
receiver == order_cycle.coordinator
end
end

View File

@@ -44,6 +44,24 @@ describe Exchange do
e.enterprise_fees.count.should == 1
end
describe "reporting whether it is an incoming exchange" 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 "returns true for incoming exchanges" do
incoming_exchange.should be_incoming
end
it "returns false for outgoing exchanges" do
outgoing_exchange.should_not be_incoming
end
end
describe "scopes" do
let(:supplier) { create(:supplier_enterprise) }
let(:coordinator) { create(:distributor_enterprise) }