Add "logo" variant in account dropdown on transfer form (#1241)

* feat: Add :logo variant in account dropdown on transfer form

* fix test

* fix test

* fix: avoid multiple queries on accounts
This commit is contained in:
Alessio Cappa
2026-03-22 23:59:57 +01:00
committed by GitHub
parent d6a0a3c588
commit ddf1c732d3
3 changed files with 16 additions and 10 deletions

View File

@@ -6,6 +6,13 @@ class TransfersController < ApplicationController
def new
@transfer = Transfer.new
@from_account_id = params[:from_account_id]
@accounts = Current.family.accounts
.alphabetically
.includes(
:account_providers,
logo_attachment: :blob
)
end
def show

View File

@@ -14,8 +14,9 @@
<% account_ids << params[:to_account_id] if params[:to_account_id].present? %>
<% account_ids << transfer.from_account_id if transfer.respond_to?(:from_account_id) && transfer.from_account_id.present? %>
<% account_ids << transfer.to_account_id if transfer.respond_to?(:to_account_id) && transfer.to_account_id.present? %>
<% selected_accounts = @accounts.select { |a| account_ids.include?(a.id) } %>
<% if account_ids.any? && Current.family.accounts.where(id: account_ids).any? { |a| a.investment? || a.crypto? } %>
<% if selected_accounts.any? { |a| a.investment? || a.crypto? } %>
<% show_type_tabs = false %>
<% end %>
@@ -26,8 +27,8 @@
<% end %>
<section class="space-y-2">
<%= f.collection_select :from_account_id, Current.family.accounts.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".from"), selected: @from_account_id }, required: true %>
<%= f.collection_select :to_account_id, Current.family.accounts.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".to") }, required: true %>
<%= f.collection_select :from_account_id, @accounts, :id, :name, { prompt: t(".select_account"), label: t(".from"), selected: @from_account_id, variant: :logo }, required: true %>
<%= f.collection_select :to_account_id, @accounts, :id, :name, { prompt: t(".select_account"), label: t(".to"), variant: :logo }, required: true %>
<%= f.number_field :amount, label: t(".amount"), required: true, min: 0, placeholder: "100", step: 0.00000001 %>
<%= f.date_field :date, value: transfer.inflow_transaction&.entry&.date || Date.current, label: t(".date"), required: true, max: Date.current %>
</section>

View File

@@ -7,8 +7,6 @@ class TransfersTest < ApplicationSystemTestCase
end
test "can create a transfer" do
checking_name = accounts(:depository).name
savings_name = accounts(:credit_card).name
transfer_date = Date.current
click_on "New transaction"
@@ -16,8 +14,8 @@ class TransfersTest < ApplicationSystemTestCase
assert_text "New transfer"
# Select accounts using DS::Select
select_ds("From", checking_name)
select_ds("To", savings_name)
select_ds("From", accounts(:depository))
select_ds("To", accounts(:credit_card))
fill_in "transfer[amount]", with: 500
fill_in "Date", with: transfer_date
@@ -31,7 +29,7 @@ class TransfersTest < ApplicationSystemTestCase
private
def select_ds(label_text, option_text)
def select_ds(label_text, record)
field_label = find("label", exact_text: label_text)
container = field_label.ancestor("div.relative")
@@ -40,13 +38,13 @@ class TransfersTest < ApplicationSystemTestCase
# If searchable, type in the search input
if container.has_selector?("input[type='search']", visible: true)
container.find("input[type='search']", visible: true).set(option_text)
container.find("input[type='search']", visible: true).set(record.name)
end
# Wait for the listbox to appear inside the relative container
listbox = container.find("[role='listbox']", visible: true)
# Click the option inside the listbox
listbox.find("[role='option']", exact_text: option_text, visible: true).click
listbox.find("[role='option'][data-value='#{record.id}']", visible: true).click
end
end