From 0305d2bd65229e6a1213a1361de5e22d3cbcdf70 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 2 Nov 2016 17:49:34 +0100 Subject: [PATCH] Adds time scopes and methods to poll --- app/models/poll.rb | 16 ++++++++++++++++ spec/factories.rb | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/app/models/poll.rb b/app/models/poll.rb index 11ac6ccb8..cc731f0b5 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -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 diff --git a/spec/factories.rb b/spec/factories.rb index abeff338c..7f9b0b63e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -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