The personal finance app for everyone (by everyone)
Ruby
Fix goal form amount width and consume dialog amount input (#3801)
Guillem Arias Fauste264be00f8548
diff --git a/app/views/goals/_form.html.erb b/app/views/goals/_form.html.erbindex f6de2a6660..e139b10fba 100644--- a/app/views/goals/_form.html.erb+++ b/app/views/goals/_form.html.erb@@ -70,8 +70,11 @@<p class="mt-1 text-xs text-secondary"><%= t("goals.form.fields.target_months_hint") %></p></div>- <div class="grid grid-cols-2 gap-3 items-start">- <div data-goal-kind-target="amountField">+ <%# Flex, not a two-column grid. A reserve hides the date, and a hidden grid+ cell still holds its column, which left the amount at half the width of+ the months field above it. Flex children share whatever is visible. %>+ <div class="flex gap-3 items-start">+ <div data-goal-kind-target="amountField" class="flex-1 min-w-0"><%= f.money_field :target_amount,label: t("goals.form.fields.target_amount"),hide_currency: true,@@ -85,7 +88,7 @@that are the actual question — so it is shown as what it is. Onlywhen there is spending to work it out from, though: with none, thecontroller leaves the field above in place and this stays hidden. %>- <div data-goal-kind-target="amountDerived" class="hidden">+ <div data-goal-kind-target="amountDerived" class="hidden flex-1 min-w-0"><p class="form-field__label"><%= t("goals.form.fields.target_balance") %></p><p class="mt-1 text-lg font-medium text-primary tabular-nums privacy-sensitive"><% if goal.target_amount.to_d.positive? %>@@ -96,7 +99,7 @@</p><p class="mt-1 text-xs text-subdued"><%= t("goals.form.fields.target_balance_hint") %></p></div>- <div data-goal-kind-target="dateField">+ <div data-goal-kind-target="dateField" class="flex-1 min-w-0"><%= f.date_field :target_date,label: t("goals.form.fields.target_date"),data: { goal_form_target: "dateInput", action: "input->goal-form#suggestedChanged" } %>diff --git a/app/views/goals/consume.html.erb b/app/views/goals/consume.html.erbindex eddb3f5dbc..4f4da5bed3 100644--- a/app/views/goals/consume.html.erb+++ b/app/views/goals/consume.html.erb@@ -6,17 +6,20 @@<% dialog.with_body do %><%= styled_form_with url: consume_goal_path(@goal), method: :post, class: "space-y-4" do |f| %>- <div class="space-y-1">- <%= label_tag :amount, t(".amount_label"), class: "text-sm text-secondary" %>- <%= number_field_tag :amount, nil,- step: Money::Currency.new(@goal.currency).step,- min: Money::Currency.new(@goal.currency).step,- max: (@goal.target_amount.to_d - @goal.consumed_amount.to_d).to_f,- required: true,- autocomplete: "off",- autofocus: true,- class: "form-field__input w-full text-right tabular-nums privacy-sensitive" %>- <p class="text-xs text-secondary">+ <div class="space-y-1.5">+ <%# The builder's money field, like the goal form's own amount: the+ currency symbol and locale formatting come with it, and a bare+ number input read as a different control from every other amount. %>+ <%= f.money_field :amount,+ label: t(".amount_label"),+ currency_value_override: @goal.currency,+ hide_currency: true,+ min: Money::Currency.new(@goal.currency).step,+ max: (@goal.target_amount.to_d - @goal.consumed_amount.to_d).to_f,+ required: true,+ autofocus: true,+ input_class: "privacy-sensitive privacy-sensitive-interactive" %>+ <p class="text-xs text-secondary privacy-sensitive"><%= t(".amount_hint", saved: @goal.current_balance_money.format(precision: 0)) %></p></div>diff --git a/app/views/shared/_money_field.html.erb b/app/views/shared/_money_field.html.erbindex 00b23d2373..399d29cfec 100644--- a/app/views/shared/_money_field.html.erb+++ b/app/views/shared/_money_field.html.erb@@ -13,7 +13,7 @@<% if options[:step].present? %> data-money-field-step-value="<%= options[:step] %>"<% end %>><% if options[:label_tooltip] %><div class="form-field__header">- <%= form.label options[:label] || t(".label"), class: "form-field__label" do %>+ <%= form.label amount_method, class: "form-field__label" do %><%= options[:label] || t(".label") %><% if options[:required] %><span class="text-red-500 ml-0.5">*</span>@@ -32,7 +32,7 @@<div class="form-field__body"><% unless options[:label_tooltip] %>- <%= form.label options[:label] || t(".label"), class: "form-field__label" do %>+ <%= form.label amount_method, class: "form-field__label" do %><%= options[:label] || t(".label") %><% if options[:required] %><span class="text-red-500 ml-0.5">*</span>@@ -47,7 +47,7 @@</span><%= form.number_field amount_method,- class: "form-field__input",+ class: class_names("form-field__input", options[:input_class]),inline: true,placeholder: "100",value: if options[:value]@@ -60,6 +60,7 @@max: options[:max] || 99999999999999,step: options[:step] || currency.step,disabled: options[:disabled],+ autofocus: options[:autofocus],data: (options[:amount_data] || {}).merge({"money-field-target": "amount","auto-submit-form-target": ("auto" if options[:auto_submit]),diff --git a/test/controllers/goals_controller_test.rb b/test/controllers/goals_controller_test.rbindex 04eab4ae39..6b9d2cb587 100644--- a/test/controllers/goals_controller_test.rb+++ b/test/controllers/goals_controller_test.rb@@ -138,6 +138,27 @@ class GoalsControllerTest < ActionDispatch::IntegrationTestassert_no_match(/#{private_account.id}/, response.body)end+ # The builder's money field, not a bare number input: currency symbol and+ # all. Its label used to point at nothing — `for` carried the label text —+ # so the check is that it names the input the amount is posted from.+ test "the consumption dialog asks for the amount with a labelled money field" do+ get consume_goal_url(@goal)++ assert_response :success+ assert_select "[data-controller='money-field'] input[type=number][name=amount][id=amount][autofocus]"+ assert_select "label[for=amount]", text: /#{I18n.t("goals.consume.amount_label")}/+ end++ # Both figures in the dialog blur under privacy mode, as they do on the goal+ # page. The amount also stays clickable: it is the one field the reader fills.+ test "the consumption dialog blurs its figures under privacy mode and the amount stays editable" do+ get consume_goal_url(@goal)++ assert_response :success+ assert_select "input[name=amount].form-field__input.privacy-sensitive.privacy-sensitive-interactive"+ assert_select "p.privacy-sensitive", text: /is currently backing this goal/+ end+test "consumption is refused against a linked account the viewer cannot see" doprivate_account = private_linked_accountlink = @goal.goal_accounts.find_by(account_id: private_account.id)