mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-05 22:26:07 +00:00
Add OrderCycle distributing_product scope
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user