From 1bc5a52139b4a33568c7c9bab96bd24a291fc482 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 8 Jan 2024 06:22:16 -0500 Subject: [PATCH] Extract SQL heredoc method for Announcement scopes (#28613) --- app/models/announcement.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/models/announcement.rb b/app/models/announcement.rb index 83a8d3682c..00529766a4 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -21,8 +21,8 @@ class Announcement < ApplicationRecord scope :unpublished, -> { where(published: false) } scope :published, -> { where(published: true) } scope :without_muted, ->(account) { joins("LEFT OUTER JOIN announcement_mutes ON announcement_mutes.announcement_id = announcements.id AND announcement_mutes.account_id = #{account.id}").where(announcement_mutes: { id: nil }) } - scope :chronological, -> { order(Arel.sql('COALESCE(announcements.starts_at, announcements.scheduled_at, announcements.published_at, announcements.created_at) ASC')) } - scope :reverse_chronological, -> { order(Arel.sql('COALESCE(announcements.starts_at, announcements.scheduled_at, announcements.published_at, announcements.created_at) DESC')) } + scope :chronological, -> { order(coalesced_chronology_timestamps.asc) } + scope :reverse_chronological, -> { order(coalesced_chronology_timestamps.desc) } has_many :announcement_mutes, dependent: :destroy has_many :announcement_reactions, dependent: :destroy @@ -33,6 +33,16 @@ class Announcement < ApplicationRecord before_validation :set_published, on: :create + class << self + def coalesced_chronology_timestamps + Arel.sql( + <<~SQL.squish + COALESCE(announcements.starts_at, announcements.scheduled_at, announcements.published_at, announcements.created_at) + SQL + ) + end + end + def to_log_human_identifier text end