mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Add some more open/close time scopes to order cycles
This commit is contained in:
@@ -14,6 +14,8 @@ class OrderCycle < ActiveRecord::Base
|
||||
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 :upcoming, lambda { where('order_cycles.orders_open_at > ?', Time.now) }
|
||||
scope :closed, lambda { where('order_cycles.orders_close_at < ?', Time.now) }
|
||||
|
||||
scope :distributing_product, lambda { |product|
|
||||
joins(:exchanges => :variants).
|
||||
@@ -25,13 +27,9 @@ class OrderCycle < ActiveRecord::Base
|
||||
joins(:exchanges).merge(Exchange.outgoing).where('exchanges.receiver_id = ?', distributor)
|
||||
}
|
||||
|
||||
scope :most_recently_closed, lambda {
|
||||
where('order_cycles.orders_close_at < ?', Time.now).order('order_cycles.orders_close_at DESC')
|
||||
}
|
||||
|
||||
scope :soonest_opening, lambda {
|
||||
where('order_cycles.orders_open_at > ?', Time.now).order('order_cycles.orders_open_at ASC')
|
||||
}
|
||||
scope :soonest_closing, active.order('order_cycles.orders_close_at ASC')
|
||||
scope :most_recently_closed, closed.order('order_cycles.orders_close_at DESC')
|
||||
scope :soonest_opening, upcoming.order('order_cycles.orders_open_at ASC')
|
||||
|
||||
|
||||
scope :managed_by, lambda { |user|
|
||||
|
||||
@@ -30,13 +30,15 @@ describe OrderCycle do
|
||||
oc.exchanges.count.should == 3
|
||||
end
|
||||
|
||||
it "finds active and inactive order cycles" do
|
||||
it "finds order cycles in various stages of their lifecycle" do
|
||||
oc_active = create(:simple_order_cycle, orders_open_at: 1.week.ago, orders_close_at: 1.week.from_now)
|
||||
oc_not_yet_open = create(:simple_order_cycle, orders_open_at: 1.week.from_now, orders_close_at: 2.weeks.from_now)
|
||||
oc_already_closed = create(:simple_order_cycle, orders_open_at: 2.weeks.ago, orders_close_at: 1.week.ago)
|
||||
|
||||
OrderCycle.active.should == [oc_active]
|
||||
OrderCycle.inactive.sort.should == [oc_not_yet_open, oc_already_closed].sort
|
||||
OrderCycle.upcoming.should == [oc_not_yet_open]
|
||||
OrderCycle.closed.should == [oc_already_closed]
|
||||
end
|
||||
|
||||
it "finds order cycles accessible by a user" do
|
||||
@@ -97,6 +99,14 @@ describe OrderCycle do
|
||||
OrderCycle.soonest_opening.should == [oc2, oc1]
|
||||
end
|
||||
|
||||
it "finds the soonest closing order cycles" do
|
||||
oc1 = create(:order_cycle, orders_close_at: 2.hours.ago)
|
||||
oc2 = create(:order_cycle, orders_close_at: 2.hour.from_now)
|
||||
oc3 = create(:order_cycle, orders_close_at: 1.hour.from_now)
|
||||
|
||||
OrderCycle.soonest_closing.should == [oc3, oc2]
|
||||
end
|
||||
|
||||
describe "finding order cycles with a particular distributor" do
|
||||
let(:c) { create(:supplier_enterprise) }
|
||||
let(:d) { create(:distributor_enterprise) }
|
||||
|
||||
Reference in New Issue
Block a user