Correct Poll current/expired/incoming scopes and helper functions

This commit is contained in:
Bertocq
2017-09-29 11:50:19 +02:00
parent 583fb65c79
commit 07592f6b0d

View File

@@ -16,23 +16,23 @@ class Poll < ActiveRecord::Base
validate :date_range
scope :current, -> { where('starts_at <= ? and ? <= ends_at', Time.current, Time.current) }
scope :incoming, -> { where('? < starts_at', Time.current) }
scope :expired, -> { where('ends_at < ?', Time.current) }
scope :current, -> { where('starts_at <= ? and ? <= ends_at', Date.current.beginning_of_day, Date.current.beginning_of_day) }
scope :incoming, -> { where('? < starts_at', Date.current.beginning_of_day) }
scope :expired, -> { where('ends_at < ?', Date.current.beginning_of_day) }
scope :published, -> { where('published = ?', true) }
scope :by_geozone_id, ->(geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) }
scope :sort_for_list, -> { order(:geozone_restricted, :starts_at, :name) }
def current?(timestamp = DateTime.current)
def current?(timestamp = Date.current.beginning_of_day)
starts_at <= timestamp && timestamp <= ends_at
end
def incoming?(timestamp = DateTime.current)
def incoming?(timestamp = Date.current.beginning_of_day)
timestamp < starts_at
end
def expired?(timestamp = DateTime.current)
def expired?(timestamp = Date.current.beginning_of_day)
ends_at < timestamp
end