Use the new components builder syntax.
This commit is contained in:
parent
a39f373779
commit
4cacb961ae
|
@ -116,6 +116,20 @@ module SimpleForm
|
|||
yield self
|
||||
end
|
||||
|
||||
def self.components(options={})
|
||||
builder = SimpleForm::Wrappers::Builder.new
|
||||
yield builder
|
||||
self.wrapper = SimpleForm::Wrappers::Root.new(builder.to_a, options)
|
||||
end
|
||||
|
||||
components :tag => :div, :class => :input, :error_class => :field_with_errors do |b|
|
||||
b.use :placeholder
|
||||
b.use :maxlength
|
||||
b.use :label_input
|
||||
b.use :hint, :tag => :span, :class => :hint
|
||||
b.use :error, :tag => :span, :class => :error
|
||||
end
|
||||
|
||||
## DEPRECATED METHODS SINCE 2.0
|
||||
|
||||
# Default tag used on hints.
|
||||
|
@ -146,6 +160,7 @@ module SimpleForm
|
|||
mattr_accessor :wrapper_error_class
|
||||
@@wrapper_error_class = :field_with_errors
|
||||
|
||||
# Define new components using the old array syntax.
|
||||
def self.components=(array)
|
||||
self.wrapper = Wrappers::Root.new(
|
||||
array.map do |item|
|
||||
|
@ -163,7 +178,4 @@ module SimpleForm
|
|||
:error_class => SimpleForm.wrapper_error_class
|
||||
)
|
||||
end
|
||||
|
||||
# Components used by the form builder.
|
||||
self.components = [ :placeholder, :label_input, :hint, :error ]
|
||||
end
|
|
@ -1,5 +1,6 @@
|
|||
module SimpleForm
|
||||
module Wrappers
|
||||
autoload :Builder, 'simple_form/wrappers/builder'
|
||||
autoload :Many, 'simple_form/wrappers/many'
|
||||
autoload :Root, 'simple_form/wrappers/root'
|
||||
autoload :Single, 'simple_form/wrappers/single'
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
module SimpleForm
|
||||
module Wrappers
|
||||
# Provides the builder syntax for components. The builder provides
|
||||
# only one method (called `use`) and it allows the following invocations:
|
||||
#
|
||||
# config.components do |b|
|
||||
# # 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
|
||||
#
|
||||
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
|
||||
yield builder
|
||||
@components << Many.new(name, builder.to_a, options || {})
|
||||
elsif options
|
||||
@components << Single.new(name, options)
|
||||
else
|
||||
@components << name
|
||||
end
|
||||
end
|
||||
|
||||
def to_a
|
||||
@components
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue