From 202951e6d97e808a487499b1e680887309fab61a Mon Sep 17 00:00:00 2001
From: Matt Jankowski <matt@jankowski.online>
Date: Mon, 8 Jan 2024 06:15:36 -0500
Subject: [PATCH] Use Arel `in_order_of` method to generate CASE for
 `DomainBlock.by_severity` (#28617)

---
 app/models/domain_block.rb      | 2 +-
 lib/mastodon/cli/maintenance.rb | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb
index ff23f8fcc..f8058324a 100644
--- a/app/models/domain_block.rb
+++ b/app/models/domain_block.rb
@@ -31,7 +31,7 @@ class DomainBlock < ApplicationRecord
   scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
   scope :with_user_facing_limitations, -> { where(severity: [:silence, :suspend]) }
   scope :with_limitations, -> { where(severity: [:silence, :suspend]).or(where(reject_media: true)) }
-  scope :by_severity, -> { order(Arel.sql('(CASE severity WHEN 0 THEN 1 WHEN 1 THEN 2 WHEN 2 THEN 0 END), domain')) }
+  scope :by_severity, -> { in_order_of(:severity, %w(noop silence suspend)).order(:domain) }
 
   def to_log_human_identifier
     domain
diff --git a/lib/mastodon/cli/maintenance.rb b/lib/mastodon/cli/maintenance.rb
index 7b3a9852a..f37662aa0 100644
--- a/lib/mastodon/cli/maintenance.rb
+++ b/lib/mastodon/cli/maintenance.rb
@@ -41,7 +41,8 @@ module Mastodon::CLI
     class SoftwareUpdate < ApplicationRecord; end
 
     class DomainBlock < ApplicationRecord
-      scope :by_severity, -> { order(Arel.sql('(CASE severity WHEN 0 THEN 1 WHEN 1 THEN 2 WHEN 2 THEN 0 END), domain')) }
+      enum severity: { silence: 0, suspend: 1, noop: 2 }
+      scope :by_severity, -> { in_order_of(:severity, %w(noop silence suspend)).order(:domain) }
     end
 
     class PreviewCard < ApplicationRecord