Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Show the year and the gap between legs on the Auto Matches page (#5777)

Sandy McArthur, Jr.e0fc7cff6b87
diff --git a/app/helpers/auto_matches_helper.rb b/app/helpers/auto_matches_helper.rb
new file mode 100644
index 0000000000..dd6d6fc908
--- /dev/null
+++ b/app/helpers/auto_matches_helper.rb
@@ -0,0 +1,21 @@
+module AutoMatchesHelper
+ # A first sync can pull years of history, so pending matches are often
+ # old. "Oct 30" alone reads as this October; the date gains the year
+ # whenever it falls in another one. Same rule as BillsHelper#bills_upcoming_date.
+ def auto_match_date(date)
+ l(date, format: date.year == Date.current.year ? :short : :short_with_year)
+ end
+
+ # The auto matcher pairs legs that are a few days apart, but the row shows
+ # one date (the inflow's, Transfer#date). The gap helps tell a same-day
+ # move from a coincidental amount match. Nil when the legs share a date
+ # or one is missing.
+ def auto_match_days_apart(transfer)
+ outflow_date = transfer.outflow_transaction&.entry&.date
+ inflow_date = transfer.inflow_transaction&.entry&.date
+ return if outflow_date.nil? || inflow_date.nil?
+
+ days = (inflow_date - outflow_date).to_i.abs
+ days.zero? ? nil : days
+ end
+end
diff --git a/app/views/auto_matches/_pending_transfer.html.erb b/app/views/auto_matches/_pending_transfer.html.erb
index ba7015f870..9b5e5b2f9e 100644
--- a/app/views/auto_matches/_pending_transfer.html.erb
+++ b/app/views/auto_matches/_pending_transfer.html.erb
@@ -37,7 +37,10 @@
<div class="text-xs text-secondary privacy-sensitive"><%= format_money(transfer.inflow_transaction.entry.amount_money.abs) %></div>
</td>
<td class="py-3 px-2 text-sm text-secondary">
- <%= l(transfer.date, format: :short) %>
+ <div class="whitespace-nowrap"><%= auto_match_date(transfer.date) %></div>
+ <% if (days_apart = auto_match_days_apart(transfer)) %>
+ <div class="text-xs text-secondary whitespace-nowrap"><%= t("auto_matches.table.days_apart", count: days_apart) %></div>
+ <% end %>
</td>
<td class="py-3 px-2 text-sm text-right">
<% if writable %>
diff --git a/config/locales/views/auto_matches/ca.yml b/config/locales/views/auto_matches/ca.yml
index 14c4ad58ff..92d4d98f5e 100644
--- a/config/locales/views/auto_matches/ca.yml
+++ b/config/locales/views/auto_matches/ca.yml
@@ -23,6 +23,9 @@ ca:
from: Des de
to: A
date: Data
+ days_apart:
+ one: "%{count} dia de diferència"
+ other: "%{count} dies de diferència"
actions: Accions
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/de.yml b/config/locales/views/auto_matches/de.yml
index a9664db0b5..f4dde170a4 100644
--- a/config/locales/views/auto_matches/de.yml
+++ b/config/locales/views/auto_matches/de.yml
@@ -23,6 +23,9 @@ de:
from: Von
to: An
date: Datum
+ days_apart:
+ one: "%{count} Tag auseinander"
+ other: "%{count} Tage auseinander"
actions: Aktionen
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/en.yml b/config/locales/views/auto_matches/en.yml
index e29c4fe5ad..272379f10b 100644
--- a/config/locales/views/auto_matches/en.yml
+++ b/config/locales/views/auto_matches/en.yml
@@ -23,6 +23,9 @@ en:
from: From
to: To
date: Date
+ days_apart:
+ one: 1 day apart
+ other: "%{count} days apart"
actions: Actions
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/es.yml b/config/locales/views/auto_matches/es.yml
index b5849a6db3..7122d8786d 100644
--- a/config/locales/views/auto_matches/es.yml
+++ b/config/locales/views/auto_matches/es.yml
@@ -23,6 +23,9 @@ es:
from: Desde
to: Hasta
date: Fecha
+ days_apart:
+ one: "%{count} día de diferencia"
+ other: "%{count} días de diferencia"
actions: Acciones
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/fr.yml b/config/locales/views/auto_matches/fr.yml
index ac874909fe..6c148f770d 100644
--- a/config/locales/views/auto_matches/fr.yml
+++ b/config/locales/views/auto_matches/fr.yml
@@ -23,6 +23,9 @@ fr:
from: De
to: Vers
date: Date
+ days_apart:
+ one: "%{count} jour d'écart"
+ other: "%{count} jours d'écart"
actions: Actions
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/hu.yml b/config/locales/views/auto_matches/hu.yml
index 0af5f8ce86..bdcfef86fe 100644
--- a/config/locales/views/auto_matches/hu.yml
+++ b/config/locales/views/auto_matches/hu.yml
@@ -23,6 +23,9 @@ hu:
from: Honnan
to: Hová
date: Dátum
+ days_apart:
+ one: "%{count} nap eltérés"
+ other: "%{count} nap eltérés"
actions: Műveletek
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/id.yml b/config/locales/views/auto_matches/id.yml
index c7be590b1c..799b54c0ba 100644
--- a/config/locales/views/auto_matches/id.yml
+++ b/config/locales/views/auto_matches/id.yml
@@ -23,6 +23,9 @@ id:
from: Dari
to: Ke
date: Tanggal
+ days_apart:
+ one: "Selisih %{count} hari"
+ other: "Selisih %{count} hari"
actions: Tindakan
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/it.yml b/config/locales/views/auto_matches/it.yml
index e8d3140c82..9c5fc6833c 100644
--- a/config/locales/views/auto_matches/it.yml
+++ b/config/locales/views/auto_matches/it.yml
@@ -23,6 +23,9 @@ it:
from: Da
to: A
date: Data
+ days_apart:
+ one: "%{count} giorno di distanza"
+ other: "%{count} giorni di distanza"
actions: Azioni
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/nb.yml b/config/locales/views/auto_matches/nb.yml
index c8713fb8cd..11d4f5e7a0 100644
--- a/config/locales/views/auto_matches/nb.yml
+++ b/config/locales/views/auto_matches/nb.yml
@@ -23,6 +23,9 @@ nb:
from: "Fra"
to: "Til"
date: "Dato"
+ days_apart:
+ one: "%{count} dag mellom"
+ other: "%{count} dager mellom"
actions: "Handlinger"
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/nl.yml b/config/locales/views/auto_matches/nl.yml
index bbc0b1b1e8..2ced0366b0 100644
--- a/config/locales/views/auto_matches/nl.yml
+++ b/config/locales/views/auto_matches/nl.yml
@@ -23,6 +23,9 @@ nl:
from: Van
to: Naar
date: Datum
+ days_apart:
+ one: "%{count} dag ertussen"
+ other: "%{count} dagen ertussen"
actions: Acties
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/pl.yml b/config/locales/views/auto_matches/pl.yml
index 10bef2a414..7f721f3c4f 100644
--- a/config/locales/views/auto_matches/pl.yml
+++ b/config/locales/views/auto_matches/pl.yml
@@ -27,6 +27,11 @@ pl:
from: Od
to: Do
date: Data
+ days_apart:
+ one: "%{count} dzień różnicy"
+ few: "%{count} dni różnicy"
+ many: "%{count} dni różnicy"
+ other: "%{count} dnia różnicy"
actions: Działania
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/pt-BR.yml b/config/locales/views/auto_matches/pt-BR.yml
index d4cefde744..b706065545 100644
--- a/config/locales/views/auto_matches/pt-BR.yml
+++ b/config/locales/views/auto_matches/pt-BR.yml
@@ -23,6 +23,9 @@ pt-BR:
from: De
to: Para
date: Data
+ days_apart:
+ one: "%{count} dia de diferença"
+ other: "%{count} dias de diferença"
actions: Ações
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/pt-PT.yml b/config/locales/views/auto_matches/pt-PT.yml
index cceb3c742f..565742689d 100644
--- a/config/locales/views/auto_matches/pt-PT.yml
+++ b/config/locales/views/auto_matches/pt-PT.yml
@@ -23,6 +23,9 @@ pt-PT:
from: De
to: Para
date: Data
+ days_apart:
+ one: "%{count} dia de diferença"
+ other: "%{count} dias de diferença"
actions: Ações
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/ro.yml b/config/locales/views/auto_matches/ro.yml
index f2eea846f7..5c958d6a40 100644
--- a/config/locales/views/auto_matches/ro.yml
+++ b/config/locales/views/auto_matches/ro.yml
@@ -25,6 +25,10 @@ ro:
from: De la
to: Către
date: Data
+ days_apart:
+ one: "%{count} zi diferență"
+ few: "%{count} zile diferență"
+ other: "%{count} de zile diferență"
actions: Acțiuni
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/ru.yml b/config/locales/views/auto_matches/ru.yml
index e332e82d16..583490cb28 100644
--- a/config/locales/views/auto_matches/ru.yml
+++ b/config/locales/views/auto_matches/ru.yml
@@ -27,6 +27,11 @@ ru:
from: Откуда
to: Куда
date: Дата
+ days_apart:
+ one: "%{count} день разницы"
+ few: "%{count} дня разницы"
+ many: "%{count} дней разницы"
+ other: "%{count} дня разницы"
actions: Действия
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/tr.yml b/config/locales/views/auto_matches/tr.yml
index dd27528c2d..7af69f5e7c 100644
--- a/config/locales/views/auto_matches/tr.yml
+++ b/config/locales/views/auto_matches/tr.yml
@@ -23,6 +23,9 @@ tr:
from: Kimden
to: Kime
date: Tarih
+ days_apart:
+ one: "%{count} gün arayla"
+ other: "%{count} gün arayla"
actions: İşlemler
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/uk.yml b/config/locales/views/auto_matches/uk.yml
index 178c09a885..a99d87d857 100644
--- a/config/locales/views/auto_matches/uk.yml
+++ b/config/locales/views/auto_matches/uk.yml
@@ -27,6 +27,11 @@ uk:
from: Звідки
to: Куди
date: Дата
+ days_apart:
+ one: "%{count} день різниці"
+ few: "%{count} дні різниці"
+ many: "%{count} днів різниці"
+ other: "%{count} дня різниці"
actions: Дії
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/vi.yml b/config/locales/views/auto_matches/vi.yml
index 7b0c8373d6..dd2d03b928 100644
--- a/config/locales/views/auto_matches/vi.yml
+++ b/config/locales/views/auto_matches/vi.yml
@@ -23,6 +23,9 @@ vi:
from: Từ
to: Đến
date: Ngày
+ days_apart:
+ one: "Cách nhau %{count} ngày"
+ other: "Cách nhau %{count} ngày"
actions: Hành động
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/zh-CN.yml b/config/locales/views/auto_matches/zh-CN.yml
index f6b1c7d915..191b035b32 100644
--- a/config/locales/views/auto_matches/zh-CN.yml
+++ b/config/locales/views/auto_matches/zh-CN.yml
@@ -23,6 +23,9 @@ zh-CN:
from: 从
to: 到
date: 日期
+ days_apart:
+ one: "相隔 %{count} 天"
+ other: "相隔 %{count} 天"
actions: 操作
separator: "·"
bulk_actions:
diff --git a/config/locales/views/auto_matches/zh-TW.yml b/config/locales/views/auto_matches/zh-TW.yml
index 08550698f5..d628c08a60 100644
--- a/config/locales/views/auto_matches/zh-TW.yml
+++ b/config/locales/views/auto_matches/zh-TW.yml
@@ -19,6 +19,9 @@ zh-TW:
from: 從
to: 到
date: 日期
+ days_apart:
+ one: "相隔 %{count} 天"
+ other: "相隔 %{count} 天"
actions: 操作
separator: "·"
bulk_actions:
diff --git a/test/controllers/auto_matches_controller_test.rb b/test/controllers/auto_matches_controller_test.rb
index 98a7ce9855..2b6243790b 100644
--- a/test/controllers/auto_matches_controller_test.rb
+++ b/test/controllers/auto_matches_controller_test.rb
@@ -29,6 +29,19 @@ class AutoMatchesControllerTest < ActionDispatch::IntegrationTest
assert_select "tr##{ActionView::RecordIdentifier.dom_id(outflow_entry.transaction.transfer)}", false
end
+ test "index shows the year of a past-year match and how far apart its legs are" do
+ outflow_entry = create_transaction(date: 2.years.ago.to_date, account: accounts(:depository), amount: 500)
+ inflow_entry = create_transaction(date: outflow_entry.date + 3.days, account: accounts(:credit_card), amount: -500)
+ @family.auto_match_transfers!
+
+ get auto_matches_url
+ assert_response :success
+
+ row = "tr##{ActionView::RecordIdentifier.dom_id(outflow_entry.transaction.transfer)}"
+ assert_select row, text: /#{Regexp.escape(I18n.l(inflow_entry.date, format: :short_with_year))}/
+ assert_select row, text: /#{Regexp.escape(I18n.t("auto_matches.table.days_apart", count: 3))}/
+ end
+
test "index excludes matches where the outflow account is private to another family member" do
# other_asset is owned by family_admin and not shared with family_member;
# depository is shared with family_member (full_control).
diff --git a/test/helpers/auto_matches_helper_test.rb b/test/helpers/auto_matches_helper_test.rb
new file mode 100644
index 0000000000..649447b5d6
--- /dev/null
+++ b/test/helpers/auto_matches_helper_test.rb
@@ -0,0 +1,31 @@
+require "test_helper"
+require "ostruct"
+
+class AutoMatchesHelperTest < ActionView::TestCase
+ test "a match date gains the year only when it falls in another one" do
+ travel_to Date.new(2026, 11, 24) do
+ assert_equal I18n.l(Date.new(2026, 10, 30), format: :short), auto_match_date(Date.new(2026, 10, 30))
+ assert_equal I18n.l(Date.new(2020, 10, 30), format: :short_with_year), auto_match_date(Date.new(2020, 10, 30))
+ end
+
+ travel_to Date.new(2027, 1, 2) do
+ assert_equal I18n.l(Date.new(2026, 12, 31), format: :short_with_year), auto_match_date(Date.new(2026, 12, 31))
+ assert_equal I18n.l(Date.new(2027, 1, 1), format: :short), auto_match_date(Date.new(2027, 1, 1))
+ end
+ end
+
+ test "days apart counts the gap between the legs either way and is nil on the same day" do
+ assert_equal 3, auto_match_days_apart(transfer_with(Date.new(2020, 10, 27), Date.new(2020, 10, 30)))
+ assert_equal 3, auto_match_days_apart(transfer_with(Date.new(2020, 10, 30), Date.new(2020, 10, 27)))
+ assert_nil auto_match_days_apart(transfer_with(Date.new(2020, 10, 30), Date.new(2020, 10, 30)))
+ assert_nil auto_match_days_apart(OpenStruct.new(outflow_transaction: nil, inflow_transaction: nil))
+ end
+
+ private
+ def transfer_with(outflow_date, inflow_date)
+ OpenStruct.new(
+ outflow_transaction: OpenStruct.new(entry: OpenStruct.new(date: outflow_date)),
+ inflow_transaction: OpenStruct.new(entry: OpenStruct.new(date: inflow_date))
+ )
+ end
+end
diff --git a/test/i18n_test.rb b/test/i18n_test.rb
index 5f3856b166..53646c4f58 100644
--- a/test/i18n_test.rb
+++ b/test/i18n_test.rb
@@ -145,6 +145,17 @@ class I18nTest < ActiveSupport::TestCase
assert_empty missing, "bills.index.notices_heading is missing for: #{missing.join(", ")}"
end
+ # The Auto Matches date cell adds "N days apart" under the date. Fallbacks
+ # would show it in English inside an otherwise translated table.
+ def test_auto_matches_days_apart_exists_for_each_auto_matches_locale
+ locales = Dir[File.expand_path("../config/locales/views/auto_matches/*.yml", __dir__)].map { |file| File.basename(file, ".yml") }
+ missing = locales.sort.reject do |locale|
+ I18n.t("auto_matches.table.days_apart", locale: locale, count: 3, fallback: false, default: nil)
+ end
+
+ assert_empty missing, "auto_matches.table.days_apart is missing for: #{missing.join(", ")}"
+ end
+
# The account status speaks wherever the holdings tab is translated.
def test_account_health_status_keys_exist_wherever_the_holdings_tab_is_translated
{