Class: SimpleFormWithClientValidation::Wrappers::Builder
- Inherits:
-
Object
- Object
- SimpleFormWithClientValidation::Wrappers::Builder
- Defined in:
- lib/simple_form_with_client_validation/wrappers/builder.rb
Overview
Provides the builder syntax for components. The builder provides three methods ‘use`, `optional` and `wrapper` and they allow the following invocations:
config.wrappers do |b|
# 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
# Use a component with specific wrapper options
b.use :error, :wrap_with => { :tag => "span", :class => "error" }
# Use a set of components by wrapping them in a tag+class.
b.wrapper :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.wrapper :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.
Instance Method Summary collapse
-
#initialize(options) ⇒ Builder
constructor
A new instance of Builder.
- #optional(name, options = nil, &block) ⇒ Object
- #to_a ⇒ Object
-
#use(name, options = nil, &block) ⇒ Object
this triggers the use of this option.
- #wrapper(name, options = nil) ⇒ Object
Constructor Details
#initialize(options) ⇒ Builder
Returns a new instance of Builder.
43 44 45 46 |
# File 'lib/simple_form_with_client_validation/wrappers/builder.rb', line 43 def initialize() @options = @components = [] end |
Instance Method Details
#optional(name, options = nil, &block) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/simple_form_with_client_validation/wrappers/builder.rb', line 69 def optional(name, =nil, &block) if block_given? ActiveSupport::Deprecation.warn "Passing a block to optional is deprecated. " \ "Please use wrapper instead of optional." return wrapper(name, , &block) end if && .keys != [:wrap_with] ActiveSupport::Deprecation.warn "Passing :tag, :class and others to optional is deprecated. " \ "Please invoke b.optional #{name.inspect}, :wrap_with => #{.inspect} instead." = { :wrap_with => } end @options[name] = false use(name, , &block) end |
#to_a ⇒ Object
110 111 112 |
# File 'lib/simple_form_with_client_validation/wrappers/builder.rb', line 110 def to_a @components end |
#use(name, options = nil, &block) ⇒ Object
this triggers the use of this option
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/simple_form_with_client_validation/wrappers/builder.rb', line 49 def use(name, =nil, &block) if block_given? ActiveSupport::Deprecation.warn "Passing a block to use is deprecated. " \ "Please use wrapper instead of use." return wrapper(name, , &block) end if && .keys != [:wrap_with] ActiveSupport::Deprecation.warn "Passing :tag, :class and others to use is deprecated. " \ "Please invoke b.use #{name.inspect}, :wrap_with => #{.inspect} instead." = { :wrap_with => } end if && wrapper = [:wrap_with] @components << Single.new(name, wrapper) else @components << name end end |
#wrapper(name, options = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/simple_form_with_client_validation/wrappers/builder.rb', line 86 def wrapper(name, =nil) if block_given? #setting name to nil if only a hash passed in name, = nil, name if name.is_a?(Hash) #create a builder based on current options of parent builder = self.class.new(@options) ||= {} #use div if nothing else supplied [:tag] = :div if [:tag].nil? #now run the block setup stuff (use, optional, and wrappers) yield builder #turn this builder into a Many object (wrapper for multiple @components) and #add to parent @components @components << Many.new(name, builder.to_a, ) else raise ArgumentError, "A block is required as argument to wrapper" end end |