From 246c7977a96d5a04434f7d186f4099f88f45f359 Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Wed, 26 Jun 2024 10:51:12 +0200 Subject: [PATCH] Pass form object to human_attribute_name when it's called from full_error. Follow up of https://github.com/heartcombo/simple_form/pull/1812 --- lib/simple_form/form_builder.rb | 2 +- test/components/label_test.rb | 5 +++++ test/form_builder/error_test.rb | 7 +++++++ test/support/models.rb | 2 ++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index d7cf621b2..20a99a4b4 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -274,7 +274,7 @@ def full_error(attribute_name, options = {}) options = options.dup options[:error_prefix] ||= if object.class.respond_to?(:human_attribute_name) - object.class.human_attribute_name(attribute_name.to_s) + object.class.human_attribute_name(attribute_name.to_s, { base: object }) else attribute_name.to_s.humanize end diff --git a/test/components/label_test.rb b/test/components/label_test.rb index bffaa0090..fa4b5519d 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -26,6 +26,11 @@ def with_label_for(object, attribute_name, type, options = {}) assert_select 'label[for=user_description]', /User Description!/ end + test 'label uses human_attribute_name and passed object as an option to it' do + with_label_for @user, :status, :text + assert_select 'label[for=user_status]', /\[#{@user.id}\] User Status!/ + end + test 'label uses human attribute name based on association name' do with_label_for @user, :company_id, :string, setup_association: true assert_select 'label', /Company Human Name!/ diff --git a/test/form_builder/error_test.rb b/test/form_builder/error_test.rb index a9e5a4955..0d7877f53 100644 --- a/test/form_builder/error_test.rb +++ b/test/form_builder/error_test.rb @@ -163,6 +163,13 @@ def with_full_error_for(object, *args) assert_no_select 'span.error b', 'markup' end + test 'full error uses human_attribute_name and passed object as an option to it' do + @user.errors.add(:status, 'error') + with_full_error_for @user, :status + + assert_select 'span.error', "\[#{@user.id}\] User Status! error" + end + # CUSTOM WRAPPERS test 'error with custom wrappers works' do diff --git a/test/support/models.rb b/test/support/models.rb index 17e0a53ce..7405fcbb9 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -208,6 +208,8 @@ def self.human_attribute_name(attribute, options = {}) 'Super User Name!' when 'description' 'User Description!' + when 'status' + "[#{options[:base].id}] User Status!" when 'company' 'Company Human Name!' else