Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Use moniker interpolation for user family labels (#3060)

Sure Admin (bot)b0edf9926203
diff --git a/app/models/user.rb b/app/models/user.rb
index 257a77e20a..d18531ee3d 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -81,6 +81,21 @@ class User < ApplicationRecord
User.exists? ? fallback_role : :super_admin
end
+ class << self
+ def human_attribute_name(attribute, options = {})
+ locale = options[:locale] || I18n.locale
+ moniker = I18n.with_locale(locale) do
+ Current.family&.moniker_label || I18n.t("shared.family_moniker.singular", default: "Family")
+ end
+
+ options = {
+ moniker: moniker
+ }.merge(options)
+
+ super(attribute, options)
+ end
+ end
+
has_one_attached :profile_image, dependent: :purge_later do |attachable|
attachable.variant :thumbnail, resize_to_fill: [ 300, 300 ], convert: :webp, saver: { quality: 80 }
attachable.variant :small, resize_to_fill: [ 72, 72 ], convert: :webp, saver: { quality: 80 }, preprocessed: true
diff --git a/config/locales/models/user/es.yml b/config/locales/models/user/es.yml
index df48324893..60cb81cbc7 100644
--- a/config/locales/models/user/es.yml
+++ b/config/locales/models/user/es.yml
@@ -4,8 +4,8 @@ es:
attributes:
user:
email: Correo electrónico
- family: Familia
- family_id: Familia
+ family: "%{moniker}"
+ family_id: "%{moniker}"
first_name: Nombre
last_name: Apellidos
password: Contraseña
@@ -18,4 +18,4 @@ es:
cannot_deactivate_admin_with_other_users: El administrador no puede eliminar la cuenta
mientras haya otros usuarios. Por favor, elimine primero a todos los miembros.
profile_image:
- invalid_file_size: El tamaño del archivo debe ser inferior a %{max_megabytes} MB.
\ No newline at end of file
+ invalid_file_size: El tamaño del archivo debe ser inferior a %{max_megabytes} MB.
diff --git a/config/locales/models/user/fr.yml b/config/locales/models/user/fr.yml
index de95b70b4f..e27b2c3dab 100644
--- a/config/locales/models/user/fr.yml
+++ b/config/locales/models/user/fr.yml
@@ -4,8 +4,8 @@ fr:
attributes:
user:
email: Adresse mail
- family: Famille
- family_id: Famille
+ family: "%{moniker}"
+ family_id: "%{moniker}"
first_name: Prénom
last_name: Nom
password: Mot de passe
diff --git a/config/locales/models/user/nb.yml b/config/locales/models/user/nb.yml
index 5ee1080461..dcfe037ba2 100644
--- a/config/locales/models/user/nb.yml
+++ b/config/locales/models/user/nb.yml
@@ -4,8 +4,8 @@ nb:
attributes:
user:
email: E-post
- family: Husholdning
- family_id: Husholdning
+ family: "%{moniker}"
+ family_id: "%{moniker}"
first_name: Fornavn
last_name: Etternavn
password: Passord
@@ -17,4 +17,4 @@ nb:
base:
cannot_deactivate_admin_with_other_users: Administrator kan ikke deaktivere kontoen mens andre brukere er til stede. Vennligst slett alle medlemmer først.
profile_image:
- invalid_file_size: filstørrelsen må være mindre enn %{max_megabytes} MB
\ No newline at end of file
+ invalid_file_size: filstørrelsen må være mindre enn %{max_megabytes} MB
diff --git a/config/locales/models/user/nl.yml b/config/locales/models/user/nl.yml
index 7f9897542e..763e1fb1fb 100644
--- a/config/locales/models/user/nl.yml
+++ b/config/locales/models/user/nl.yml
@@ -4,8 +4,8 @@ nl:
attributes:
user:
email: Email
- family: Familie
- family_id: Familie
+ family: "%{moniker}"
+ family_id: "%{moniker}"
first_name: Voornaam
last_name: Achternaam
password: Wachtwoord
diff --git a/config/locales/models/user/pt-BR.yml b/config/locales/models/user/pt-BR.yml
index 34ca4419ca..00917ada92 100644
--- a/config/locales/models/user/pt-BR.yml
+++ b/config/locales/models/user/pt-BR.yml
@@ -4,8 +4,8 @@ pt-BR:
attributes:
user:
email: E-mail
- family: Família
- family_id: Família
+ family: "%{moniker}"
+ family_id: "%{moniker}"
first_name: Primeiro Nome
last_name: Sobrenome
password: Senha
diff --git a/config/locales/models/user/ro.yml b/config/locales/models/user/ro.yml
index f652331bbd..97ed051030 100644
--- a/config/locales/models/user/ro.yml
+++ b/config/locales/models/user/ro.yml
@@ -3,8 +3,8 @@ ro:
attributes:
user:
email: Email
- family: Familie
- family_id: Familie
+ family: "%{moniker}"
+ family_id: "%{moniker}"
first_name: Prenume
last_name: Nume de familie
password: Parolă
@@ -16,4 +16,4 @@ ro:
base:
cannot_deactivate_admin_with_other_users: Administratorul nu poate șterge contul cât timp există alți utilizatori. Te rugăm să ștergi toți membrii mai întâi.
profile_image:
- invalid_file_size: dimensiunea fișierului trebuie să fie mai mică de %{max_megabytes}MB
\ No newline at end of file
+ invalid_file_size: dimensiunea fișierului trebuie să fie mai mică de %{max_megabytes}MB
diff --git a/config/locales/models/user/zh-CN.yml b/config/locales/models/user/zh-CN.yml
index 95b724df07..da484b07ae 100644
--- a/config/locales/models/user/zh-CN.yml
+++ b/config/locales/models/user/zh-CN.yml
@@ -4,8 +4,8 @@ zh-CN:
attributes:
user:
email: 邮箱
- family: 家庭
- family_id: 家庭
+ family: "%{moniker}"
+ family_id: "%{moniker}"
first_name: 名字
last_name: 姓氏
password: 密码
diff --git a/config/locales/views/shared/es.yml b/config/locales/views/shared/es.yml
index e03d67e0ca..100d0619d5 100644
--- a/config/locales/views/shared/es.yml
+++ b/config/locales/views/shared/es.yml
@@ -17,5 +17,10 @@ es:
exchange_rate_help: Elige cómo introducir el importe.
syncing_notice:
syncing: Sincronizando datos de cuentas...
+ family_moniker:
+ group_plural: Grupos
+ group_singular: Grupo
+ plural: Familias
+ singular: Familia
trend_change:
no_change: "sin cambios"
diff --git a/config/locales/views/shared/hu.yml b/config/locales/views/shared/hu.yml
index 50d7fd0006..55cf675c0e 100644
--- a/config/locales/views/shared/hu.yml
+++ b/config/locales/views/shared/hu.yml
@@ -29,6 +29,8 @@ hu:
resource_deletion_body: "Biztosan törli a következőt: %{resource}? Ez a művelet nem vonható vissza."
resource_deletion_btn_text: "%{resource} törlése"
family_moniker:
+ group_plural: Csoportok
+ group_singular: Csoport
singular: Háztartás
plural: Háztartások
trend_change:
diff --git a/config/locales/views/shared/nb.yml b/config/locales/views/shared/nb.yml
index c305e44af8..03bb7666f0 100644
--- a/config/locales/views/shared/nb.yml
+++ b/config/locales/views/shared/nb.yml
@@ -17,5 +17,10 @@ nb:
exchange_rate_help: Velg hvordan du vil fylle inn beløpet.
syncing_notice:
syncing: Synkroniserer kontodata...
+ family_moniker:
+ group_plural: Grupper
+ group_singular: Gruppe
+ plural: Husholdninger
+ singular: Husholdning
trend_change:
- no_change: "ingen endring"
\ No newline at end of file
+ no_change: "ingen endring"
diff --git a/config/locales/views/shared/nl.yml b/config/locales/views/shared/nl.yml
index 8d4b294905..d22df5d685 100644
--- a/config/locales/views/shared/nl.yml
+++ b/config/locales/views/shared/nl.yml
@@ -17,5 +17,10 @@ nl:
exchange_rate_help: Kies hoe u het bedrag wilt invoeren.
syncing_notice:
syncing: Accountsgegevens synchroniseren...
+ family_moniker:
+ group_plural: Groepen
+ group_singular: Groep
+ plural: Families
+ singular: Familie
trend_change:
no_change: "geen verandering"
diff --git a/config/locales/views/shared/pl.yml b/config/locales/views/shared/pl.yml
index e58b9c3222..6b8f0342ee 100644
--- a/config/locales/views/shared/pl.yml
+++ b/config/locales/views/shared/pl.yml
@@ -12,5 +12,10 @@ pl:
syncing_notice:
syncing: Synchronizowanie danych kont...
require_admin: "Tę akcję mogą wykonać tylko administratorzy"
+ family_moniker:
+ group_plural: Grupy
+ group_singular: Grupa
+ plural: Rodziny
+ singular: Rodzina
trend_change:
no_change: "bez zmian"
diff --git a/config/locales/views/shared/pt-BR.yml b/config/locales/views/shared/pt-BR.yml
index ab58eeee59..3404299500 100644
--- a/config/locales/views/shared/pt-BR.yml
+++ b/config/locales/views/shared/pt-BR.yml
@@ -17,5 +17,10 @@ pt-BR:
exchange_rate_help: Escolha como inserir o valor.
syncing_notice:
syncing: Sincronizando dados das contas...
+ family_moniker:
+ group_plural: Grupos
+ group_singular: Grupo
+ plural: Famílias
+ singular: Família
trend_change:
no_change: "sem alteração"
diff --git a/config/locales/views/shared/ro.yml b/config/locales/views/shared/ro.yml
index 9e735bd9eb..22b7e16e61 100644
--- a/config/locales/views/shared/ro.yml
+++ b/config/locales/views/shared/ro.yml
@@ -17,5 +17,10 @@ ro:
exchange_rate_help: Alege cum dorești să introduci suma.
syncing_notice:
syncing: Se sincronizează datele conturilor...
+ family_moniker:
+ group_plural: Grupuri
+ group_singular: Grup
+ plural: Familii
+ singular: Familie
trend_change:
no_change: "fără schimbare"
diff --git a/test/models/user_test.rb b/test/models/user_test.rb
index b2bbfb7963..c8a4d9bd0b 100644
--- a/test/models/user_test.rb
+++ b/test/models/user_test.rb
@@ -68,6 +68,60 @@ class UserTest < ActiveSupport::TestCase
assert_equal "D", user.initial
end
+ test "family validation label uses localized default moniker" do
+ I18n.with_locale(:es) do
+ Current.stubs(:family).returns(nil)
+ user = User.new(email: "[email protected]", password: user_password_test)
+
+ assert_not user.valid?
+ assert_includes user.errors.full_messages, "Familia debe existir"
+ end
+ end
+
+ test "family validation label uses current family moniker" do
+ family = families(:dylan_family)
+ family.update!(moniker: "Group")
+
+ I18n.with_locale(:es) do
+ Current.stubs(:family).returns(family)
+ user = User.new(email: "[email protected]", password: user_password_test)
+
+ assert_not user.valid?
+ assert_includes user.errors.full_messages, "Grupo debe existir"
+ end
+ end
+
+ test "family attribute labels use requested locale for current family moniker" do
+ family = families(:dylan_family)
+ family.update!(moniker: "Group")
+ Current.stubs(:family).returns(family)
+
+ I18n.with_locale(:en) do
+ assert_equal "Grupo", User.human_attribute_name(:family, locale: :es)
+ assert_equal "Grupo", User.human_attribute_name(:family_id, locale: :es)
+ end
+ end
+
+ test "family validation label renders in every supported locale" do
+ family = families(:dylan_family)
+ family.update!(moniker: "Group")
+ Current.stubs(:family).returns(family)
+
+ LanguagesHelper::SUPPORTED_LOCALES.each do |locale|
+ I18n.with_locale(locale) do
+ user = User.new(email: "missing-family-#{locale.parameterize}@example.com", password: user_password_test)
+ family_label = User.human_attribute_name(:family, locale: locale)
+ family_id_label = User.human_attribute_name(:family_id, locale: locale)
+
+ assert_not user.valid?
+ assert_includes user.errors.full_messages_for(:family).to_sentence,
+ family_label,
+ "expected family error to include moniker label for #{locale}"
+ assert_equal family_label, family_id_label
+ end
+ end
+ end
+
test "names are normalized" do
@user.update!(first_name: "", last_name: "")
assert_nil @user.first_name