2013-11-12 11:47:28 +00:00
|
|
|
class BroadcastMessage < ActiveRecord::Base
|
2016-10-06 21:17:11 +00:00
|
|
|
include CacheMarkdownField
|
2015-02-05 22:20:55 +00:00
|
|
|
include Sortable
|
|
|
|
|
2016-10-06 21:17:11 +00:00
|
|
|
cache_markdown_field :message, pipeline: :broadcast_message
|
|
|
|
|
2015-12-01 23:53:44 +00:00
|
|
|
validates :message, presence: true
|
2013-11-12 12:28:12 +00:00
|
|
|
validates :starts_at, presence: true
|
2015-12-01 23:53:44 +00:00
|
|
|
validates :ends_at, presence: true
|
2013-11-12 13:08:20 +00:00
|
|
|
|
2015-12-01 23:53:44 +00:00
|
|
|
validates :color, allow_blank: true, color: true
|
|
|
|
validates :font, allow_blank: true, color: true
|
2013-12-19 18:11:01 +00:00
|
|
|
|
2015-12-31 22:07:11 +00:00
|
|
|
default_value_for :color, '#E75E40'
|
|
|
|
default_value_for :font, '#FFFFFF'
|
|
|
|
|
2013-11-12 13:08:20 +00:00
|
|
|
def self.current
|
2016-01-29 18:38:57 +00:00
|
|
|
Rails.cache.fetch("broadcast_message_current", expires_in: 1.minute) do
|
2016-01-27 17:20:17 +00:00
|
|
|
where("ends_at > :now AND starts_at <= :now", now: Time.zone.now).last
|
|
|
|
end
|
2015-12-31 22:07:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def active?
|
|
|
|
started? && !ended?
|
|
|
|
end
|
|
|
|
|
|
|
|
def started?
|
|
|
|
Time.zone.now >= starts_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def ended?
|
|
|
|
ends_at < Time.zone.now
|
|
|
|
end
|
2013-11-12 11:47:28 +00:00
|
|
|
end
|