mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-30 21:27:17 +00:00
Find incoming and outgoing exchanges
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user