Adds time scopes and methods to poll

This commit is contained in:
kikito
2016-11-02 17:49:34 +01:00
parent 575aa62960
commit 0305d2bd65
2 changed files with 29 additions and 0 deletions

View File

@@ -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

View File

@@ -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