The personal finance app for everyone (by everyone)
Ruby
Adapt holdings to number inputs (#1258)
soky srmc7b9bc48bca1
diff --git a/app/javascript/utils/parse_locale_float.js b/app/javascript/utils/parse_locale_float.jsindex 4df30a5d9d..0011f5eabd 100644--- a/app/javascript/utils/parse_locale_float.js+++ b/app/javascript/utils/parse_locale_float.js@@ -1,9 +1,22 @@// Parses a float from a string that may use either commas or dots as decimal separators.// Handles formats like "1,234.56" (English) and "1.234,56" (French/European).-export default function parseLocaleFloat(value) {+//+// When a `separator` hint is provided (e.g., from currency metadata), parsing is+// deterministic. Without a hint, a heuristic detects the format from the string.+export default function parseLocaleFloat(value, { separator } = {}) {if (typeof value !== "string") return Number.parseFloat(value) || 0const cleaned = value.replace(/\s/g, "")++ // Deterministic parsing when the currency's decimal separator is known+ if (separator === ",") {+ return Number.parseFloat(cleaned.replace(/\./g, "").replace(",", ".")) || 0+ }+ if (separator === ".") {+ return Number.parseFloat(cleaned.replace(/,/g, "")) || 0+ }++ // Heuristic: detect separator from the string when no hint is availableconst lastComma = cleaned.lastIndexOf(",")const lastDot = cleaned.lastIndexOf(".")diff --git a/app/views/holdings/_cost_basis_cell.html.erb b/app/views/holdings/_cost_basis_cell.html.erbindex 6e4b630bb2..f482d00978 100644--- a/app/views/holdings/_cost_basis_cell.html.erb+++ b/app/views/holdings/_cost_basis_cell.html.erb@@ -61,12 +61,12 @@<label class="form-field__label"><%= t(".total_cost_basis_label") %></label><div class="flex items-center gap-1"><span class="text-secondary text-sm font-medium"><%= currency.symbol %></span>- <input type="text" inputmode="decimal"+ <input type="number" step="any"name="holding[cost_basis]"class="form-field__input grow"placeholder="0.00"autocomplete="off"- value="<%= number_with_precision(current_total, precision: 2) if current_total %>"+ value="<%= sprintf("%.2f", current_total) if current_total %>"data-action="input->cost-basis-form#updatePerShare"data-cost-basis-form-target="total"><span class="text-secondary text-sm"><%= currency.iso_code %></span>@@ -82,11 +82,11 @@<label class="text-xs text-secondary block mb-1"><%= t(".or_per_share_label") %></label><div class="flex items-center gap-1"><span class="text-secondary text-sm font-medium"><%= currency.symbol %></span>- <input type="text" inputmode="decimal"+ <input type="number" step="any"class="form-field__input grow"placeholder="0.00"autocomplete="off"- value="<%= number_with_precision(current_per_share, precision: 2) if current_per_share %>"+ value="<%= sprintf("%.2f", current_per_share) if current_per_share %>"data-action="input->cost-basis-form#updateTotal"data-cost-basis-form-target="perShare"><span class="text-secondary text-sm"><%= currency.iso_code %></span>diff --git a/app/views/holdings/show.html.erb b/app/views/holdings/show.html.erbindex c94582471b..38ff086c04 100644--- a/app/views/holdings/show.html.erb+++ b/app/views/holdings/show.html.erb@@ -133,12 +133,12 @@<label class="form-field__label"><%= t("holdings.cost_basis_cell.total_cost_basis_label") %></label><div class="flex items-center gap-1"><span class="text-secondary text-sm font-medium"><%= currency.symbol %></span>- <input type="text" inputmode="decimal"+ <input type="number" step="any"name="holding[cost_basis]"class="form-field__input grow"placeholder="0.00"autocomplete="off"- value="<%= number_with_precision(current_total, precision: 2) if current_total %>"+ value="<%= sprintf("%.2f", current_total) if current_total %>"data-action="input->drawer-cost-basis#updatePerShare"data-drawer-cost-basis-target="total"><span class="text-secondary text-sm"><%= currency.iso_code %></span>@@ -153,11 +153,11 @@<label class="text-xs text-secondary block mb-1"><%= t("holdings.cost_basis_cell.or_per_share_label") %></label><div class="flex items-center gap-1"><span class="text-secondary text-sm font-medium"><%= currency.symbol %></span>- <input type="text" inputmode="decimal"+ <input type="number" step="any"class="form-field__input grow"placeholder="0.00"autocomplete="off"- value="<%= number_with_precision(current_per_share, precision: 2) if current_per_share %>"+ value="<%= sprintf("%.2f", current_per_share) if current_per_share %>"data-action="input->drawer-cost-basis#updateTotal"data-drawer-cost-basis-target="perShare"><span class="text-secondary text-sm"><%= currency.iso_code %></span>diff --git a/app/views/trades/_header.html.erb b/app/views/trades/_header.html.erbindex 3cd66872e9..a9f1e12bbd 100644--- a/app/views/trades/_header.html.erb+++ b/app/views/trades/_header.html.erb@@ -18,7 +18,7 @@</h3><% if entry.linked? %>- <span title="Linked with Plaid">+ <span title="<%= t("transactions.transaction.linked_with_provider", provider: entry.account.provider_name&.titleize) %>"><%= icon("refresh-ccw", size: "sm") %></span><% end %>diff --git a/app/views/transactions/_header.html.erb b/app/views/transactions/_header.html.erbindex f91f5d0ee7..d80c13bedd 100644--- a/app/views/transactions/_header.html.erb+++ b/app/views/transactions/_header.html.erb@@ -12,7 +12,7 @@<%= icon "arrow-left-right", size: "sm", class: "text-secondary" %><% end %><% if entry.linked? %>- <span title="<%= t("transactions.transaction.linked_with_plaid") %>" class="text-secondary">+ <span title="<%= t("transactions.transaction.linked_with_provider", provider: entry.account.provider_name&.titleize) %>" class="text-secondary"><%= icon("refresh-ccw", size: "sm") %></span><% end %>diff --git a/config/locales/views/transactions/ca.yml b/config/locales/views/transactions/ca.ymlindex 7a1a2ad040..dc3b949811 100644--- a/config/locales/views/transactions/ca.yml+++ b/config/locales/views/transactions/ca.yml@@ -122,6 +122,7 @@ ca:transaction:pending: Pendentpending_tooltip: Transacció pendent — pot canviar quan es publiqui+ linked_with_provider: Vinculat amb %{provider}possible_duplicate: Duplicat?potential_duplicate_tooltip: Això pot ser un duplicat d'una altra transaccióreview_recommended: Revisadiff --git a/config/locales/views/transactions/de.yml b/config/locales/views/transactions/de.ymlindex a8e280497b..2b19d6942f 100644--- a/config/locales/views/transactions/de.yml+++ b/config/locales/views/transactions/de.yml@@ -75,7 +75,7 @@ de:transaction:pending: Ausstehendpending_tooltip: Ausstehende Transaktion — kann sich bei Buchung ändern- linked_with_plaid: Mit Plaid verknüpft+ linked_with_provider: Mit %{provider} verknüpftactivity_type_tooltip: Art der Anlageaktivitätpossible_duplicate: Duplikat?potential_duplicate_tooltip: Dies könnte ein Duplikat einer anderen Transaktion seindiff --git a/config/locales/views/transactions/en.yml b/config/locales/views/transactions/en.ymlindex 836aabff37..0c4b5b990c 100644--- a/config/locales/views/transactions/en.yml+++ b/config/locales/views/transactions/en.yml@@ -86,7 +86,7 @@ en:transaction:pending: Pendingpending_tooltip: Pending transaction — may change when posted- linked_with_plaid: Linked with Plaid+ linked_with_provider: Linked with %{provider}activity_type_tooltip: Investment activity typepossible_duplicate: Duplicate?potential_duplicate_tooltip: This may be a duplicate of another transactiondiff --git a/config/locales/views/transactions/es.yml b/config/locales/views/transactions/es.ymlindex 7e0ea10dc1..b5d2944225 100644--- a/config/locales/views/transactions/es.yml+++ b/config/locales/views/transactions/es.yml@@ -76,7 +76,7 @@ es:transaction:pending: Pendientepending_tooltip: Transacción pendiente — puede cambiar al confirmarse- linked_with_plaid: Vinculado con Plaid+ linked_with_provider: Vinculado con %{provider}activity_type_tooltip: Tipo de actividad de inversiónpossible_duplicate: ¿Duplicada?potential_duplicate_tooltip: Esto puede ser un duplicado de otra transaccióndiff --git a/config/locales/views/transactions/fr.yml b/config/locales/views/transactions/fr.ymlindex e4f6960e94..d6aeb52a3a 100644--- a/config/locales/views/transactions/fr.yml+++ b/config/locales/views/transactions/fr.yml@@ -45,6 +45,8 @@ fr:edit_merchants: Modifier les marchandsedit_tags: Modifier les étiquettesimport: Importer+ transaction:+ linked_with_provider: Lié avec %{provider}index:transaction: transactiontransactions: transactionsdiff --git a/config/locales/views/transactions/nb.yml b/config/locales/views/transactions/nb.ymlindex 5661ad062d..479659c303 100644--- a/config/locales/views/transactions/nb.yml+++ b/config/locales/views/transactions/nb.yml@@ -46,6 +46,8 @@ nb:edit_merchants: Rediger selgereedit_tags: Rediger taggerimport: Importer+ transaction:+ linked_with_provider: Koblet med %{provider}index:transaction: transaksjontransactions: transaksjonerdiff --git a/config/locales/views/transactions/nl.yml b/config/locales/views/transactions/nl.ymlindex 4e3bb1dfcf..25415e4fc4 100644--- a/config/locales/views/transactions/nl.yml+++ b/config/locales/views/transactions/nl.yml@@ -74,6 +74,7 @@ nl:transaction:pending: Wachtendpending_tooltip: Wachtende transactie — kan wijzigen bij posting+ linked_with_provider: Gekoppeld met %{provider}activity_type_tooltip: Beleggingsactiviteitstypepossible_duplicate: Duplicaat?potential_duplicate_tooltip: Dit kan een duplicaat zijn van een andere transactiediff --git a/config/locales/views/transactions/pt-BR.yml b/config/locales/views/transactions/pt-BR.ymlindex 15476c952a..5d898a27a5 100644--- a/config/locales/views/transactions/pt-BR.yml+++ b/config/locales/views/transactions/pt-BR.yml@@ -49,6 +49,8 @@ pt-BR:edit_merchants: Editar comerciantesedit_tags: Editar tagsimport: Importar+ transaction:+ linked_with_provider: Vinculado com %{provider}index:transaction: transaçãotransactions: transaçõesdiff --git a/config/locales/views/transactions/ro.yml b/config/locales/views/transactions/ro.ymlindex 8312d34061..dd0d710879 100644--- a/config/locales/views/transactions/ro.yml+++ b/config/locales/views/transactions/ro.yml@@ -45,6 +45,8 @@ ro:edit_merchants: Editează comercianțiedit_tags: Editează eticheteimport: Importă+ transaction:+ linked_with_provider: Conectat cu %{provider}index:transaction: tranzacțietransactions: tranzacțiidiff --git a/config/locales/views/transactions/tr.yml b/config/locales/views/transactions/tr.ymlindex b7f545a8e1..bb27ce92c8 100644--- a/config/locales/views/transactions/tr.yml+++ b/config/locales/views/transactions/tr.yml@@ -45,6 +45,8 @@ tr:edit_merchants: Satıcıları düzenleedit_tags: Etiketleri düzenleimport: İçe aktar+ transaction:+ linked_with_provider: "%{provider} ile bağlantılı"index:transaction: işlemtransactions: işlemlerdiff --git a/config/locales/views/transactions/zh-CN.yml b/config/locales/views/transactions/zh-CN.ymlindex b8533bba2a..11fa2e3e59 100644--- a/config/locales/views/transactions/zh-CN.yml+++ b/config/locales/views/transactions/zh-CN.yml@@ -25,6 +25,8 @@ zh-CN:edit_merchants: 编辑商户edit_tags: 编辑标签import: 导入+ transaction:+ linked_with_provider: 已与 %{provider} 关联index:import: 导入transaction: 交易diff --git a/config/locales/views/transactions/zh-TW.yml b/config/locales/views/transactions/zh-TW.ymlindex d5bb0fb469..53c080a8e9 100644--- a/config/locales/views/transactions/zh-TW.yml+++ b/config/locales/views/transactions/zh-TW.yml@@ -48,6 +48,8 @@ zh-TW:edit_merchants: 編輯商家edit_tags: 編輯標籤import: 匯入+ transaction:+ linked_with_provider: 已與 %{provider} 連結index:transaction: 筆交易transactions: 筆交易diff --git a/test/javascript/parse_locale_float_test.mjs b/test/javascript/parse_locale_float_test.mjsindex cc233e25e9..5d88d9e558 100644--- a/test/javascript/parse_locale_float_test.mjs+++ b/test/javascript/parse_locale_float_test.mjs@@ -1,11 +1,20 @@import { describe, it } from "node:test"import assert from "node:assert/strict"-// Inline the function to avoid needing a bundler for ESM imports-function parseLocaleFloat(value) {+// Inline the function to avoid needing a bundler for ESM imports.+// Must be kept in sync with app/javascript/utils/parse_locale_float.js+function parseLocaleFloat(value, { separator } = {}) {if (typeof value !== "string") return Number.parseFloat(value) || 0const cleaned = value.replace(/\s/g, "")++ if (separator === ",") {+ return Number.parseFloat(cleaned.replace(/\./g, "").replace(",", ".")) || 0+ }+ if (separator === ".") {+ return Number.parseFloat(cleaned.replace(/,/g, "")) || 0+ }+const lastComma = cleaned.lastIndexOf(",")const lastDot = cleaned.lastIndexOf(".")@@ -74,6 +83,10 @@ describe("parseLocaleFloat", () => {it("treats 1,000 as one thousand", () => {assert.equal(parseLocaleFloat("1,000"), 1000)})++ it("treats 1,000,000 as one million", () => {+ assert.equal(parseLocaleFloat("1,000,000"), 1000000)+ })})describe("integers", () => {@@ -96,6 +109,79 @@ describe("parseLocaleFloat", () => {})})+ describe("negative numbers", () => {+ it("parses negative dot-decimal", () => {+ assert.equal(parseLocaleFloat("-1,234.56"), -1234.56)+ })++ it("parses negative comma-decimal", () => {+ assert.equal(parseLocaleFloat("-1.234,56"), -1234.56)+ })++ it("parses simple negative", () => {+ assert.equal(parseLocaleFloat("-256.54"), -256.54)+ })++ it("parses negative European simple", () => {+ assert.equal(parseLocaleFloat("-256,54"), -256.54)+ })+ })++ describe("with separator hint", () => {+ describe("comma separator (European currencies like EUR)", () => {+ const opts = { separator: "," }++ it("disambiguates 1,234 as 1.234 (European decimal)", () => {+ assert.equal(parseLocaleFloat("1,234", opts), 1.234)+ })++ it("parses 1.234,56 correctly", () => {+ assert.equal(parseLocaleFloat("1.234,56", opts), 1234.56)+ })++ it("parses simple comma decimal", () => {+ assert.equal(parseLocaleFloat("256,54", opts), 256.54)+ })++ it("parses integer without separators", () => {+ assert.equal(parseLocaleFloat("1234", opts), 1234)+ })++ it("parses negative value", () => {+ assert.equal(parseLocaleFloat("-1.234,56", opts), -1234.56)+ })+ })++ describe("dot separator (English currencies like USD)", () => {+ const opts = { separator: "." }++ it("disambiguates 1,234 as 1234 (English thousands)", () => {+ assert.equal(parseLocaleFloat("1,234", opts), 1234)+ })++ it("parses 1,234.56 correctly", () => {+ assert.equal(parseLocaleFloat("1,234.56", opts), 1234.56)+ })++ it("parses simple dot decimal", () => {+ assert.equal(parseLocaleFloat("256.54", opts), 256.54)+ })++ it("parses integer without separators", () => {+ assert.equal(parseLocaleFloat("1234", opts), 1234)+ })++ it("parses negative value", () => {+ assert.equal(parseLocaleFloat("-1,234.56", opts), -1234.56)+ })+ })++ it("falls back to heuristic when no hint given", () => {+ assert.equal(parseLocaleFloat("1,234"), 1234)+ assert.equal(parseLocaleFloat("256,54"), 256.54)+ })+ })+describe("edge cases", () => {it("returns 0 for empty string", () => {assert.equal(parseLocaleFloat(""), 0)