The personal finance app for everyone (by everyone)
Ruby
Explain a pending transfer match on its pill and open the transfer (#4787)
Yong Cong Sinba91753f1fdd
diff --git a/app/helpers/transactions_helper.rb b/app/helpers/transactions_helper.rbindex 7fb659ddc3..4ec21a1fb3 100644--- a/app/helpers/transactions_helper.rb+++ b/app/helpers/transactions_helper.rb@@ -351,4 +351,13 @@ module TransactionsHelperrescue StandardErrorvalue.to_send++ # A pending transfer match as the viewer may see it from `transaction`'s+ # row. The accounts and transactions pages set @accessible_account_ids;+ # a view that does not, such as the turbo stream that redraws a match,+ # looks the viewer's accessible accounts up once per request.+ def pending_transfer_match(transaction)+ @transfer_match_visible_account_ids ||= (@accessible_account_ids || accessible_accounts.pluck(:id)).to_set+ Transfer::PendingMatch.new(transaction, visible_account_ids: @transfer_match_visible_account_ids)+ endenddiff --git a/app/models/transfer/pending_match.rb b/app/models/transfer/pending_match.rbnew file mode 100644index 0000000000..1975a1535a--- /dev/null+++ b/app/models/transfer/pending_match.rb@@ -0,0 +1,37 @@+# A pending transfer match seen from one of its legs: the other leg, and how+# much of it a viewer may be shown. The other leg's account is named only to+# a viewer who can see it, and the transfer is linked only then, as its page+# needs access to both legs.+class Transfer::PendingMatch+ attr_reader :transaction, :transfer++ def initialize(transaction, visible_account_ids:)+ @transaction = transaction+ @transfer = transaction.transfer+ @visible_account_ids = visible_account_ids.to_set+ end++ def counterpart_entry+ (outflow_leg? ? transfer.inflow_transaction : transfer.outflow_transaction)&.entry+ end++ def counterpart_account+ outflow_leg? ? transfer.to_account : transfer.from_account+ end++ def counterpart_visible?+ account = counterpart_account+ account.present? && @visible_account_ids.include?(account.id)+ end++ # A leg whose kind already makes its row open the transfer needs no second+ # link to it.+ def links_to_transfer?+ counterpart_visible? && !transaction.transfer?+ end++ private+ def outflow_leg?+ transfer.outflow_transaction_id == transaction.id+ end+enddiff --git a/app/views/transactions/_transfer_match.html.erb b/app/views/transactions/_transfer_match.html.erbindex 3b24218606..eb88b24eca 100644--- a/app/views/transactions/_transfer_match.html.erb+++ b/app/views/transactions/_transfer_match.html.erb@@ -6,22 +6,59 @@<%= icon "link-2", size: "sm", class: "text-secondary" %></span><% elsif transaction.transfer.pending? %>- <span class="hidden lg:flex items-center">- <%= render DS::Pill.new(- label: t("transactions.transfer_match.auto_matched"),- tone: :neutral,- marker: false,- show_dot: false- ) %>- </span>- <span class="flex items-center lg:hidden">- <%= render DS::Pill.new(- label: t("transactions.transfer_match.auto_matched_short"),- tone: :neutral,- marker: false,- show_dot: false- ) %>- </span>+ <%# The pill's tooltip gives the other side of the suggested match (see+ Transfer::PendingMatch) and, when the viewer may see it, the pill+ opens the transfer. The tooltip is the design system's rather than a+ native title, so the amount in it is blurred in privacy mode. %>+ <% match = pending_transfer_match(transaction) %>+ <% pills = capture do %>+ <%# Empty titles: the tooltip below replaces the pills' native ones. %>+ <span class="hidden lg:flex items-center">+ <%= render DS::Pill.new(+ label: t("transactions.transfer_match.auto_matched"),+ tone: :neutral,+ marker: false,+ show_dot: false,+ title: "",+ label_testid: "transfer-match-pill"+ ) %>+ </span>+ <span class="flex items-center lg:hidden">+ <%= render DS::Pill.new(+ label: t("transactions.transfer_match.auto_matched_short"),+ tone: :neutral,+ marker: false,+ show_dot: false,+ title: "",+ label_testid: "transfer-match-pill"+ ) %>+ </span>+ <% end %>+ <%= render DS::Tooltip.new(placement: "top", html_class: "min-w-0") do |tooltip| %>+ <% tooltip.with_trigger do %>+ <%# The focusable element itself names the tooltip as its description:+ an ancestor's aria-describedby is not announced for it. %>+ <% if match.links_to_transfer? %>+ <%= link_to transfer_path(match.transfer),+ class: "flex items-center rounded-full hover:opacity-80 focus-ring",+ aria: { describedby: tooltip.tooltip_id },+ data: { turbo_frame: "drawer", turbo_prefetch: false } do %>+ <%= pills %>+ <% end %>+ <% else %>+ <%# Its own tab stop, so keyboard users can open the tooltip. %>+ <span class="flex items-center rounded-full focus-ring" tabindex="0" aria-describedby="<%= tooltip.tooltip_id %>"><%= pills %></span>+ <% end %>+ <% end %>+ <% if match.counterpart_visible? %>+ <%= t("transactions.transfer_match.auto_matched_tooltip_html",+ amount: tag.span(format_money(match.counterpart_entry.amount_money.abs), class: "privacy-sensitive"),+ account: match.counterpart_account.name,+ date: l(match.counterpart_entry.date, format: :long)) %>+ <% else %>+ <%= t("transactions.transfer_match.auto_matched_tooltip_hidden") %>+ <% end %>+ <% end %><div class="flex items-center gap-1"><%= render DS::Button.new(diff --git a/config/locales/views/transactions/en.yml b/config/locales/views/transactions/en.ymlindex bf44a48de5..816bc80ea7 100644--- a/config/locales/views/transactions/en.yml+++ b/config/locales/views/transactions/en.yml@@ -170,6 +170,8 @@ en:transfer_match:auto_matched: Match?auto_matched_short: Match?+ auto_matched_tooltip_html: "%{amount} in %{account} on %{date}. Click to review."+ auto_matched_tooltip_hidden: A transaction in an account you cannot see.confirm_match: Confirm matchpayment_confirmed: Payment is confirmedreject_match: Reject matchdiff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rbindex 2ca5c0decf..f4b1cba03f 100644--- a/test/controllers/transactions_controller_test.rb+++ b/test/controllers/transactions_controller_test.rb@@ -100,6 +100,66 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest"the preview-gated bill link must not render for a user without the flag"end+ test "index explains a pending transfer match in its tooltip and opens the transfer" do+ outflow = create_transaction(account: accounts(:depository), name: "Match outflow", amount: 320, date: Date.current)+ inflow = create_transaction(account: accounts(:credit_card), name: "Match inflow", amount: -320, date: Date.current)+ transfer = Transfer.create!(outflow_transaction: outflow.transaction, inflow_transaction: inflow.transaction, status: "pending")++ get transactions_url++ assert_response :success+ match = "##{dom_id(outflow.transaction, :transfer_match)}"+ assert_select "#{match} a[href='#{transfer_path(transfer)}'][data-turbo-frame=drawer]" do+ assert_select "span", text: I18n.t("transactions.transfer_match.auto_matched"), minimum: 1+ end+ tooltip = "$320.00 in #{accounts(:credit_card).name} on #{I18n.l(Date.current, format: :long)}. Click to review."+ assert_select "#{match} [role=tooltip]", text: tooltip+ # The amount is blurred in privacy mode, which a native title would escape.+ assert_select "#{match} [role=tooltip] span.privacy-sensitive", text: "$320.00"+ assert_select "#{match} [title*='320']", count: 0+ # The link, which takes focus, is described by the tooltip itself.+ tooltip_id = css_select("#{match} [role=tooltip]").first["id"]+ assert_select "#{match} a[href='#{transfer_path(transfer)}'][aria-describedby=?]", tooltip_id++ # The index shows one leg of a pair; the other leg's row is on its own account's page.+ get account_url(accounts(:credit_card))++ assert_response :success+ assert_select "##{dom_id(inflow.transaction, :transfer_match)} a[href='#{transfer_path(transfer)}']"+ assert_select "##{dom_id(inflow.transaction, :transfer_match)} [role=tooltip]", text: /#{Regexp.escape(accounts(:depository).name)}/+ end++ test "index does not name or link a pending match's other side in an account the viewer cannot see" do+ # depository is shared with family_member; investment is not.+ outflow = create_transaction(account: accounts(:depository), name: "Private match outflow", amount: 320, date: Date.current)+ inflow = create_transaction(account: accounts(:investment), name: "Private match inflow", amount: -320, date: Date.current)+ transfer = Transfer.create!(outflow_transaction: outflow.transaction, inflow_transaction: inflow.transaction, status: "pending")+ sign_in users(:family_member)++ get transactions_url++ assert_response :success+ match = "##{dom_id(outflow.transaction, :transfer_match)}"+ assert_select "#{match} [role=tooltip]", text: I18n.t("transactions.transfer_match.auto_matched_tooltip_hidden")+ assert_select "#{match} a[href='#{transfer_path(transfer)}']", count: 0+ assert_select "#{match} span[tabindex='0'] [data-testid='transfer-match-pill']", minimum: 1+ assert_select "#{match} span[tabindex='0'][aria-describedby=?]", css_select("#{match} [role=tooltip]").first["id"]+ assert_select match, text: /#{Regexp.escape(accounts(:investment).name)}/, count: 0+ end++ test "index gives a pending match on a transfer row no second link to the transfer" do+ outflow = create_transaction(account: accounts(:depository), name: "Moved match outflow", amount: 320, date: Date.current, kind: "funds_movement")+ inflow = create_transaction(account: accounts(:credit_card), name: "Moved match inflow", amount: -320, date: Date.current)+ transfer = Transfer.create!(outflow_transaction: outflow.transaction, inflow_transaction: inflow.transaction, status: "pending")++ get transactions_url++ assert_response :success+ match = "##{dom_id(outflow.transaction, :transfer_match)}"+ assert_select "#{match} a[href='#{transfer_path(transfer)}']", count: 0+ assert_select "#{match} [role=tooltip]", text: /#{Regexp.escape(accounts(:credit_card).name)}/+ end+test "index points an unmatched transaction with a counterpart to the transfer matcher" dooutflow = create_transaction(account: accounts(:depository), name: "Hint outflow", amount: 500, date: Date.current)inflow = create_transaction(account: accounts(:credit_card), name: "Hint inflow", amount: -500, date: Date.current)diff --git a/test/models/transfer/pending_match_test.rb b/test/models/transfer/pending_match_test.rbnew file mode 100644index 0000000000..764fccce41--- /dev/null+++ b/test/models/transfer/pending_match_test.rb@@ -0,0 +1,40 @@+require "test_helper"++class Transfer::PendingMatchTest < ActiveSupport::TestCase+ include EntriesTestHelper++ setup do+ @outflow = create_transaction(account: accounts(:depository), amount: 320, date: Date.current)+ @inflow = create_transaction(account: accounts(:credit_card), amount: -320, date: Date.current - 1)+ @transfer = Transfer.create!(outflow_transaction: @outflow.transaction, inflow_transaction: @inflow.transaction, status: "pending")+ @everything = [ accounts(:depository).id, accounts(:credit_card).id ]+ end++ test "each leg's counterpart is the other leg and its account" do+ from_outflow = Transfer::PendingMatch.new(@outflow.transaction.reload, visible_account_ids: @everything)+ from_inflow = Transfer::PendingMatch.new(@inflow.transaction.reload, visible_account_ids: @everything)++ assert_equal [ @inflow, accounts(:credit_card) ], [ from_outflow.counterpart_entry, from_outflow.counterpart_account ]+ assert_equal [ @outflow, accounts(:depository) ], [ from_inflow.counterpart_entry, from_inflow.counterpart_account ]+ assert_equal @transfer, from_outflow.transfer+ end++ test "the counterpart is shown and the transfer linked only to a viewer who can see the other account" do+ seen = Transfer::PendingMatch.new(@outflow.transaction.reload, visible_account_ids: @everything)+ hidden = Transfer::PendingMatch.new(@outflow.transaction.reload, visible_account_ids: [ accounts(:depository).id ])++ assert seen.counterpart_visible?+ assert seen.links_to_transfer?+ assert_not hidden.counterpart_visible?+ assert_not hidden.links_to_transfer?+ end++ test "a leg that is already a transfer row gets no second link to the transfer" do+ @outflow.transaction.update!(kind: "funds_movement")++ match = Transfer::PendingMatch.new(@outflow.transaction.reload, visible_account_ids: @everything)++ assert match.counterpart_visible?+ assert_not match.links_to_transfer?+ end+enddiff --git a/test/system/transactions_test.rb b/test/system/transactions_test.rbindex 9b16546904..a28873535b 100644--- a/test/system/transactions_test.rb+++ b/test/system/transactions_test.rb@@ -209,7 +209,7 @@ class TransactionsTest < ApplicationSystemTestCasevisit transactions_url(per_page: @page_size)assert_selector match- pill_bounds = find("#{match} [title='#{I18n.t("transactions.transfer_match.auto_matched")}']", visible: true).evaluate_script("this.getBoundingClientRect().toJSON()")+ pill_bounds = find("#{match} [data-testid='transfer-match-pill']", visible: true).evaluate_script("this.getBoundingClientRect().toJSON()")amount_bounds = find("##{dom_id(outflow)} p.privacy-sensitive").evaluate_script("this.getBoundingClientRect().toJSON()")assert_in_delta pill_bounds["y"] + pill_bounds["height"] / 2,amount_bounds["y"] + amount_bounds["height"] / 2, 0.5