2011-09-02 14:33:03 -04:00
|
|
|
module SimpleForm
|
|
|
|
module Wrappers
|
|
|
|
class Many
|
2011-09-02 16:14:23 -04:00
|
|
|
include Enumerable
|
|
|
|
|
2011-09-02 14:33:03 -04:00
|
|
|
attr_reader :namespace, :defaults, :components
|
2011-09-03 03:04:16 -04:00
|
|
|
alias :to_sym :namespace
|
2011-09-02 14:33:03 -04:00
|
|
|
|
|
|
|
def initialize(namespace, *args)
|
2011-09-02 16:14:23 -04:00
|
|
|
@defaults = args.extract_options!
|
2011-09-02 14:33:03 -04:00
|
|
|
@namespace = namespace
|
|
|
|
@components = args
|
2011-09-02 16:14:23 -04:00
|
|
|
@defaults[:class] = Array.wrap(@defaults[:class])
|
2011-09-02 14:33:03 -04:00
|
|
|
end
|
|
|
|
|
2011-09-03 03:01:47 -04:00
|
|
|
def render(input)
|
2011-09-02 14:33:03 -04:00
|
|
|
content = "".html_safe
|
|
|
|
options = input.options
|
|
|
|
|
|
|
|
components.each do |component|
|
|
|
|
next if options[component] == false
|
2011-09-03 03:04:16 -04:00
|
|
|
rendered = component.respond_to?(:render) ? component.render(input) : input.send(component)
|
2011-09-02 14:33:03 -04:00
|
|
|
content.safe_concat rendered.to_s if rendered
|
|
|
|
end
|
|
|
|
|
|
|
|
wrap(input, options, content)
|
|
|
|
end
|
|
|
|
|
2011-09-02 16:14:23 -04:00
|
|
|
def each
|
|
|
|
@components.each { |c| yield(c) }
|
|
|
|
end
|
|
|
|
|
2011-09-02 14:33:03 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def wrap(input, options, content)
|
2011-09-02 16:14:23 -04:00
|
|
|
tag = options[:"#{namespace}_tag"] || @defaults[:tag]
|
2011-09-02 14:33:03 -04:00
|
|
|
return content unless tag
|
|
|
|
|
2011-09-02 16:14:23 -04:00
|
|
|
klass = html_classes(input, options)
|
2011-09-02 14:33:03 -04:00
|
|
|
opts = options[:"#{namespace}_html"] || {}
|
2011-09-02 16:14:23 -04:00
|
|
|
opts[:class] = (klass << opts[:class]).join(' ').strip unless klass.empty?
|
2011-09-02 14:33:03 -04:00
|
|
|
input.template.content_tag(tag, content, opts)
|
|
|
|
end
|
2011-09-02 16:14:23 -04:00
|
|
|
|
|
|
|
def html_classes(input, options)
|
|
|
|
@defaults[:class].dup
|
|
|
|
end
|
2011-09-02 14:33:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|