From 08dd11f8d4c281b8b564df0548d3d991d3eaee68 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 6 Jan 2025 03:18:05 -0500 Subject: [PATCH] Use `in_order_of` with `filter: false` in `AccountSummary.localized` (#33446) --- app/models/account_summary.rb | 2 +- spec/models/account_summary_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 spec/models/account_summary_spec.rb diff --git a/app/models/account_summary.rb b/app/models/account_summary.rb index 327c0ef305..7522a70193 100644 --- a/app/models/account_summary.rb +++ b/app/models/account_summary.rb @@ -17,6 +17,6 @@ class AccountSummary < ApplicationRecord has_many :follow_recommendation_suppressions, primary_key: :account_id, foreign_key: :account_id, inverse_of: false, dependent: nil scope :safe, -> { where(sensitive: false) } - scope :localized, ->(locale) { order(Arel::Nodes::Case.new.when(arel_table[:language].eq(locale)).then(1).else(0).desc) } + scope :localized, ->(locale) { in_order_of(:language, [locale], filter: false) } scope :filtered, -> { where.missing(:follow_recommendation_suppressions) } end diff --git a/spec/models/account_summary_spec.rb b/spec/models/account_summary_spec.rb new file mode 100644 index 0000000000..cede8cda55 --- /dev/null +++ b/spec/models/account_summary_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe AccountSummary do + describe 'Scopes' do + describe '.localized' do + let(:first) { Fabricate :account } + let(:last) { Fabricate :account } + + before do + Fabricate :status, account: first, language: 'en' + Fabricate :status, account: last, language: 'es' + described_class.refresh + end + + it 'returns records in order of language' do + expect(described_class.localized('en')) + .to contain_exactly( + have_attributes(account_id: first.id, language: 'en'), + have_attributes(account_id: last.id, language: 'es') + ) + end + end + end +end