From 4cacb961aed6643f8022e6dbe19057ccb4072d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 3 Sep 2011 10:33:57 +0200 Subject: [PATCH] Use the new components builder syntax. --- lib/simple_form.rb | 18 ++++++++-- lib/simple_form/wrappers.rb | 1 + lib/simple_form/wrappers/builder.rb | 51 +++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 lib/simple_form/wrappers/builder.rb diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 026afe98..01daf3c4 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -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 \ No newline at end of file diff --git a/lib/simple_form/wrappers.rb b/lib/simple_form/wrappers.rb index c05c4cb4..95a89855 100644 --- a/lib/simple_form/wrappers.rb +++ b/lib/simple_form/wrappers.rb @@ -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' diff --git a/lib/simple_form/wrappers/builder.rb b/lib/simple_form/wrappers/builder.rb new file mode 100644 index 00000000..f61aa95e --- /dev/null +++ b/lib/simple_form/wrappers/builder.rb @@ -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 \ No newline at end of file