Adds time scopes and methods to poll
This commit is contained in:
@@ -4,4 +4,20 @@ class Poll < ActiveRecord::Base
|
||||
has_many :questions
|
||||
|
||||
validates :name, presence: true
|
||||
|
||||
scope :current, -> { where('starts_at <= ? and ? <= ends_at', Time.now, Time.now) }
|
||||
scope :incoming, -> { where('? < starts_at', Time.now) }
|
||||
scope :expired, -> { where('ends_at < ?', Time.now) }
|
||||
|
||||
def current?(timestamp = DateTime.now)
|
||||
starts_at <= timestamp && timestamp <= ends_at
|
||||
end
|
||||
|
||||
def incoming?(timestamp = DateTime.now)
|
||||
timestamp < starts_at
|
||||
end
|
||||
|
||||
def expired?(timestamp = DateTime.now)
|
||||
ends_at < timestamp
|
||||
end
|
||||
end
|
||||
|
||||
@@ -265,6 +265,19 @@ FactoryGirl.define do
|
||||
|
||||
factory :poll do
|
||||
sequence(:name) { |n| "Poll #{n}" }
|
||||
|
||||
starts_at { 1.month.ago }
|
||||
ends_at { 1.month.from_now }
|
||||
|
||||
trait :incoming do
|
||||
starts_at { 2.days.from_now }
|
||||
ends_at { 1.month.from_now }
|
||||
end
|
||||
|
||||
trait :expired do
|
||||
starts_at { 1.month.ago }
|
||||
ends_at { 15.days.ago }
|
||||
end
|
||||
end
|
||||
|
||||
factory :poll_officer, class: 'Poll::Officer' do
|
||||
|
||||
Reference in New Issue
Block a user