2011-09-03 04:33:57 -04: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 05:31:24 -04:00
|
|
|
# config.wrappers do |b|
|
2011-09-03 04:33:57 -04: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
|
|
|
|
#
|
2011-12-04 08:57:12 -05:00
|
|
|
# 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 04:33:57 -04:00
|
|
|
class Builder
|
2012-01-11 15:12:49 -05:00
|
|
|
def initialize(options)
|
|
|
|
@options = options
|
2011-09-03 04:33:57 -04:00
|
|
|
@components = []
|
|
|
|
end
|
|
|
|
|
2012-01-11 15:12:49 -05:00
|
|
|
def use(name, options=nil, &block)
|
2012-02-16 08:47:59 -05:00
|
|
|
if block_given?
|
|
|
|
ActiveSupport::Deprecation.warn "Passing a block to use is deprecated. " \
|
|
|
|
"Please use wrapper instead of use."
|
|
|
|
return wrapper(name, options, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
if options && options.keys != [:wrap_with]
|
|
|
|
ActiveSupport::Deprecation.warn "Passing :tag, :class and others to use is deprecated. " \
|
|
|
|
"Please invoke b.use #{name.inspect}, :wrap_with => #{options.inspect} instead."
|
|
|
|
options = { :wrap_with => options }
|
|
|
|
end
|
|
|
|
|
|
|
|
if options && wrapper = options[:wrap_with]
|
|
|
|
@components << Single.new(name, wrapper)
|
|
|
|
else
|
|
|
|
@components << name
|
|
|
|
end
|
2012-01-11 15:12:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def optional(name, options=nil, &block)
|
2012-02-16 08:47:59 -05:00
|
|
|
if block_given?
|
|
|
|
ActiveSupport::Deprecation.warn "Passing a block to optional is deprecated. " \
|
|
|
|
"Please use wrapper instead of optional."
|
|
|
|
return wrapper(name, options, &block)
|
|
|
|
end
|
2012-01-11 15:12:49 -05:00
|
|
|
|
2012-02-16 08:47:59 -05:00
|
|
|
if options && options.keys != [:wrap_with]
|
|
|
|
ActiveSupport::Deprecation.warn "Passing :tag, :class and others to optional is deprecated. " \
|
|
|
|
"Please invoke b.optional #{name.inspect}, :wrap_with => #{options.inspect} instead."
|
|
|
|
options = { :wrap_with => options }
|
|
|
|
end
|
2012-01-11 15:12:49 -05:00
|
|
|
|
2012-02-16 08:47:59 -05:00
|
|
|
@options[name] = false
|
|
|
|
use(name, options, &block)
|
|
|
|
end
|
2012-01-11 15:12:49 -05:00
|
|
|
|
2012-02-16 08:47:59 -05:00
|
|
|
def wrapper(name, options=nil)
|
2011-09-03 04:33:57 -04:00
|
|
|
if block_given?
|
|
|
|
name, options = nil, name if name.is_a?(Hash)
|
2012-01-11 15:12:49 -05:00
|
|
|
builder = self.class.new(@options)
|
2011-09-05 10:51:04 -04:00
|
|
|
options ||= {}
|
2011-11-09 09:34:21 -05:00
|
|
|
options[:tag] = :div if options[:tag].nil?
|
2011-09-03 04:33:57 -04:00
|
|
|
yield builder
|
2011-09-05 10:51:04 -04:00
|
|
|
@components << Many.new(name, builder.to_a, options)
|
2011-09-03 04:33:57 -04:00
|
|
|
else
|
2012-02-16 08:47:59 -05:00
|
|
|
raise ArgumentError, "A block is required as argument to wrapper"
|
2011-09-03 04:33:57 -04:00
|
|
|
end
|
|
|
|
end
|
2012-02-16 08:47:59 -05:00
|
|
|
|
|
|
|
def to_a
|
|
|
|
@components
|
|
|
|
end
|
2011-09-03 04:33:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|