diff --git a/app/controllers/admin/rules_controller.rb b/app/controllers/admin/rules_controller.rb index 58d52e629..f355aa230 100644 --- a/app/controllers/admin/rules_controller.rb +++ b/app/controllers/admin/rules_controller.rb @@ -17,6 +17,9 @@ module Admin def edit authorize @rule, :update? + + missing_languages = RuleTranslation.languages - @rule.translations.pluck(:language) + missing_languages.each { |lang| @rule.translations.build(language: lang) } end def create diff --git a/app/models/rule.rb b/app/models/rule.rb index fc262d6b2..51f856bb5 100644 --- a/app/models/rule.rb +++ b/app/models/rule.rb @@ -20,7 +20,7 @@ class Rule < ApplicationRecord self.discard_column = :deleted_at 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 } diff --git a/app/models/rule_translation.rb b/app/models/rule_translation.rb index 71e5773e5..ccf0d4ba3 100644 --- a/app/models/rule_translation.rb +++ b/app/models/rule_translation.rb @@ -20,4 +20,8 @@ class RuleTranslation < ApplicationRecord scope :for_locale, ->(locale) { where(language: I18n::Locale::Tag.tag(locale).to_a.first) } 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 diff --git a/app/views/admin/rules/_translation_fields.html.haml b/app/views/admin/rules/_translation_fields.html.haml index bf8d81722..95bb568ae 100644 --- a/app/views/admin/rules/_translation_fields.html.haml +++ b/app/views/admin/rules/_translation_fields.html.haml @@ -5,7 +5,7 @@ = f.input :language, collection: ui_languages, 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 = f.hidden_field :id if f.object&.persisted? # Required so Rails doesn't put the field outside of the