The personal finance app for everyone (by everyone)
Ruby
Show the hint passed to form fields as help_text (#3822)
Guillem Arias Fauste515dc657ed02
diff --git a/app/helpers/styled_form_builder.rb b/app/helpers/styled_form_builder.rbindex e03ef83ec6..688e4cd7b9 100644--- a/app/helpers/styled_form_builder.rb+++ b/app/helpers/styled_form_builder.rb@@ -6,11 +6,16 @@ class StyledFormBuilder < ActionView::Helpers::FormBuilderNON_TEXT_FIELD_HELPERS = [ :label, :check_box, :checkbox, :radio_button, :fields_for, :fields, :hidden_field, :file_field ].freezeclass_attribute :text_field_helpers, default: field_helpers - NON_TEXT_FIELD_HELPERS+ # Options the builder handles itself instead of passing them to the input as+ # HTML attributes. `required` is left out because it's both: the builder adds+ # the label's asterisk and the input keeps the attribute.+ BUILDER_OPTIONS = [ :label, :label_tooltip, :inline, :container_class, :help_text ].freeze+text_field_helpers.each do |selector|class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1def #{selector}(method, options = {})- form_options = options.slice(:label, :label_tooltip, :inline, :container_class, :required)- html_options = options.except(:label, :label_tooltip, :inline, :container_class)+ form_options = options.slice(*BUILDER_OPTIONS, :required)+ html_options = options.except(*BUILDER_OPTIONS)build_field(method, form_options, html_options) do |merged_options|super(method, merged_options)@@ -129,8 +134,17 @@ class StyledFormBuilder < ActionView::Helpers::FormBuilderprivatedef build_field(method, options = {}, html_options = {}, &block)+ if options[:help_text].present?+ help_text_id = field_id(method, :help_text)+ describedby = [ html_options.dig(:aria, :describedby), help_text_id ].compact.join(" ")+ html_options = html_options.deep_merge(aria: { describedby: describedby })+ help_text_element = @template.tag.p(options[:help_text], id: help_text_id, class: "text-xs text-secondary px-1")+ end++ # Bare fields keep their layout, so the hint follows the input unwrapped.if options[:inline] || options[:label] == false- return yield({ class: "form-field__input" }.merge(html_options))+ field_element = yield({ class: "form-field__input" }.merge(html_options))+ return help_text_element ? field_element + help_text_element : field_elementendlabel_element = build_label(method, options)@@ -138,7 +152,7 @@ class StyledFormBuilder < ActionView::Helpers::FormBuildercontainer_classes = [ "form-field", options[:container_class] ].compact- @template.tag.div class: container_classes do+ container = @template.tag.div class: container_classes doif options[:label_tooltip]@template.tag.div(class: "form-field__header") dolabel_element +@@ -155,6 +169,10 @@ class StyledFormBuilder < ActionView::Helpers::FormBuilderendendend++ return container unless help_text_element++ @template.tag.div(class: "space-y-1") { container + help_text_element }enddef normalize_options(options, html_options)diff --git a/config/brakeman.ignore b/config/brakeman.ignoreindex 8ddff990ae..a6bb3e36ea 100644--- a/config/brakeman.ignore+++ b/config/brakeman.ignore@@ -141,13 +141,13 @@{"warning_type": "Dangerous Eval","warning_code": 13,- "fingerprint": "ad8e31fe9321feba741d551654a6fb0c0c7cfe8abee81822e6143404817bff5b",+ "fingerprint": "abc0f66938760081100a003a9ce62aae3fc27a3e62bcaa026a009da6a2ed99d1","check_name": "Evaluation","message": "Dynamic string evaluated as code","file": "app/helpers/styled_form_builder.rb",- "line": 10,+ "line": 15,"link": "https://brakemanscanner.org/docs/warning_types/dangerous_eval/",- "code": "class_eval(\" def #{selector}(method, options = {})\\n form_options = options.slice(:label, :label_tooltip, :inline, :container_class, :required)\\n html_options = options.except(:label, :label_tooltip, :inline, :container_class)\\n\\n build_field(method, form_options, html_options) do |merged_options|\\n super(method, merged_options)\\n end\\n end\\n\", \"app/helpers/styled_form_builder.rb\", (10 + 1))",+ "code": "class_eval(\" def #{selector}(method, options = {})\\n form_options = options.slice(*BUILDER_OPTIONS, :required)\\n html_options = options.except(*BUILDER_OPTIONS)\\n\\n build_field(method, form_options, html_options) do |merged_options|\\n super(method, merged_options)\\n end\\n end\\n\", \"app/helpers/styled_form_builder.rb\", (15 + 1))","render_path": null,"location": {"type": "method",diff --git a/test/controllers/settings/api_keys_controller_test.rb b/test/controllers/settings/api_keys_controller_test.rbindex 74e85d4fba..b19469ff4f 100644--- a/test/controllers/settings/api_keys_controller_test.rb+++ b/test/controllers/settings/api_keys_controller_test.rb@@ -32,6 +32,15 @@ class Settings::ApiKeysControllerTest < ActionDispatch::IntegrationTestassert_response :successend+ test "new shows the name hint under the field and points the input at it" do+ get new_settings_api_key_path+ assert_response :success++ assert_select "p#api_key_name_help_text", text: "Choose a descriptive name to help you identify this key later."+ assert_select "input[name='api_key[name]'][aria-describedby=?]", "api_key_name_help_text"+ assert_select "input[help_text]", count: 0+ end+test "create makes a new key without revoking existing keys" doexisting = ApiKey.create!(user: @user,diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rbindex e4ea15064d..5c0b929b55 100644--- a/test/helpers/application_helper_test.rb+++ b/test/helpers/application_helper_test.rb@@ -86,6 +86,16 @@ class ApplicationHelperTest < ActionView::TestCase@account3 = Account.new(currency: "EUR", balance: -7)end+ test "#styled_form_with keeps a field's help_text when the field has no label" do+ html = styled_form_with(url: "/", scope: :account) do |form|+ form.text_field :name, label: false, help_text: "Shown to everyone in the family"+ end++ fragment = Nokogiri::HTML.fragment(html)+ assert_equal "Shown to everyone in the family", fragment.at("p#account_name_help_text")&.text+ assert_equal "account_name_help_text", fragment.at("input[name='account[name]']")["aria-describedby"]+ end+test "#totals_by_currency(collection: collection, money_method: money_method)" doassert_equal "$3.00", totals_by_currency(collection: [ @account1, @account2 ], money_method: :balance_money)assert_equal "$3.00 | -€7.00", totals_by_currency(collection: [ @account1, @account2, @account3 ], money_method: :balance_money)