Read about our upcoming Code of Conduct on this issue

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

5
6
  cache_markdown_field :message, pipeline: :broadcast_message

Robert Speicher's avatar
Robert Speicher committed
7
  validates :message,   presence: true
8
  validates :starts_at, presence: true
Robert Speicher's avatar
Robert Speicher committed
9
  validates :ends_at,   presence: true
10

Robert Speicher's avatar
Robert Speicher committed
11
12
  validates :color, allow_blank: true, color: true
  validates :font,  allow_blank: true, color: true
13

14
15
16
  default_value_for :color, '#E75E40'
  default_value_for :font,  '#FFFFFF'

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

  def active?
    started? && !ended?
  end

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

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