Rely on pluck for the SELECT in RuleTranslation.languages (#35826)

This commit is contained in:
Matt Jankowski 2025-08-22 19:58:40 -04:00 committed by GitHub
parent abe5413638
commit 20bc34ca52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 1 deletions

View File

@ -22,6 +22,6 @@ class RuleTranslation < ApplicationRecord
scope :by_language_length, -> { order(Arel.sql('LENGTH(LANGUAGE)').desc) } scope :by_language_length, -> { order(Arel.sql('LENGTH(LANGUAGE)').desc) }
def self.languages def self.languages
RuleTranslation.joins(:rule).merge(Rule.kept).select(:language).distinct.pluck(:language).sort joins(:rule).merge(Rule.kept).distinct.pluck(:language).sort
end end
end end

View File

@ -52,4 +52,22 @@ RSpec.describe RuleTranslation do
end end
end end
end end
describe '.languages' do
let(:discarded_rule) { Fabricate :rule, deleted_at: 5.days.ago }
let(:kept_rule) { Fabricate :rule }
before do
Fabricate :rule_translation, rule: discarded_rule, language: 'en'
Fabricate :rule_translation, rule: kept_rule, language: 'es'
Fabricate :rule_translation, language: 'fr'
Fabricate :rule_translation, language: 'es'
end
it 'returns ordered distinct languages connected to non-discarded rules' do
expect(described_class.languages)
.to be_an(Array)
.and eq(%w(es fr))
end
end
end end