Skip to content

Commit

Permalink
Pass form object to human_attribute_name when it's called from full_e…
Browse files Browse the repository at this point in the history
…rror.

Follow up of #1812
  • Loading branch information
nashby committed Jun 26, 2024
1 parent b8abb1b commit 246c797
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/simple_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/components/label_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!/
Expand Down
7 changes: 7 additions & 0 deletions test/form_builder/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 246c797

Please sign in to comment.