2011-09-02 18:33:03 +00:00
|
|
|
module SimpleForm
|
|
|
|
module Wrappers
|
2011-09-03 07:15:47 +00:00
|
|
|
# A wrapper is an object that holds several components and render them.
|
2014-03-11 22:52:05 +00:00
|
|
|
# A component may be any object that responds to `render`.
|
2011-09-03 07:15:47 +00:00
|
|
|
# This API allows inputs/components to be easily wrapped, removing the
|
|
|
|
# need to modify the code only to wrap input in an extra tag.
|
|
|
|
#
|
|
|
|
# `Many` represents a wrapper around several components at the same time.
|
|
|
|
# It may optionally receive a namespace, allowing it to be configured
|
|
|
|
# on demand on input generation.
|
2011-09-02 18:33:03 +00:00
|
|
|
class Many
|
|
|
|
attr_reader :namespace, :defaults, :components
|
|
|
|
|
2014-03-11 22:12:08 +00:00
|
|
|
def initialize(namespace, components, defaults = {})
|
2011-09-02 18:33:03 +00:00
|
|
|
@namespace = namespace
|
2011-09-03 07:08:36 +00:00
|
|
|
@components = components
|
|
|
|
@defaults = defaults
|
2012-02-18 14:47:02 +00:00
|
|
|
@defaults[:tag] = :div unless @defaults.key?(:tag)
|
2012-12-13 17:03:17 +00:00
|
|
|
@defaults[:class] = Array(@defaults[:class])
|
2011-09-02 18:33:03 +00:00
|
|
|
end
|
|
|
|
|
2011-09-03 07:01:47 +00:00
|
|
|
def render(input)
|
2011-09-02 18:33:03 +00:00
|
|
|
content = "".html_safe
|
|
|
|
options = input.options
|
|
|
|
|
|
|
|
components.each do |component|
|
2014-03-10 21:06:55 +00:00
|
|
|
next if options[component.namespace] == false
|
2014-03-10 21:13:05 +00:00
|
|
|
rendered = component.render(input)
|
2011-09-02 18:33:03 +00:00
|
|
|
content.safe_concat rendered.to_s if rendered
|
|
|
|
end
|
|
|
|
|
|
|
|
wrap(input, options, content)
|
|
|
|
end
|
|
|
|
|
2011-09-03 10:24:47 +00:00
|
|
|
def find(name)
|
|
|
|
return self if namespace == name
|
|
|
|
|
|
|
|
@components.each do |c|
|
|
|
|
if c.is_a?(Symbol)
|
|
|
|
return nil if c == namespace
|
|
|
|
elsif value = c.find(name)
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
2011-09-02 20:14:23 +00:00
|
|
|
end
|
|
|
|
|
2011-09-02 18:33:03 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def wrap(input, options, content)
|
2011-09-03 07:15:47 +00:00
|
|
|
return content if options[namespace] == false
|
2014-06-23 19:11:35 +00:00
|
|
|
return if defaults[:unless_blank] && content.empty?
|
2011-09-03 07:15:47 +00:00
|
|
|
|
2011-09-03 07:08:36 +00:00
|
|
|
tag = (namespace && options[:"#{namespace}_tag"]) || @defaults[:tag]
|
2011-09-02 18:33:03 +00:00
|
|
|
return content unless tag
|
|
|
|
|
2011-09-02 20:14:23 +00:00
|
|
|
klass = html_classes(input, options)
|
2012-06-27 02:23:40 +00:00
|
|
|
opts = html_options(options)
|
2011-09-02 20:14:23 +00:00
|
|
|
opts[:class] = (klass << opts[:class]).join(' ').strip unless klass.empty?
|
2011-09-02 18:33:03 +00:00
|
|
|
input.template.content_tag(tag, content, opts)
|
|
|
|
end
|
2011-09-02 20:14:23 +00:00
|
|
|
|
2012-06-27 02:23:40 +00:00
|
|
|
def html_options(options)
|
2014-03-11 23:27:10 +00:00
|
|
|
(@defaults[:html] || {}).merge(options[:"#{namespace}_html"] || {})
|
2012-06-27 02:23:40 +00:00
|
|
|
end
|
|
|
|
|
2011-09-02 20:14:23 +00:00
|
|
|
def html_classes(input, options)
|
|
|
|
@defaults[:class].dup
|
|
|
|
end
|
2011-09-02 18:33:03 +00:00
|
|
|
end
|
|
|
|
end
|
2011-09-11 21:46:47 +00:00
|
|
|
end
|