mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
Deal only with objets in the Wrapper API
This commit is contained in:
parent
8a50572e74
commit
826841294f
6 changed files with 25 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
19
lib/simple_form/wrappers/leaf.rb
Normal file
19
lib/simple_form/wrappers/leaf.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue