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
This commit is contained in:
parent
6f3e89f4ed
commit
58f9c303aa
7 changed files with 38 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@
|
|||
<p>
|
||||
<%= broadcast.processed_html %>
|
||||
</p>
|
||||
<h3>Last Active On: <%= broadcast.active_status_updated_at&.strftime("%b %d, %Y %H:%M UTC") %></h3>
|
||||
<h3>Status:</h3>
|
||||
<h3>
|
||||
<span class="badge badge-<%= broadcast.active? ? "success" : "warning" %>">
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
class AddActiveStatusUpdatedAtToBroadcasts < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
add_column :broadcasts, :active_status_updated_at, :datetime
|
||||
end
|
||||
end
|
||||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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" }
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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) }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue