From dfd1a89975e3f1d5c11e05563034d8a040430f96 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 16 Aug 2013 15:42:06 +1000 Subject: [PATCH] Test whether exchanges are incoming --- app/models/exchange.rb | 4 ++++ spec/models/exchange_spec.rb | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/models/exchange.rb b/app/models/exchange.rb index 8ad87d5302..a8f9ef9175 100644 --- a/app/models/exchange.rb +++ b/app/models/exchange.rb @@ -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 diff --git a/spec/models/exchange_spec.rb b/spec/models/exchange_spec.rb index ff3829ec67..5f342c6ab3 100644 --- a/spec/models/exchange_spec.rb +++ b/spec/models/exchange_spec.rb @@ -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) }