Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(accounts): localize investment labels and chart dates (#3178)

Maxence C.e08d9de5b669
diff --git a/app/models/balance/chart_series_builder.rb b/app/models/balance/chart_series_builder.rb
index 1daeaf1e30..ccb3c9ab7f 100644
--- a/app/models/balance/chart_series_builder.rb
+++ b/app/models/balance/chart_series_builder.rb
@@ -33,14 +33,15 @@ class Balance::ChartSeriesBuilder
raise
end
- # Unrealized gains series: for each date, sum of (market value - cost basis) across
+ # Builds an unrealized-gains series from the latest holding snapshot per security.
+ # For each date, it sums market value minus cost basis across
# the latest holding snapshot per security. Holdings without a usable cost basis
# (nil, or unlocked zero from providers) contribute a gain of 0.
def gains_series
values = gains_query_data.map do |datum|
Series::Value.new(
date: datum.date,
- date_formatted: I18n.l(datum.date, format: :long),
+ date_formatted: Series.format_date(datum.date),
value: Money.new(datum.end_gains, currency),
trend: Trend.new(
current: Money.new(datum.end_gains, currency),
@@ -69,6 +70,7 @@ class Balance::ChartSeriesBuilder
@interval || period.interval
end
+ # Builds a balance series for a balance column and its matching start column.
def build_series_for(column)
values = query_data.map do |datum|
if @strict_currency_conversion && datum[:missing_currency].present?
@@ -83,7 +85,7 @@ class Balance::ChartSeriesBuilder
Series::Value.new(
date: datum.date,
- date_formatted: I18n.l(datum.date, format: :long),
+ date_formatted: Series.format_date(datum.date),
value: Money.new(datum.send(column), currency),
trend: Trend.new(
current: Money.new(datum.send(column), currency),
diff --git a/app/models/balance/linked_investment_series_normalizer.rb b/app/models/balance/linked_investment_series_normalizer.rb
index b78838ef38..6c73677a01 100644
--- a/app/models/balance/linked_investment_series_normalizer.rb
+++ b/app/models/balance/linked_investment_series_normalizer.rb
@@ -163,7 +163,7 @@ class Balance::LinkedInvestmentSeriesNormalizer
def opening_point(date:, date_formatted:, value:)
Series::Value.new(
date: date,
- date_formatted: date_formatted || I18n.l(date, format: :long),
+ date_formatted: date_formatted || Series.format_date(date),
value: value,
trend: Trend.new(
current: value,
diff --git a/app/models/balance/series_aggregator.rb b/app/models/balance/series_aggregator.rb
index fdf4d9dadd..8fcdd080ea 100644
--- a/app/models/balance/series_aggregator.rb
+++ b/app/models/balance/series_aggregator.rb
@@ -8,6 +8,7 @@ class Balance::SeriesAggregator
@align_to_common_start = align_to_common_start
end
+ # Combines component series into one currency-normalized balance series.
def aggregate
return empty_series if normalized_series_list.empty?
@@ -29,7 +30,7 @@ class Balance::SeriesAggregator
series_value = Series::Value.new(
date: date,
- date_formatted: I18n.l(date, format: :long),
+ date_formatted: Series.format_date(date),
value: current_value,
trend: Trend.new(
current: current_value,
diff --git a/app/models/series.rb b/app/models/series.rb
index 6c2800bf15..3fcc3bea10 100644
--- a/app/models/series.rb
+++ b/app/models/series.rb
@@ -21,6 +21,12 @@ class Series
)
class << self
+ # Formats a chart date using the current family's configured date format.
+ def format_date(date)
+ I18n.l(date, format: Current.family&.date_format.presence || :long)
+ end
+
+ # Builds a series and its per-point trends from unordered raw values.
def from_raw_values(values, interval: "1 day")
raise ArgumentError, "Must be an array of at least 2 values" unless values.size >= 2
raise ArgumentError, "Must have date and value properties" unless values.all? { |value| value.has_key?(:date) && value.has_key?(:value) }
@@ -36,7 +42,7 @@ class Series
values: [ nil, *ordered ].each_cons(2).map do |prev_value, curr_value|
Value.new(
date: curr_value[:date],
- date_formatted: I18n.l(curr_value[:date], format: :long),
+ date_formatted: format_date(curr_value[:date]),
value: curr_value[:value],
trend: Trend.new(
current: curr_value[:value],
diff --git a/app/models/valuation.rb b/app/models/valuation.rb
index f80226be31..fd042b2897 100644
--- a/app/models/valuation.rb
+++ b/app/models/valuation.rb
@@ -8,16 +8,34 @@ class Valuation < ApplicationRecord
}, validate: true, default: "reconciliation"
class << self
+ # Returns the stable, persisted name for a reconciliation valuation.
def build_reconciliation_name(accountable_type)
Valuation::Name.new("reconciliation", accountable_type).to_s
end
+ # Returns the stable, persisted name for an opening balance valuation.
def build_opening_anchor_name(accountable_type)
Valuation::Name.new("opening_anchor", accountable_type).to_s
end
+ # Returns the stable, persisted name for a provider-managed current valuation.
def build_current_anchor_name(accountable_type)
Valuation::Name.new("current_anchor", accountable_type).to_s
end
end
+
+ # Localizes generated names for display while preserving custom entry names.
+ def display_name(entry)
+ name = Valuation::Name.new(kind, entry.account.accountable_type)
+ return entry.name unless entry.name == name.to_s
+
+ translation_key = "valuations.names.#{name.translation_key}"
+ if opening_anchor? &&
+ !I18n.exists?(translation_key, I18n.locale, fallback: false) &&
+ I18n.exists?("valuations.show.opening_balance", I18n.locale, fallback: false)
+ return I18n.t("valuations.show.opening_balance")
+ end
+
+ I18n.t(translation_key)
+ end
end
diff --git a/app/models/valuation/name.rb b/app/models/valuation/name.rb
index 79398cdeaa..525efab98e 100644
--- a/app/models/valuation/name.rb
+++ b/app/models/valuation/name.rb
@@ -4,6 +4,7 @@ class Valuation::Name
@accountable_type = accountable_type
end
+ # Returns the stable English name persisted for this valuation type.
def to_s
case valuation_kind
when "opening_anchor"
@@ -15,9 +16,22 @@ class Valuation::Name
end
end
+ # Returns the I18n key suffix for this valuation type and account type.
+ def translation_key
+ case valuation_kind
+ when "opening_anchor"
+ opening_anchor_translation_key
+ when "current_anchor"
+ current_anchor_translation_key
+ else
+ reconciliation_translation_key
+ end
+ end
+
private
attr_reader :valuation_kind, :accountable_type
+ # Selects the persisted name for an opening balance valuation.
def opening_anchor_name
case accountable_type
when "Property", "Vehicle"
@@ -31,6 +45,7 @@ class Valuation::Name
end
end
+ # Selects the persisted name for a provider-managed current valuation.
def current_anchor_name
case accountable_type
when "Property", "Vehicle"
@@ -44,6 +59,7 @@ class Valuation::Name
end
end
+ # Selects the persisted name for a manual reconciliation valuation.
def recon_name
case accountable_type
when "Property", "Investment", "Vehicle", "Crypto", "OtherAsset"
@@ -54,4 +70,33 @@ class Valuation::Name
"Manual balance update"
end
end
+
+ # Selects the translation key for an opening balance valuation.
+ def opening_anchor_translation_key
+ case accountable_type
+ when "Property", "Vehicle" then :original_purchase_price
+ when "Loan" then :original_principal
+ when "Investment", "Crypto", "OtherAsset" then :opening_account_value
+ else :opening_balance
+ end
+ end
+
+ # Selects the translation key for a provider-managed current valuation.
+ def current_anchor_translation_key
+ case accountable_type
+ when "Property", "Vehicle" then :current_market_value
+ when "Loan" then :current_loan_balance
+ when "Investment", "Crypto", "OtherAsset" then :current_account_value
+ else :current_balance
+ end
+ end
+
+ # Selects the translation key for a manual reconciliation valuation.
+ def reconciliation_translation_key
+ case accountable_type
+ when "Property", "Investment", "Vehicle", "Crypto", "OtherAsset" then :manual_value_update
+ when "Loan" then :manual_principal_update
+ else :manual_balance_update
+ end
+ end
end
diff --git a/app/views/investment_activity/_quick_edit_badge.html.erb b/app/views/investment_activity/_quick_edit_badge.html.erb
index c8503a163d..355b1a316d 100644
--- a/app/views/investment_activity/_quick_edit_badge.html.erb
+++ b/app/views/investment_activity/_quick_edit_badge.html.erb
@@ -61,7 +61,7 @@
data: (income_trade ? {} : { action: "click->activity-label-quick-edit#toggle", activity_label_quick_edit_target: "badge" }),
title: (has_label ? t("transactions.transaction.activity_type_tooltip") : t("transactions.show.activity_type"))) do %>
<% if has_label %>
- <%= investment_activity_label_name(label) %>
+ <%= t("transactions.activity_labels.#{label.parameterize(separator: "_")}", default: label) %>
<% else %>
<%= icon "tag", size: "xs", color: "current" %>
<span><%= t("transactions.show.activity_type") %></span>
@@ -89,7 +89,7 @@
class="w-full text-left px-3 py-1.5 text-sm text-primary hover:bg-surface-inset transition-colors <%= activity_label == label ? "font-semibold bg-surface-inset" : "" %>"
data-action="click->activity-label-quick-edit#select"
data-label="<%= activity_label %>">
- <%= investment_activity_label_name(activity_label) %>
+ <%= t("transactions.activity_labels.#{activity_label.parameterize(separator: "_")}", default: activity_label) %>
</button>
<% end %>
</div>
diff --git a/app/views/valuations/_header.html.erb b/app/views/valuations/_header.html.erb
index 2a8566273a..b43d24c7cf 100644
--- a/app/views/valuations/_header.html.erb
+++ b/app/views/valuations/_header.html.erb
@@ -3,7 +3,7 @@
<%= tag.header class: "mb-4 space-y-1", id: dom_id(entry, :header) do %>
<span class="text-secondary text-sm">
<% valuation = entry.entryable %>
- <%= valuation.respond_to?(:opening_anchor?) && valuation.opening_anchor? ? t("valuations.show.opening_balance") : entry.name %>
+ <%= valuation.display_name(entry) %>
</span>
<div class="flex items-center gap-4">
diff --git a/app/views/valuations/_valuation.html.erb b/app/views/valuations/_valuation.html.erb
index 003faf5fec..a7c5f7cc0d 100644
--- a/app/views/valuations/_valuation.html.erb
+++ b/app/views/valuations/_valuation.html.erb
@@ -18,7 +18,7 @@
<div class="truncate text-primary">
<%= link_to(
- valuation.opening_anchor? ? t("valuations.show.opening_balance") : entry.name,
+ valuation.display_name(entry),
entry_path(entry),
data: { turbo_frame: "drawer", turbo_prefetch: false },
class: "hover:underline"
diff --git a/config/locales/views/accounts/fr.yml b/config/locales/views/accounts/fr.yml
index f707731833..162960fa04 100644
--- a/config/locales/views/accounts/fr.yml
+++ b/config/locales/views/accounts/fr.yml
@@ -132,7 +132,7 @@ fr:
statements: Déclarations
tabs:
activity: Activité
- holdings: Holdings
+ holdings: Titres
overview: Aperçu
statements: Déclarations
sidebar:
diff --git a/config/locales/views/components/fr.yml b/config/locales/views/components/fr.yml
index ec14bf7b5b..33b54512a8 100644
--- a/config/locales/views/components/fr.yml
+++ b/config/locales/views/components/fr.yml
@@ -68,14 +68,14 @@ fr:
debt_balance: Solde de la dette
estimated_property_value: Valeur estimée de la propriété
estimated_vehicle_value: Valeur estimée du véhicule
- holdings_value: Valeur des holdings
+ holdings_value: Valeur des titres
remaining_principal_balance: Solde principal restant
total_account_value: Valeur totale du compte
total_gains: Gains totaux
views:
cash: Espèces
- gains: Gains / ROI
- holdings: Holdings
+ gains: Gains / rendement
+ holdings: Titres
total_value: Valeur totale
vs_available_history: par rapport à l'historique disponible
period_picker:
diff --git a/config/locales/views/valuations/en.yml b/config/locales/views/valuations/en.yml
index bcaf6e9027..8cc330fac9 100644
--- a/config/locales/views/valuations/en.yml
+++ b/config/locales/views/valuations/en.yml
@@ -1,6 +1,18 @@
---
en:
valuations:
+ names:
+ current_account_value: Current account value
+ current_balance: Current balance
+ current_loan_balance: Current loan balance
+ current_market_value: Current market value
+ manual_balance_update: Manual balance update
+ manual_principal_update: Manual principal update
+ manual_value_update: Manual value update
+ opening_account_value: Opening account value
+ opening_balance: Opening balance
+ original_principal: Original principal
+ original_purchase_price: Original purchase price
confirmation_contents:
this_will: "This will %{action_verb} the account value on"
to_colon: "to:"
diff --git a/config/locales/views/valuations/fr.yml b/config/locales/views/valuations/fr.yml
index b72243e10b..714f30e5cd 100644
--- a/config/locales/views/valuations/fr.yml
+++ b/config/locales/views/valuations/fr.yml
@@ -1,6 +1,18 @@
---
fr:
valuations:
+ names:
+ current_account_value: Valeur actuelle du compte
+ current_balance: Solde actuel
+ current_loan_balance: Solde actuel du prêt
+ current_market_value: Valeur marchande actuelle
+ manual_balance_update: Mise à jour manuelle du solde
+ manual_principal_update: Mise à jour manuelle du principal
+ manual_value_update: Mise à jour manuelle de la valeur
+ opening_account_value: Valeur d'ouverture du compte
+ opening_balance: Solde d'ouverture
+ original_principal: Principal initial
+ original_purchase_price: Prix d'achat initial
confirm_create:
confirm: Confirmer
title: Confirmer le nouveau solde
diff --git a/test/models/balance/linked_investment_series_normalizer_test.rb b/test/models/balance/linked_investment_series_normalizer_test.rb
index 4a1ed3081a..330ad09122 100644
--- a/test/models/balance/linked_investment_series_normalizer_test.rb
+++ b/test/models/balance/linked_investment_series_normalizer_test.rb
@@ -152,6 +152,7 @@ class Balance::LinkedInvestmentSeriesNormalizerTest < ActiveSupport::TestCase
account.account_providers.create!(provider: coinstats_account)
opening_date = 8.days.ago.to_date
+ account.family.update!(date_format: "%Y-%m-%d")
account.set_opening_anchor_balance(balance: 500, date: opening_date)
account.entries.create!(
name: "Trade",
@@ -174,12 +175,16 @@ class Balance::LinkedInvestmentSeriesNormalizerTest < ActiveSupport::TestCase
favorable_direction: account.favorable_direction
)
- normalizer = Balance::LinkedInvestmentSeriesNormalizer.new(account: account, series: raw_series)
- normalized = normalizer.normalize
+ normalized = I18n.with_locale(:en) do
+ Current.stubs(:family).returns(account.family)
+
+ Balance::LinkedInvestmentSeriesNormalizer.new(account: account, series: raw_series).normalize
+ end
assert_equal opening_date, normalized.start_date
assert_equal [ opening_date, 3.days.ago.to_date, Date.current ], normalized.values.map(&:date)
assert_equal Money.new(500, "USD"), normalized.values.first.value
+ assert_equal opening_date.strftime("%Y-%m-%d"), normalized.values.first.date_formatted
end
test "normalizer resets first point to opening balance when sample lands on exact opening date" do
diff --git a/test/models/series_test.rb b/test/models/series_test.rb
index 40ccf41519..f63f56f2a0 100644
--- a/test/models/series_test.rb
+++ b/test/models/series_test.rb
@@ -1,6 +1,14 @@
require "test_helper"
class SeriesTest < ActiveSupport::TestCase
+ test "formats chart dates using the family's preferred format" do
+ family = families(:empty)
+ family.update!(date_format: "%d/%m/%Y")
+ Current.stubs(:family).returns(family)
+
+ assert_equal "01/06/2026", Series.format_date(Date.new(2026, 6, 1))
+ end
+
test "a change between two values that print identically reads as no change" do
series = build_series(previous: 100.001, current: 100.004, currency: "USD")
diff --git a/test/models/valuation_test.rb b/test/models/valuation_test.rb
new file mode 100644
index 0000000000..a454784403
--- /dev/null
+++ b/test/models/valuation_test.rb
@@ -0,0 +1,28 @@
+require "test_helper"
+
+class ValuationTest < ActiveSupport::TestCase
+ test "localizes generated valuation names when rendering" do
+ entry = Entry.new(name: "Manual value update", account: accounts(:investment))
+ valuation = Valuation.new(kind: "reconciliation")
+
+ I18n.with_locale(:fr) do
+ assert_equal "Mise à jour manuelle de la valeur", valuation.display_name(entry)
+ end
+ end
+
+ test "preserves custom valuation names when rendering" do
+ entry = Entry.new(name: "Réévaluation annuelle", account: accounts(:investment))
+ valuation = Valuation.new(kind: "reconciliation")
+
+ assert_equal "Réévaluation annuelle", valuation.display_name(entry)
+ end
+
+ test "uses the existing localized opening balance when the new name is unavailable" do
+ entry = Entry.new(name: "Opening balance", account: accounts(:depository))
+ valuation = Valuation.new(kind: "opening_anchor")
+
+ I18n.with_locale(:de) do
+ assert_equal "Eröffnungssaldo", valuation.display_name(entry)
+ end
+ end
+end