heartcombo--simple_form/lib/simple_form/wrappers/builder.rb

63 lines
2.0 KiB
Ruby
Raw Normal View History

2011-09-03 08:33:57 +00:00
module SimpleForm
module Wrappers
# Provides the builder syntax for components. The builder provides
# only one method (called `use`) and it allows the following invocations:
#
2011-09-04 09:31:24 +00:00
# config.wrappers do |b|
2011-09-03 08:33:57 +00:00
# # Use a single component
# b.use :placeholder
#
# # Use a component with specific wrapper options
# b.use :error, :tag => "span", :class => "error"
#
# # Use a set of components by wrapping them in a tag+class.
# b.use :tag => "div", :class => "another" do |ba|
# ba.use :label
# ba.use :input
# end
#
# # Use a set of components by wrapping them in a tag+class.
# # This wrapper is identified by :label_input, which means it can
# # be turned off on demand with `f.input :name, :label_input => false`
# b.use :label_input, :tag => "div", :class => "another" do |ba|
# ba.use :label
# ba.use :input
# end
# end
#
# The builder also accepts default options at the root level. This is usually
# used if you want a component to be disabled by default:
#
# config.wrappers :hint => false do |b|
# b.use :hint
# b.use :label_input
# end
#
# In the example above, hint defaults to false, which means it won't automatically
# do the lookup anymore. It will only be triggered when :hint is explicitly set.
2011-09-03 08:33:57 +00:00
class Builder
def initialize
@components = []
end
def use(name, options=nil)
if block_given?
name, options = nil, name if name.is_a?(Hash)
builder = self.class.new
2011-09-05 14:51:04 +00:00
options ||= {}
options[:tag] = :div if options[:tag].nil?
2011-09-03 08:33:57 +00:00
yield builder
2011-09-05 14:51:04 +00:00
@components << Many.new(name, builder.to_a, options)
2011-09-03 08:33:57 +00:00
elsif options
@components << Single.new(name, options)
else
@components << name
end
end
def to_a
@components
end
end
end
end