1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00
heartcombo--simple_form/lib/simple_form/wrappers/many.rb

50 lines
1.3 KiB
Ruby
Raw Normal View History

2011-09-02 14:33:03 -04:00
module SimpleForm
module Wrappers
class Many
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)
@defaults = args.extract_options!
2011-09-02 14:33:03 -04:00
@namespace = namespace
@components = args
@defaults[:class] = Array.wrap(@defaults[:class])
2011-09-02 14:33:03 -04:00
end
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
def each
@components.each { |c| yield(c) }
end
2011-09-02 14:33:03 -04:00
private
def wrap(input, options, content)
tag = options[:"#{namespace}_tag"] || @defaults[:tag]
2011-09-02 14:33:03 -04:00
return content unless tag
klass = html_classes(input, options)
2011-09-02 14:33:03 -04:00
opts = options[:"#{namespace}_html"] || {}
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
def html_classes(input, options)
@defaults[:class].dup
end
2011-09-02 14:33:03 -04:00
end
end
end