BigW Consortium Gitlab

broadcast_message.rb 698 Bytes
Newer Older
1
class BroadcastMessage < ActiveRecord::Base
2 3
  include Sortable

4
  validates :message,   presence: true
5
  validates :starts_at, presence: true
6
  validates :ends_at,   presence: true
7

8 9
  validates :color, allow_blank: true, color: true
  validates :font,  allow_blank: true, color: true
10

11 12 13
  default_value_for :color, '#E75E40'
  default_value_for :font,  '#FFFFFF'

14
  def self.current
Josh Frye committed
15
    Rails.cache.fetch("broadcast_message_current", expires_in: 1.minute) do
16 17
      where("ends_at > :now AND starts_at <= :now", now: Time.zone.now).last
    end
18 19 20 21 22 23 24 25 26 27 28 29 30
  end

  def active?
    started? && !ended?
  end

  def started?
    Time.zone.now >= starts_at
  end

  def ended?
    ends_at < Time.zone.now
  end
31
end