Skip to content

Commit 246c797

Browse files
committed
Pass form object to human_attribute_name when it's called from full_error.
Follow up of #1812
1 parent b8abb1b commit 246c797

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

lib/simple_form/form_builder.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def full_error(attribute_name, options = {})
274274
options = options.dup
275275

276276
options[:error_prefix] ||= if object.class.respond_to?(:human_attribute_name)
277-
object.class.human_attribute_name(attribute_name.to_s)
277+
object.class.human_attribute_name(attribute_name.to_s, { base: object })
278278
else
279279
attribute_name.to_s.humanize
280280
end

test/components/label_test.rb

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ def with_label_for(object, attribute_name, type, options = {})
2626
assert_select 'label[for=user_description]', /User Description!/
2727
end
2828

29+
test 'label uses human_attribute_name and passed object as an option to it' do
30+
with_label_for @user, :status, :text
31+
assert_select 'label[for=user_status]', /\[#{@user.id}\] User Status!/
32+
end
33+
2934
test 'label uses human attribute name based on association name' do
3035
with_label_for @user, :company_id, :string, setup_association: true
3136
assert_select 'label', /Company Human Name!/

test/form_builder/error_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ def with_full_error_for(object, *args)
163163
assert_no_select 'span.error b', 'markup'
164164
end
165165

166+
test 'full error uses human_attribute_name and passed object as an option to it' do
167+
@user.errors.add(:status, 'error')
168+
with_full_error_for @user, :status
169+
170+
assert_select 'span.error', "\[#{@user.id}\] User Status! error"
171+
end
172+
166173
# CUSTOM WRAPPERS
167174

168175
test 'error with custom wrappers works' do

test/support/models.rb

+2
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ def self.human_attribute_name(attribute, options = {})
208208
'Super User Name!'
209209
when 'description'
210210
'User Description!'
211+
when 'status'
212+
"[#{options[:base].id}] User Status!"
211213
when 'company'
212214
'Company Human Name!'
213215
else

0 commit comments

Comments
 (0)