diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 94641f92..95f6bf57 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -137,7 +137,8 @@ module SimpleForm input = find_input(attribute_name, options) wrapper = find_wrapper(input.input_type, options) - components = (wrapper.components & ATTRIBUTE_COMPONENTS) + [:input] + components = (wrapper.components.map(&:namespace) & ATTRIBUTE_COMPONENTS) + [:input] + components = components.map { |component| SimpleForm::Wrappers::Leaf.new(component) } SimpleForm::Wrappers::Root.new(components, wrapper.options.merge(wrapper: false)).render input end diff --git a/lib/simple_form/wrappers.rb b/lib/simple_form/wrappers.rb index 20241e4c..8aa11e92 100644 --- a/lib/simple_form/wrappers.rb +++ b/lib/simple_form/wrappers.rb @@ -4,5 +4,6 @@ module SimpleForm autoload :Many, 'simple_form/wrappers/many' autoload :Root, 'simple_form/wrappers/root' autoload :Single, 'simple_form/wrappers/single' + autoload :Leaf, 'simple_form/wrappers/leaf' end end diff --git a/lib/simple_form/wrappers/builder.rb b/lib/simple_form/wrappers/builder.rb index 6a00f4f2..685efde4 100644 --- a/lib/simple_form/wrappers/builder.rb +++ b/lib/simple_form/wrappers/builder.rb @@ -49,7 +49,7 @@ module SimpleForm if options && wrapper = options[:wrap_with] @components << Single.new(name, wrapper) else - @components << name + @components << Leaf.new(name) end end diff --git a/lib/simple_form/wrappers/leaf.rb b/lib/simple_form/wrappers/leaf.rb new file mode 100644 index 00000000..06efbbbf --- /dev/null +++ b/lib/simple_form/wrappers/leaf.rb @@ -0,0 +1,19 @@ +module SimpleForm + module Wrappers + class Leaf + attr_reader :namespace + + def initialize(namespace) + @namespace = namespace + end + + def render(input) + input.send(@namespace) + end + + def find(name) + return self if @namespace == name + end + end + end +end diff --git a/lib/simple_form/wrappers/many.rb b/lib/simple_form/wrappers/many.rb index 93819717..6583ca0d 100644 --- a/lib/simple_form/wrappers/many.rb +++ b/lib/simple_form/wrappers/many.rb @@ -25,7 +25,7 @@ module SimpleForm options = input.options components.each do |component| - next if options[component] == false + next if options[component.namespace] == false rendered = component.respond_to?(:render) ? component.render(input) : input.send(component) content.safe_concat rendered.to_s if rendered end diff --git a/lib/simple_form/wrappers/single.rb b/lib/simple_form/wrappers/single.rb index eda04208..d3606a66 100644 --- a/lib/simple_form/wrappers/single.rb +++ b/lib/simple_form/wrappers/single.rb @@ -3,7 +3,7 @@ module SimpleForm # `Single` is an optimization for a wrapper that has only one component. class Single < Many def initialize(name, options={}) - super(name, [name], options) + super(name, [Leaf.new(name)], options) end def render(input)