Allow direct setting of for attribute on label, only overide with :input_html => :id if :input_html also contains a key :id

This commit is contained in:
Bert Goethals 2010-12-30 16:39:06 +01:00 committed by Carlos Antonio da Silva
parent 2f1b079c6e
commit a484b560d1
2 changed files with 11 additions and 1 deletions

View File

@ -37,7 +37,7 @@ module SimpleForm
def label_html_options
label_options = html_options_for(:label, [input_type, required_class])
label_options[:for] = options[:input_html][:id] if options.key?(:input_html)
label_options[:for] = options[:input_html][:id] if options.key?(:input_html) && options[:input_html].key?(:id)
label_options
end

View File

@ -170,6 +170,16 @@ class LabelTest < ActionView::TestCase
assert_select 'label[for=my_new_id]'
end
test 'label should allow overwriting of for attribute' do
with_label_for @user, :name, :string, :label_html => { :for => 'my_new_id' }
assert_select 'label[for=my_new_id]'
end
test 'label should allow overwriting of for attribute with input_html not containing id' do
with_label_for @user, :name, :string, :label_html => { :for => 'my_new_id' }, :input_html => {:class => 'foo'}
assert_select 'label[for=my_new_id]'
end
test 'label should use default input id when it was not overridden' do
with_label_for @user, :name, :string, :input_html => { :class => 'my_new_id' }
assert_select 'label[for=user_name]'