Add attribute for to label in collection extension

This commit is contained in:
Erich Kist 2013-11-18 18:01:00 -02:00
parent 18c3cecedc
commit 958bcf4a32
2 changed files with 19 additions and 7 deletions

View File

@ -15,7 +15,19 @@ module SimpleForm
rendered_item = yield item, value, text, default_html_options.merge(additional_html_options)
item_wrapper_tag ? @template_object.content_tag(item_wrapper_tag, rendered_item, class: item_wrapper_class) : rendered_item
if item_wrapper_tag
options = { class: item_wrapper_class }
if item_wrapper_tag.to_s == 'label'
label_options = {}
add_default_name_and_id_for_value(text, label_options)
options['for'] = label_options['id']
end
@template_object.content_tag(item_wrapper_tag, rendered_item, options)
else
rendered_item
end
end.join.html_safe
end

View File

@ -48,8 +48,8 @@ class CollectionRadioButtonsInputTest < ActionView::TestCase
with_input_for @user, :name, :radio_buttons, collection: ['Jose', 'Carlos']
assert_select 'input[type=radio][value=Jose]'
assert_select 'input[type=radio][value=Carlos]'
assert_select 'label.collection_radio_buttons', 'Jose'
assert_select 'label.collection_radio_buttons', 'Carlos'
assert_select 'label.collection_radio_buttons[for=user_name_jose]', 'Jose'
assert_select 'label.collection_radio_buttons[for=user_name_carlos]', 'Carlos'
end
test 'input should do automatic collection translation for radio types using defaults key' do
@ -59,8 +59,8 @@ class CollectionRadioButtonsInputTest < ActionView::TestCase
with_input_for @user, :gender, :radio_buttons, collection: [:male, :female]
assert_select 'input[type=radio][value=male]'
assert_select 'input[type=radio][value=female]'
assert_select 'label.collection_radio_buttons', 'Male'
assert_select 'label.collection_radio_buttons', 'Female'
assert_select 'label.collection_radio_buttons[for=user_gender_male]', 'Male'
assert_select 'label.collection_radio_buttons[for=user_gender_female]', 'Female'
end
end
@ -71,8 +71,8 @@ class CollectionRadioButtonsInputTest < ActionView::TestCase
with_input_for @user, :gender, :radio_buttons, collection: [:male, :female]
assert_select 'input[type=radio][value=male]'
assert_select 'input[type=radio][value=female]'
assert_select 'label.collection_radio_buttons', 'Male'
assert_select 'label.collection_radio_buttons', 'Female'
assert_select 'label.collection_radio_buttons[for=user_gender_male]', 'Male'
assert_select 'label.collection_radio_buttons[for=user_gender_female]', 'Female'
end
end