Change rule translation interface to display english name and populate empty translations (#35170)

This commit is contained in:
Claire 2025-06-25 12:02:19 +02:00 committed by GitHub
parent 72f2f35bfb
commit 0a7418e6d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 9 additions and 2 deletions

View File

@ -17,6 +17,9 @@ module Admin
def edit def edit
authorize @rule, :update? authorize @rule, :update?
missing_languages = RuleTranslation.languages - @rule.translations.pluck(:language)
missing_languages.each { |lang| @rule.translations.build(language: lang) }
end end
def create def create

View File

@ -20,7 +20,7 @@ class Rule < ApplicationRecord
self.discard_column = :deleted_at self.discard_column = :deleted_at
has_many :translations, inverse_of: :rule, class_name: 'RuleTranslation', dependent: :destroy has_many :translations, inverse_of: :rule, class_name: 'RuleTranslation', dependent: :destroy
accepts_nested_attributes_for :translations, reject_if: :all_blank, allow_destroy: true accepts_nested_attributes_for :translations, reject_if: ->(attributes) { attributes['text'].blank? }, allow_destroy: true
validates :text, presence: true, length: { maximum: TEXT_SIZE_LIMIT } validates :text, presence: true, length: { maximum: TEXT_SIZE_LIMIT }

View File

@ -20,4 +20,8 @@ class RuleTranslation < ApplicationRecord
scope :for_locale, ->(locale) { where(language: I18n::Locale::Tag.tag(locale).to_a.first) } scope :for_locale, ->(locale) { where(language: I18n::Locale::Tag.tag(locale).to_a.first) }
scope :by_language_length, -> { order(Arel.sql('LENGTH(LANGUAGE)').desc) } scope :by_language_length, -> { order(Arel.sql('LENGTH(LANGUAGE)').desc) }
def self.languages
RuleTranslation.joins(:rule).merge(Rule.kept).select(:language).distinct.pluck(:language).sort
end
end end

View File

@ -5,7 +5,7 @@
= f.input :language, = f.input :language,
collection: ui_languages, collection: ui_languages,
include_blank: false, include_blank: false,
label_method: ->(locale) { native_locale_name(locale) } label_method: ->(locale) { "#{native_locale_name(locale)} (#{standard_locale_name(locale)})" }
.fields-row__column.fields-group .fields-row__column.fields-group
= f.hidden_field :id if f.object&.persisted? # Required so Rails doesn't put the field outside of the <tr/> = f.hidden_field :id if f.object&.persisted? # Required so Rails doesn't put the field outside of the <tr/>