Make wrapper a component.
This commit is contained in:
parent
c255e0a504
commit
7329eb4c72
|
@ -15,10 +15,14 @@ module SimpleForm
|
|||
# Components used by the form builder.
|
||||
mattr_accessor :components
|
||||
@@components = [
|
||||
SimpleForm::Components::Label, SimpleForm::Components::Input,
|
||||
SimpleForm::Components::Hint, SimpleForm::Components::Error
|
||||
SimpleForm::Components::Wrapper, SimpleForm::Components::Label,
|
||||
SimpleForm::Components::Input, SimpleForm::Components::Hint, SimpleForm::Components::Error
|
||||
]
|
||||
|
||||
# The terminator sent to the last component
|
||||
mattr_accessor :terminator
|
||||
@@terminator = lambda { "" }
|
||||
|
||||
# Series of attemps to detect a default label method for collection
|
||||
mattr_accessor :collection_label_methods
|
||||
@@collection_label_methods = [ :name, :title, :to_s ]
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
module SimpleForm
|
||||
module Components
|
||||
autoload :Base, 'simple_form/components/base'
|
||||
autoload :Error, 'simple_form/components/error'
|
||||
autoload :Hint, 'simple_form/components/hint'
|
||||
autoload :Input, 'simple_form/components/input'
|
||||
autoload :Label, 'simple_form/components/label'
|
||||
autoload :Base, 'simple_form/components/base'
|
||||
autoload :Error, 'simple_form/components/error'
|
||||
autoload :Hint, 'simple_form/components/hint'
|
||||
autoload :Input, 'simple_form/components/input'
|
||||
autoload :Label, 'simple_form/components/label'
|
||||
autoload :Wrapper, 'simple_form/components/wrapper'
|
||||
end
|
||||
end
|
|
@ -7,11 +7,16 @@ module SimpleForm
|
|||
@basename ||= name.split("::").last.underscore.to_sym
|
||||
end
|
||||
|
||||
def initialize(builder, attribute, input_type, options)
|
||||
def initialize(builder, component)
|
||||
@builder = builder
|
||||
@attribute = attribute
|
||||
@input_type = input_type
|
||||
@options = options
|
||||
@component = component
|
||||
@attribute = @builder.attribute
|
||||
@input_type = @builder.input_type
|
||||
@options = @builder.options
|
||||
end
|
||||
|
||||
def call
|
||||
generate + @component.call
|
||||
end
|
||||
|
||||
def generate
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
module SimpleForm
|
||||
module Components
|
||||
class Wrapper < Base
|
||||
def call
|
||||
if SimpleForm.wrapper_tag
|
||||
template.content_tag(SimpleForm.wrapper_tag, @component.call)
|
||||
else
|
||||
@component.call
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,38 +1,30 @@
|
|||
module SimpleForm
|
||||
class FormBuilder < ActionView::Helpers::FormBuilder
|
||||
# Make the template accessible for components
|
||||
attr_reader :template
|
||||
attr_reader :template, :object_name, :object, :attribute, :input_type, :options
|
||||
|
||||
def input(attribute, options={})
|
||||
input_type = default_input_type(attribute, options)
|
||||
@attribute, @options = attribute, options
|
||||
@input_type = default_input_type
|
||||
|
||||
pieces = SimpleForm.components.collect do |klass|
|
||||
next if options[klass.basename] == false
|
||||
klass.new(self, attribute, input_type, options).generate
|
||||
component = SimpleForm.terminator
|
||||
SimpleForm.components.reverse.each do |klass|
|
||||
next if @options[klass.basename] == false
|
||||
component = klass.new(self, component)
|
||||
end
|
||||
|
||||
wrap_content(pieces.compact.join)
|
||||
component.call
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def wrap_content(content)
|
||||
if SimpleForm.wrapper_tag
|
||||
@template.content_tag(SimpleForm.wrapper_tag, content)
|
||||
else
|
||||
content
|
||||
end
|
||||
end
|
||||
|
||||
def default_input_type(attribute, options)
|
||||
return options[:as].to_sym if options[:as]
|
||||
return :select if options[:collection]
|
||||
def default_input_type
|
||||
return @options[:as].to_sym if @options[:as]
|
||||
return :select if @options[:collection]
|
||||
|
||||
input_type = if @object.respond_to?(:column_for_attribute)
|
||||
column = @object.column_for_attribute(attribute)
|
||||
column.type if column
|
||||
else
|
||||
:string
|
||||
end
|
||||
|
||||
case input_type
|
||||
|
|
|
@ -2,9 +2,13 @@ require 'test_helper'
|
|||
|
||||
class ErrorTest < ActionView::TestCase
|
||||
|
||||
def with_error_for(object, attribute, type, &block)
|
||||
def with_error_for(object, attribute, type, options={}, &block)
|
||||
simple_form_for object do |f|
|
||||
error = SimpleForm::Components::Error.new(f, attribute, type, {})
|
||||
f.attribute = attribute
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
|
||||
error = SimpleForm::Components::Error.new(f, SimpleForm.terminator)
|
||||
concat(error.generate)
|
||||
yield error if block_given?
|
||||
end
|
||||
|
|
|
@ -4,7 +4,11 @@ class ErrorTest < ActionView::TestCase
|
|||
|
||||
def with_hint_for(object, attribute, type, options={})
|
||||
simple_form_for object do |f|
|
||||
hint = SimpleForm::Components::Hint.new(f, attribute, type, options)
|
||||
f.attribute = attribute
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
|
||||
hint = SimpleForm::Components::Hint.new(f, SimpleForm.terminator)
|
||||
concat(hint.generate)
|
||||
yield hint if block_given?
|
||||
end
|
||||
|
|
|
@ -8,7 +8,11 @@ class InputTest < ActionView::TestCase
|
|||
|
||||
def with_input_for(object, attribute, type, options={})
|
||||
simple_form_for object do |f|
|
||||
input = SimpleForm::Components::Input.new(f, attribute, type, options)
|
||||
f.attribute = attribute
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
|
||||
input = SimpleForm::Components::Input.new(f, SimpleForm.terminator)
|
||||
concat(input.generate)
|
||||
yield input if block_given?
|
||||
end
|
||||
|
|
|
@ -10,7 +10,11 @@ class LabelTest < ActionView::TestCase
|
|||
|
||||
def with_label_for(object, attribute, type, options={})
|
||||
simple_form_for object do |f|
|
||||
label = SimpleForm::Components::Label.new(f, attribute, type, options)
|
||||
f.attribute = attribute
|
||||
f.input_type = type
|
||||
f.options = options
|
||||
|
||||
label = SimpleForm::Components::Label.new(f, SimpleForm.terminator)
|
||||
concat(label.generate)
|
||||
yield label if block_given?
|
||||
end
|
||||
|
|
|
@ -15,6 +15,10 @@ require 'simple_form'
|
|||
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
||||
I18n.default_locale = :en
|
||||
|
||||
class SimpleForm::FormBuilder
|
||||
attr_accessor :attribute, :input_type, :options
|
||||
end
|
||||
|
||||
class ActionView::TestCase
|
||||
include MiscHelpers
|
||||
|
||||
|
|
Loading…
Reference in New Issue