Accept options in label component too

This commit is contained in:
Rafael Mendonça França 2014-03-11 11:22:20 -03:00
parent df47a8599d
commit 51bdd20246
3 changed files with 35 additions and 3 deletions

View File

@ -22,10 +22,16 @@ module SimpleForm
end
def label(context=nil)
if generate_label_for_attribute?
@builder.label(label_target, label_text, label_html_options)
if context
label_options = merged_label_options(context.options)
else
template.label_tag(nil, label_text, label_html_options)
label_options = label_html_options
end
if generate_label_for_attribute?
@builder.label(label_target, label_text, label_options)
else
template.label_tag(nil, label_text, label_options)
end
end
@ -46,6 +52,7 @@ module SimpleForm
if options.key?(:input_html) && options[:input_html].key?(:id)
label_options[:for] = options[:input_html][:id]
end
label_options
end
@ -74,6 +81,14 @@ module SimpleForm
def generate_label_for_attribute?
true
end
def merged_label_options(context_options)
label_html_options.merge(context_options) do |_, oldval, newval|
if Array === oldval
oldval + Array(newval)
end
end
end
end
end
end

View File

@ -221,4 +221,14 @@ class WrapperTest < ActionView::TestCase
assert_select "div.custom_wrapper input.string.inline-class"
end
test 'label accepts attributes in the DSL' do
swap_wrapper :default, self.custom_wrapper_with_label_class do
with_concat_form_for @user do |f|
concat f.input :name
end
end
assert_select "div.custom_wrapper label.string.inline-class"
end
end

View File

@ -75,6 +75,13 @@ module MiscHelpers
end
end
def custom_wrapper_with_label_class
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.use :label, class: 'inline-class'
b.use :input
end
end
def custom_wrapper_with_wrapped_input
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.wrapper tag: :div, class: 'elem' do |component|