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

80 lines
2.6 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
# three methods `use`, `optional` and `wrapper` and they allow the following invocations:
2011-09-03 08:33:57 +00:00
#
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 :html5
#
# # Use the component, but do not automatically lookup. It will only be triggered when
# # :placeholder is explicitly set.
# b.optional :placeholder
2011-09-03 08:33:57 +00:00
#
# # Use a component with specific wrapper options
2013-01-28 21:02:59 +00:00
# b.use :error, wrap_with: { tag: "span", class: "error" }
2011-09-03 08:33:57 +00:00
#
# # Use a set of components by wrapping them in a tag+class.
2013-01-28 21:02:59 +00:00
# b.wrapper tag: "div", class: "another" do |ba|
2011-09-03 08:33:57 +00:00
# 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
2013-01-28 21:02:59 +00:00
# # be turned off on demand with `f.input :name, label_input: false`
# b.wrapper :label_input, tag: "div", class: "another" do |ba|
2011-09-03 08:33:57 +00:00
# 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:
#
2013-01-28 21:02:59 +00:00
# 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(options)
@options = options
2011-09-03 08:33:57 +00:00
@components = []
end
2014-04-22 17:29:42 +00:00
def use(name, options = {})
2012-02-16 13:47:59 +00:00
if options && wrapper = options[:wrap_with]
@components << Single.new(name, wrapper, options.except(:wrap_with))
2012-02-16 13:47:59 +00:00
else
@components << Leaf.new(name, options)
2012-02-16 13:47:59 +00:00
end
end
2014-03-11 22:12:08 +00:00
def optional(name, options = {}, &block)
2012-02-16 13:47:59 +00:00
@options[name] = false
2014-04-22 17:29:42 +00:00
use(name, options)
2012-02-16 13:47:59 +00:00
end
2014-03-11 22:12:08 +00:00
def wrapper(name, options = nil)
2011-09-03 08:33:57 +00:00
if block_given?
name, options = nil, name if name.is_a?(Hash)
builder = self.class.new(@options)
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
else
2012-02-16 13:47:59 +00:00
raise ArgumentError, "A block is required as argument to wrapper"
2011-09-03 08:33:57 +00:00
end
end
2012-02-16 13:47:59 +00:00
def to_a
@components
end
2011-09-03 08:33:57 +00:00
end
end
end