diff --git a/app/models/broadcast.rb b/app/models/broadcast.rb index 22a59932a..55e394f77 100644 --- a/app/models/broadcast.rb +++ b/app/models/broadcast.rb @@ -10,6 +10,8 @@ class Broadcast < ApplicationRecord 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") } @@ -32,4 +34,8 @@ class Broadcast < ApplicationRecord 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 diff --git a/app/views/internal/broadcasts/index.html.erb b/app/views/internal/broadcasts/index.html.erb index 1493fdc5f..3b3ab014a 100644 --- a/app/views/internal/broadcasts/index.html.erb +++ b/app/views/internal/broadcasts/index.html.erb @@ -31,6 +31,7 @@

<%= broadcast.processed_html %>

+

Last Active On: <%= broadcast.active_status_updated_at&.strftime("%b %d, %Y %H:%M UTC") %>

Status:

"> diff --git a/db/migrate/20200609195523_add_active_status_updated_at_to_broadcasts.rb b/db/migrate/20200609195523_add_active_status_updated_at_to_broadcasts.rb new file mode 100644 index 000000000..a185a1a9b --- /dev/null +++ b/db/migrate/20200609195523_add_active_status_updated_at_to_broadcasts.rb @@ -0,0 +1,5 @@ +class AddActiveStatusUpdatedAtToBroadcasts < ActiveRecord::Migration[6.0] + def change + add_column :broadcasts, :active_status_updated_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 6805d09d8..18de6a23d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -267,6 +267,7 @@ ActiveRecord::Schema.define(version: 2020_06_15_213003) do create_table "broadcasts", id: :serial, force: :cascade do |t| t.boolean "active", default: false + t.datetime "active_status_updated_at" t.string "banner_style" t.text "body_markdown" t.datetime "created_at" diff --git a/spec/factories/broadcasts.rb b/spec/factories/broadcasts.rb index 0920bfadb..7b5787c27 100644 --- a/spec/factories/broadcasts.rb +++ b/spec/factories/broadcasts.rb @@ -1,6 +1,7 @@ FactoryBot.define do factory :broadcast do active { true } + active_status_updated_at { 2.days.ago } factory :set_up_profile_broadcast do title { "Welcome Notification: set_up_profile" } diff --git a/spec/models/broadcast_spec.rb b/spec/models/broadcast_spec.rb index cc4b85e81..86323400b 100644 --- a/spec/models/broadcast_spec.rb +++ b/spec/models/broadcast_spec.rb @@ -18,4 +18,14 @@ RSpec.describe Broadcast, type: :model do expect(inactive_broadcast).not_to be_valid expect(inactive_broadcast.errors.full_messages.join).to include("You can only have one active announcement broadcast") end + + it "updates the Broadcast's active_status_updated_at timestamp" do + Timecop.freeze(Time.current) do + current_time = Time.zone.now + broadcast = create(:welcome_broadcast, active: false) + expect(broadcast.active_status_updated_at).to eq(2.days.ago) + broadcast.update(active: true) + expect(broadcast.active_status_updated_at).to eq current_time + end + end end diff --git a/spec/requests/internal/broadcasts_spec.rb b/spec/requests/internal/broadcasts_spec.rb index 5bdd78f95..ff1886b5f 100644 --- a/spec/requests/internal/broadcasts_spec.rb +++ b/spec/requests/internal/broadcasts_spec.rb @@ -48,6 +48,20 @@ RSpec.describe "/internal/broadcasts", type: :request do end end + describe "PUT /internal/broadcasts" do + let!(:broadcast) { create(:welcome_broadcast, active: false) } + + it "updates the Broadcast's active_status_updated_at timestamp" do + old_time = broadcast.active_status_updated_at + Timecop.freeze(Time.current) do + expect do + put "/internal/broadcasts/#{broadcast.id}", params: params + end.to change { broadcast.reload.active }.from(false).to(true) + expect(broadcast.active_status_updated_at).not_to eq(old_time) + end + end + end + describe "DELETE /internal/broadcasts/:id" do let!(:broadcast) { create(:welcome_broadcast) }