From 58f9c303aab85e333ef3e4f030bcc477c4b6013d Mon Sep 17 00:00:00 2001 From: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com> Date: Tue, 16 Jun 2020 13:04:14 -0600 Subject: [PATCH] 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 --- app/models/broadcast.rb | 6 ++++++ app/views/internal/broadcasts/index.html.erb | 1 + ...3_add_active_status_updated_at_to_broadcasts.rb | 5 +++++ db/schema.rb | 1 + spec/factories/broadcasts.rb | 1 + spec/models/broadcast_spec.rb | 10 ++++++++++ spec/requests/internal/broadcasts_spec.rb | 14 ++++++++++++++ 7 files changed, 38 insertions(+) create mode 100644 db/migrate/20200609195523_add_active_status_updated_at_to_broadcasts.rb 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) }