Add OrderCycle distributing_product scope

This commit is contained in:
Rohan Mitchell
2013-05-28 13:27:52 +10:00
parent 46fd94eded
commit 3bedfc3d1d
2 changed files with 31 additions and 0 deletions

View File

@@ -12,6 +12,10 @@ class OrderCycle < ActiveRecord::Base
scope :active, lambda { where('orders_open_at <= ? AND orders_close_at >= ?', Time.now, Time.now) }
scope :inactive, lambda { where('orders_open_at > ? OR orders_close_at < ?', Time.now, Time.now) }
scope :distributing_product, lambda { |product| joins(:exchanges => :variants).
where('exchanges.sender_id = order_cycles.coordinator_id').
where('spree_variants.id IN (?)', product.variants_including_master.map(&:id)).
select('DISTINCT order_cycles.*') }
def suppliers
self.exchanges.where(:receiver_id => self.coordinator).map(&:sender).uniq

View File

@@ -40,6 +40,33 @@ describe OrderCycle do
OrderCycle.inactive.sort.should == [oc_not_yet_open, oc_already_closed].sort
end
describe "finding order cycles distributing a product" do
it "returns order cycles distributing the product's master variant" do
p = create(:product)
d = create(:distributor_enterprise)
oc = create(:simple_order_cycle, distributors: [d], variants: [p.master])
OrderCycle.distributing_product(p).should == [oc]
end
it "returns order cycles distributing another variant" do
p = create(:product)
v = create(:variant, product: p)
d = create(:distributor_enterprise)
oc = create(:simple_order_cycle, distributors: [d], variants: [v])
OrderCycle.distributing_product(p).should == [oc]
end
it "does not return order cycles supplying but not distributing a product" do
p = create(:product)
s = create(:supplier_enterprise)
oc = create(:simple_order_cycle)
ex = create(:exchange, order_cycle: oc, sender: s, receiver: oc.coordinator)
ex.variants << p.master
OrderCycle.distributing_product(p).should == []
end
end
it "reports its suppliers" do
oc = create(:simple_order_cycle)