The personal finance app for everyone
Refactor: Use native error i18n lookup (#1076)
Pedro Carmona14c4b9e93cd2
diff --git a/app/controllers/settings/profiles_controller.rb b/app/controllers/settings/profiles_controller.rbindex 326a06f0..a2c6b219 100644--- a/app/controllers/settings/profiles_controller.rb+++ b/app/controllers/settings/profiles_controller.rb@@ -17,7 +17,7 @@ class Settings::ProfilesController < SettingsControllerif Current.user.update(user_params_with_family)redirect_to settings_profile_path, notice: t(".success")else- redirect_to settings_profile_path, alert: t(".file_size_error")+ redirect_to settings_profile_path, alert: Current.user.errors.full_messages.to_sentenceendenddiff --git a/app/models/account/entry.rb b/app/models/account/entry.rbindex d031de96..345fe26b 100644--- a/app/models/account/entry.rb+++ b/app/models/account/entry.rb@@ -204,7 +204,14 @@ class Account::Entry < ApplicationRecordcurrent_qty = account.holding_qty(account_trade.security)if current_qty < account_trade.qty.abs- errors.add(:base, "cannot sell #{account_trade.qty.abs} shares of #{account_trade.security.ticker} because you only own #{current_qty} shares")+ # i18n-tasks-use t('activerecord.errors.models.account/entry.attributes.base.invalid_sell_quantity')+ errors.add(+ :base,+ :invalid_sell_quantity,+ sell_qty: account_trade.qty.abs,+ ticker: account_trade.security.ticker,+ current_qty: current_qty+ )endendenddiff --git a/app/models/account/transfer.rb b/app/models/account/transfer.rbindex 85fa9406..402c3776 100644--- a/app/models/account/transfer.rb+++ b/app/models/account/transfer.rb@@ -13,15 +13,15 @@ class Account::Transfer < ApplicationRecordenddef from_name- outflow_transaction&.account&.name || I18n.t("account.transfer.from_fallback_name")+ outflow_transaction&.account&.name || I18n.t("account/transfer.from_fallback_name")enddef to_name- inflow_transaction&.account&.name || I18n.t("account.transfer.to_fallback_name")+ inflow_transaction&.account&.name || I18n.t("account/transfer.to_fallback_name")enddef name- I18n.t("account.transfer.name", from_account: from_name, to_account: to_name)+ I18n.t("account/transfer.name", from_account: from_name, to_account: to_name)enddef inflow_transaction@@ -72,24 +72,28 @@ class Account::Transfer < ApplicationRecorddef transaction_countunless entries.size == 2- errors.add :entries, "must have exactly 2 entries"+ # i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_have_exactly_2_entries')+ errors.add :entries, :must_have_exactly_2_entriesendenddef from_different_accountsaccounts = entries.map { |e| e.account_id }.uniq- errors.add :entries, "must be from different accounts" if accounts.size < entries.size+ # i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_be_from_different_accounts')+ errors.add :entries, :must_be_from_different_accounts if accounts.size < entries.sizeenddef net_zero_flowsunless entries.sum(&:amount).zero?- errors.add :transactions, "must have an inflow and outflow that net to zero"+ # i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_have_an_inflow_and_outflow_that_net_to_zero')+ errors.add :entries, :must_have_an_inflow_and_outflow_that_net_to_zeroendenddef all_transactions_markedunless entries.all?(&:marked_as_transfer)- errors.add :entries, "must be marked as transfer"+ # i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_be_marked_as_transfer')+ errors.add :entries, :must_be_marked_as_transferendendenddiff --git a/app/models/import.rb b/app/models/import.rbindex 6a02bf22..2083113f 100644--- a/app/models/import.rb+++ b/app/models/import.rb@@ -178,7 +178,8 @@ class Import < ApplicationRecordbeginCSV.parse(raw_csv_str || "")rescue CSV::MalformedCSVError- errors.add(:raw_csv_str, "is not a valid CSV format")+ # i18n-tasks-use t('activerecord.errors.models.import.attributes.raw_csv_str.invalid_csv_format')+ errors.add(:raw_csv_str, :invalid_csv_format)endendenddiff --git a/app/models/time_series/trend.rb b/app/models/time_series/trend.rbindex 4b989e9f..e4959bc9 100644--- a/app/models/time_series/trend.rb+++ b/app/models/time_series/trend.rb@@ -83,18 +83,22 @@ class TimeSeries::Trenddef values_must_be_of_same_typeunless current.class == previous.class || [ previous, current ].any?(&:nil?)- errors.add :current, "must be of the same type as previous"- errors.add :previous, "must be of the same type as current"+ # i18n-tasks-use t('activemodel.errors.models.time_series/trend.attributes.current.must_be_of_the_same_type_as_previous')+ errors.add :current, :must_be_of_the_same_type_as_previous+ # i18n-tasks-use t('activemodel.errors.models.time_series/trend.attributes.previous.must_be_of_the_same_type_as_current')+ errors.add :previous, :must_be_of_the_same_type_as_currentendenddef values_must_be_of_known_typeunless current.is_a?(Money) || current.is_a?(Numeric) || current.nil?- errors.add :current, "must be of type Money, Numeric, or nil"+ # i18n-tasks-use t('activemodel.errors.models.time_series/trend.attributes.current.must_be_of_type_money_numeric_or_nil')+ errors.add :current, :must_be_of_type_money_numeric_or_nilendunless previous.is_a?(Money) || previous.is_a?(Numeric) || previous.nil?- errors.add :previous, "must be of type Money, Numeric, or nil"+ # i18n-tasks-use t('activemodel.errors.models.time_series/trend.attributes.previous.must_be_of_type_money_numeric_or_nil')+ errors.add :previous, :must_be_of_type_money_numeric_or_nilendenddiff --git a/app/models/time_series/value.rb b/app/models/time_series/value.rbindex c19c3c97..2f698768 100644--- a/app/models/time_series/value.rb+++ b/app/models/time_series/value.rb@@ -40,7 +40,8 @@ class TimeSeries::Valuedef value_must_be_of_known_typeunless value.is_a?(Money) || value.is_a?(Numeric)- errors.add :value, "must be a Money or Numeric"+ # i18n-tasks-use t('activemodel.errors.models.time_series/value.attributes.value.must_be_a_money_or_numeric')+ errors.add :value, must_be_a_money_or_numericendendenddiff --git a/app/models/user.rb b/app/models/user.rbindex 0d553904..62f5209c 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -55,7 +55,8 @@ class User < ApplicationRecorddef can_deactivateif admin? && family.users.count > 1- errors.add(:base, I18n.t("activerecord.errors.user.cannot_deactivate_admin_with_other_users"))+ # i18n-tasks-use t('activerecord.errors.models.user.attributes.base.cannot_deactivate_admin_with_other_users')+ errors.add(:base, :cannot_deactivate_admin_with_other_users)endend@@ -83,7 +84,8 @@ class User < ApplicationRecorddef profile_image_sizeif profile_image.attached? && profile_image.byte_size > 5.megabytes- errors.add(:profile_image, "is too large. Maximum size is 5 MB.")+ # i18n-tasks-use t('activerecord.errors.models.user.attributes.profile_image.invalid_file_size')+ errors.add(:profile_image, :invalid_file_size, max_megabytes: 5)endendenddiff --git a/config/locales/models/account/entry/en.yml b/config/locales/models/account/entry/en.ymlnew file mode 100644index 00000000..e1e0605e--- /dev/null+++ b/config/locales/models/account/entry/en.yml@@ -0,0 +1,10 @@+---+en:+ activerecord:+ errors:+ models:+ account/entry:+ attributes:+ base:+ invalid_sell_quantity: cannot sell %{sell_qty} shares of %{ticker} because+ you only own %{current_qty} sharesdiff --git a/config/locales/models/account/transfer/en.yml b/config/locales/models/account/transfer/en.ymlindex dc33084d..c0fe38d5 100644--- a/config/locales/models/account/transfer/en.yml+++ b/config/locales/models/account/transfer/en.yml@@ -1,7 +1,17 @@---en:- account:- transfer:- from_fallback_name: Originator- name: Transfer from %{from_account} to %{to_account}- to_fallback_name: Receiver+ account/transfer:+ from_fallback_name: Originator+ name: Transfer from %{from_account} to %{to_account}+ to_fallback_name: Receiver+ activerecord:+ errors:+ models:+ account/transfer:+ attributes:+ entries:+ must_be_from_different_accounts: must be from different accounts+ must_be_marked_as_transfer: must be marked as transfer+ must_have_an_inflow_and_outflow_that_net_to_zero: must have an inflow+ and outflow that net to zero+ must_have_exactly_2_entries: must have exactly 2 entriesdiff --git a/config/locales/models/import/en.yml b/config/locales/models/import/en.ymlnew file mode 100644index 00000000..56cdec7d--- /dev/null+++ b/config/locales/models/import/en.yml@@ -0,0 +1,9 @@+---+en:+ activerecord:+ errors:+ models:+ import:+ attributes:+ raw_csv_str:+ invalid_csv_format: is not a valid CSV formatdiff --git a/config/locales/models/time_series/trend/en.yml b/config/locales/models/time_series/trend/en.ymlnew file mode 100644index 00000000..5f02353f--- /dev/null+++ b/config/locales/models/time_series/trend/en.yml@@ -0,0 +1,15 @@+---+en:+ activemodel:+ errors:+ models:+ time_series/trend:+ attributes:+ current:+ must_be_of_the_same_type_as_previous: must be of the same type as previous+ must_be_of_type_money_numeric_or_nil: must be of type Money, Numeric,+ or nil+ previous:+ must_be_of_the_same_type_as_current: must be of the same type as current+ must_be_of_type_money_numeric_or_nil: must be of type Money, Numeric,+ or nildiff --git a/config/locales/models/time_series/value/en.yml b/config/locales/models/time_series/value/en.ymlnew file mode 100644index 00000000..d6ac035f--- /dev/null+++ b/config/locales/models/time_series/value/en.yml@@ -0,0 +1,9 @@+---+en:+ activemodel:+ errors:+ models:+ time_series/value:+ attributes:+ value:+ must_be_a_money_or_numeric: must be a Money or Numericdiff --git a/config/locales/models/user/en.yml b/config/locales/models/user/en.ymlindex 43307ed2..9b66de68 100644--- a/config/locales/models/user/en.yml+++ b/config/locales/models/user/en.yml@@ -11,6 +11,11 @@ en:password: Passwordpassword_confirmation: Password Confirmationerrors:- user:- cannot_deactivate_admin_with_other_users: Admin cannot delete account while- other users are present. Please delete all members first.+ models:+ user:+ attributes:+ base:+ cannot_deactivate_admin_with_other_users: Admin cannot delete account+ while other users are present. Please delete all members first.+ profile_image:+ invalid_file_size: file size must be less than %{max_megabytes}MBdiff --git a/config/locales/views/settings/en.yml b/config/locales/views/settings/en.ymlindex ae080c06..b1b9d267 100644--- a/config/locales/views/settings/en.yml+++ b/config/locales/views/settings/en.yml@@ -108,5 +108,4 @@ en:profile_title: Profilesave: Saveupdate:- file_size_error: File size must be less than 5MBsuccess: Profile updated successfully.diff --git a/test/controllers/settings/profiles_controller_test.rb b/test/controllers/settings/profiles_controller_test.rbindex eb14c554..19503fff 100644--- a/test/controllers/settings/profiles_controller_test.rb+++ b/test/controllers/settings/profiles_controller_test.rb@@ -25,6 +25,7 @@ class Settings::ProfilesControllerTest < ActionDispatch::IntegrationTestdelete settings_profile_urlassert_redirected_to settings_profile_url+ assert_equal "Admin cannot delete account while other users are present. Please delete all members first.", flash[:alert]assert_no_enqueued_jobs only: UserPurgeJobassert User.find(@admin.id).active?end