Add order cycle active and inactive scopes

This commit is contained in:
Rohan Mitchell
2013-01-31 10:14:12 +11:00
parent 8b5aea42b8
commit fe824c2d4e
2 changed files with 12 additions and 0 deletions

View File

@@ -9,6 +9,9 @@ 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 :inactive, lambda { where('orders_open_at > ? OR orders_close_at < ?', Time.now, Time.now) }
def suppliers
self.exchanges.where(:receiver_id => self.coordinator).map(&:sender).uniq

View File

@@ -31,6 +31,15 @@ describe OrderCycle do
oc.exchanges.count.should == 3
end
it "finds active and inactive order cycles" 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
end
it "reports its suppliers" do
oc = create(:simple_order_cycle)