mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-15 23:57:48 +00:00
Add scope: OrderCycle.with_distributor
This commit is contained in:
@@ -24,9 +24,8 @@ module OrderCyclesHelper
|
||||
|
||||
def order_cycle_options
|
||||
@order_cycles.
|
||||
select { |oc| oc.distributors.include? current_distributor }.
|
||||
map { |oc| [order_cycle_close_to_s(oc.orders_close_at),
|
||||
oc.id] }
|
||||
with_distributor(current_distributor).
|
||||
map { |oc| [order_cycle_close_to_s(oc.orders_close_at), oc.id] }
|
||||
end
|
||||
|
||||
def order_cycle_close_to_s(orders_close_at)
|
||||
|
||||
@@ -11,15 +11,21 @@ class OrderCycle < ActiveRecord::Base
|
||||
|
||||
validates_presence_of :name, :coordinator_id
|
||||
|
||||
scope :active, lambda { where('orders_open_at <= ? AND orders_close_at >= ?', Time.now, Time.now) }
|
||||
scope :active_or_complete, lambda { where('orders_open_at <= ?', Time.now) }
|
||||
scope :inactive, lambda { where('orders_open_at > ? OR orders_close_at < ?', Time.now, Time.now) }
|
||||
scope :active, lambda { where('order_cycles.orders_open_at <= ? AND order_cycles.orders_close_at >= ?', Time.now, Time.now) }
|
||||
scope :active_or_complete, lambda { where('order_cycles.orders_open_at <= ?', Time.now) }
|
||||
scope :inactive, lambda { where('order_cycles.orders_open_at > ? OR order_cycles.orders_close_at < ?', Time.now, Time.now) }
|
||||
|
||||
scope :distributing_product, lambda { |product| joins(:exchanges => :variants).
|
||||
where('exchanges.sender_id = order_cycles.coordinator_id').
|
||||
scope :distributing_product, lambda { |product|
|
||||
joins(:exchanges => :variants).
|
||||
merge(Exchange.outgoing).
|
||||
where('spree_variants.id IN (?)', product.variants_including_master.map(&:id)).
|
||||
select('DISTINCT order_cycles.*') }
|
||||
|
||||
scope :with_distributor, lambda { |distributor|
|
||||
joins(:exchanges).merge(Exchange.outgoing).where('exchanges.receiver_id = ?', distributor)
|
||||
}
|
||||
|
||||
|
||||
scope :managed_by, lambda { |user|
|
||||
if user.has_spree_role?('admin')
|
||||
scoped
|
||||
|
||||
@@ -66,6 +66,26 @@ describe OrderCycle do
|
||||
end
|
||||
end
|
||||
|
||||
describe "finding order cycles with a particular distributor" do
|
||||
let(:c) { create(:supplier_enterprise) }
|
||||
let(:d) { create(:distributor_enterprise) }
|
||||
|
||||
it "returns order cycles with that distributor" do
|
||||
oc = create(:simple_order_cycle, coordinator: c, distributors: [d])
|
||||
OrderCycle.with_distributor(d).should == [oc]
|
||||
end
|
||||
|
||||
it "does not return order cycles with that enterprise as supplier" do
|
||||
oc = create(:simple_order_cycle, coordinator: c, suppliers: [d])
|
||||
OrderCycle.with_distributor(d).should == []
|
||||
end
|
||||
|
||||
it "does not return order cycles without that distributor" do
|
||||
oc = create(:simple_order_cycle, coordinator: c)
|
||||
OrderCycle.with_distributor(d).should == []
|
||||
end
|
||||
end
|
||||
|
||||
it "reports its suppliers" do
|
||||
oc = create(:simple_order_cycle)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user