broadcast_message.rb 1007 Bytes
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4 5
# == Schema Information
#
# Table name: broadcast_messages
#
#  id         :integer          not null, primary key
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6
#  message    :text             not null
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7 8
#  starts_at  :datetime
#  ends_at    :datetime
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
9 10
#  created_at :datetime
#  updated_at :datetime
11 12
#  color      :string(255)
#  font       :string(255)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13 14
#

15
class BroadcastMessage < ActiveRecord::Base
16 17
  include Sortable

18
  validates :message,   presence: true
19
  validates :starts_at, presence: true
20
  validates :ends_at,   presence: true
21

22 23
  validates :color, allow_blank: true, color: true
  validates :font,  allow_blank: true, color: true
24

25 26 27
  default_value_for :color, '#E75E40'
  default_value_for :font,  '#FFFFFF'

28
  def self.current
Josh Frye's avatar
Josh Frye committed
29
    Rails.cache.fetch("broadcast_message_current", expires_in: 1.minute) do
30 31
      where("ends_at > :now AND starts_at <= :now", now: Time.zone.now).last
    end
32 33 34 35 36 37 38 39 40 41 42 43 44
  end

  def active?
    started? && !ended?
  end

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

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