Add OrderCycle#distributed_variants

This commit is contained in:
Rohan Mitchell
2013-05-28 14:01:04 +10:00
parent fbac41b060
commit 9ef7da1339
2 changed files with 17 additions and 2 deletions

View File

@@ -5,6 +5,9 @@ class OrderCycle < ActiveRecord::Base
has_many :exchanges, :dependent => :destroy
# TODO: DRY the incoming/outgoing clause used in several cases below
# See Spree::Product definition, scopes variants and variants_including_master
# This will require these accessors to be renamed
attr_accessor :incoming_exchanges, :outgoing_exchanges
validates_presence_of :name, :coordinator_id
@@ -29,6 +32,10 @@ class OrderCycle < ActiveRecord::Base
self.exchanges.map(&:variants).flatten.uniq
end
def distributed_variants
self.exchanges.where(:sender_id => self.coordinator).map(&:variants).flatten.uniq
end
def products
self.variants.map(&:product).uniq
end

View File

@@ -103,15 +103,19 @@ describe OrderCycle do
before(:each) do
@oc = create(:simple_order_cycle)
e0 = create(:exchange,
order_cycle: @oc, sender: create(:enterprise), receiver: @oc.coordinator)
e1 = create(:exchange,
order_cycle: @oc, sender: @oc.coordinator, receiver: create(:enterprise))
e2 = create(:exchange,
order_cycle: @oc, sender: @oc.coordinator, receiver: create(:enterprise))
@p0 = create(:product)
@p1 = create(:product)
@p2 = create(:product)
@p2_v = create(:variant, product: @p2)
e0.variants << @p0.master
e1.variants << @p1.master
e1.variants << @p2.master
e1.variants << @p2_v
@@ -119,11 +123,15 @@ describe OrderCycle do
end
it "reports on the variants exchanged in the order cycle" do
@oc.variants.sort.should == [@p1.master, @p2.master, @p2_v].sort
@oc.variants.sort.should == [@p0.master, @p1.master, @p2.master, @p2_v].sort
end
it "reports on the variants distributed in the order cycle" do
@oc.distributed_variants.sort.should == [@p1.master, @p2.master, @p2_v].sort
end
it "reports on the products exchanged in the order cycle" do
@oc.products.sort.should == [@p1, @p2]
@oc.products.sort.should == [@p0, @p1, @p2]
end
end
end