docbrown/app/models/broadcast.rb
Vaidehi Joshi defab11a4d
Provide default crayon styles when creating banners (#8361) [deploy]
* Provide default crayon styles when creating banners

* Import crayons styles into /internal pages.
* Allow admins to use crayons banner styles when creating broadcasts.
* Preview banner styles with crayons.

* Use constant and helper for broadcast banner styles

* Add VALID_BANNER_STYLES frozen constant to Broadcast class.
* Add banner_style helper for determining banner style class.
2020-06-09 11:26:14 -07:00

35 lines
1.3 KiB
Ruby

class Broadcast < ApplicationRecord
VALID_BANNER_STYLES = %w[default brand success warning error].freeze
resourcify
has_many :notifications, as: :notifiable, inverse_of: :notifiable
validates :title, uniqueness: { scope: :type_of }, presence: true
validates :type_of, :processed_html, presence: true
validates :type_of, inclusion: { in: %w[Announcement Welcome] }
validates :banner_style, inclusion: { in: VALID_BANNER_STYLES }, allow_blank: true
validate :single_active_announcement_broadcast
scope :active, -> { where(active: true) }
scope :announcement, -> { where(type_of: "Announcement") }
scope :welcome, -> { where(type_of: "Welcome") }
def get_inner_body(content)
Nokogiri::HTML(content).at("body").inner_html
end
private
def single_active_announcement_broadcast
# Only add errors if we are trying to modify an announcement
# broadcast while another announcement broadcast is already
# active to ensure that only one can be active at a time.
active_broadcasts = Broadcast.announcement.active
first_broadcast = active_broadcasts.order(id: :asc).limit(1)
return unless active &&
type_of == "Announcement" &&
![nil, id].include?(first_broadcast.pick(:id))
errors.add(:base, "You can only have one active announcement broadcast")
end
end