docbrown/app/models/broadcast.rb
Julianna Tetreault 58f9c303aa
Add active_status_updated_at to the Broadcasts Table (#8451) [deploy]
* Add last_active_at datetime to Broadcasts table

* Add last_active_at to Broadcast index view

* Add callback to Broadcast.rb to update last_active_at

* Add a data_update script to backfill the last_active_at column on the Broadcasts table

* Add a spec for updating the last_active_at timestamp in models/broadcast_spec.rb

* Format last_active_at timestamp via strftime in index.html.erb

* Remove data_update_script: backfill_last_active_at_for_broadcasts

* Remove ActiveRecord::Dirty method from broadcast.rb

* Adjust callback in broadcast.rb and last_active_at timestamp format in index.html.erb

* Adjust time in #update_last_active_at and add the safe navigation operator to index.html.erb
  * Accounts for nil last_active_at values in the view
  * Uses Time.current in place of Time.zone.now

* Remove last_active_at param from Broadcasts::Controller

* Add spec to broadcasts_spec.rb to test the updated attribute via a request

* Adjust current_time in broadcasts_spec.rb

* Adjust Timecop.freeze in broadcasts_spec.rb to resolve Travis faliure (fingers crossed)

* Adjust expectation to compare against last_active_at timestamp rather than current time

* Adds Timecop back and stores last_active_at timestamp to test against

* Rename last_active_at to active_status_updated_at on Broadcasts table
  - Renames last_active_at to active_status_updated_at in specs
  - Renames last_active_at to active_status_updated_at in model
  - Renames last_active_at to active_status_updated_at in view
  - Adjusts spelling in spec descriptions
  - Rewords active_status_updated_at in view to Last Active On
2020-06-16 13:04:14 -06:00

41 lines
1.5 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
before_save :update_active_status_updated_at, if: :will_save_change_to_active?
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
def update_active_status_updated_at
self.active_status_updated_at = Time.current
end
end