Moving input specific attributes to input instead
of FormBuilder, dirty unreadeable assignments
This commit is contained in:
parent
693153d169
commit
973e23c289
|
@ -1,7 +1,6 @@
|
||||||
module SimpleForm
|
module SimpleForm
|
||||||
class FormBuilder < ActionView::Helpers::FormBuilder
|
class FormBuilder < ActionView::Helpers::FormBuilder
|
||||||
attr_reader :template, :object_name, :object, :attribute_name, :column,
|
attr_reader :template, :object_name, :object, :reflection, :options
|
||||||
:reflection, :input_type, :options
|
|
||||||
|
|
||||||
extend MapType
|
extend MapType
|
||||||
include SimpleForm::Inputs
|
include SimpleForm::Inputs
|
||||||
|
@ -80,14 +79,14 @@ module SimpleForm
|
||||||
# given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly.
|
# given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly.
|
||||||
#
|
#
|
||||||
def input(attribute_name, options={}, &block)
|
def input(attribute_name, options={}, &block)
|
||||||
define_simple_form_attributes(attribute_name, options)
|
column = find_attribute_column(attribute_name)
|
||||||
|
input_type = default_input_type(attribute_name, column, options)
|
||||||
if block_given?
|
if block_given?
|
||||||
SimpleForm::Inputs::BlockInput.new(self, block).render
|
SimpleForm::Inputs::BlockInput.new(self, block).render
|
||||||
else
|
else
|
||||||
klass = self.class.mappings[input_type] ||
|
klass = self.class.mappings[input_type] ||
|
||||||
self.class.const_get(:"#{input_type.to_s.camelize}Input")
|
self.class.const_get(:"#{input_type.to_s.camelize}Input")
|
||||||
klass.new(self).render
|
klass.new(self, attribute_name, column, input_type, options).render
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias :attribute :input
|
alias :attribute :input
|
||||||
|
@ -176,8 +175,10 @@ module SimpleForm
|
||||||
# f.error :name, :id => "cool_error"
|
# f.error :name, :id => "cool_error"
|
||||||
#
|
#
|
||||||
def error(attribute_name, options={})
|
def error(attribute_name, options={})
|
||||||
define_simple_form_attributes(attribute_name, :error_html => options)
|
options[:error_html] = options
|
||||||
SimpleForm::Inputs::Base.new(self).error
|
column = find_attribute_column(attribute_name)
|
||||||
|
input_type = default_input_type(attribute_name, column, options)
|
||||||
|
SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).error
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates a hint tag for the given attribute. Accepts a symbol indicating
|
# Creates a hint tag for the given attribute. Accepts a symbol indicating
|
||||||
|
@ -191,9 +192,15 @@ module SimpleForm
|
||||||
# f.hint "Don't forget to accept this"
|
# f.hint "Don't forget to accept this"
|
||||||
#
|
#
|
||||||
def hint(attribute_name, options={})
|
def hint(attribute_name, options={})
|
||||||
attribute_name, options[:hint] = nil, attribute_name if attribute_name.is_a?(String)
|
options[:hint_html] = options
|
||||||
define_simple_form_attributes(attribute_name, :hint => options.delete(:hint), :hint_html => options)
|
if attribute_name.is_a?(String)
|
||||||
SimpleForm::Inputs::Base.new(self).hint
|
options[:hint] = attribute_name
|
||||||
|
attribute_name, column, input_type = nil, nil, nil
|
||||||
|
else
|
||||||
|
column = find_attribute_column(attribute_name)
|
||||||
|
input_type = default_input_type(attribute_name, column, options)
|
||||||
|
end
|
||||||
|
SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).hint
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates a default label tag for the given attribute. You can give a label
|
# Creates a default label tag for the given attribute. You can give a label
|
||||||
|
@ -212,9 +219,12 @@ module SimpleForm
|
||||||
def label(attribute_name, *args)
|
def label(attribute_name, *args)
|
||||||
return super if args.first.is_a?(String)
|
return super if args.first.is_a?(String)
|
||||||
options = args.extract_options!
|
options = args.extract_options!
|
||||||
define_simple_form_attributes(attribute_name, :label => options.delete(:label),
|
options[:label] = options.delete(:label)
|
||||||
:label_html => options, :required => options.delete(:required))
|
options[:label_html] = options
|
||||||
SimpleForm::Inputs::Base.new(self).label
|
options[:required] = options.delete(:required)
|
||||||
|
column = find_attribute_column(attribute_name)
|
||||||
|
input_type = default_input_type(attribute_name, column, options)
|
||||||
|
SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).label
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates an error notification message that only appears when the form object
|
# Creates an error notification message that only appears when the form object
|
||||||
|
@ -234,30 +244,20 @@ module SimpleForm
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Setup default simple form attributes.
|
|
||||||
def define_simple_form_attributes(attribute_name, options) #:nodoc:
|
|
||||||
@options = options
|
|
||||||
|
|
||||||
if @attribute_name = attribute_name
|
|
||||||
@column = find_attribute_column
|
|
||||||
@input_type = default_input_type
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Attempt to guess the better input type given the defined options. By
|
# Attempt to guess the better input type given the defined options. By
|
||||||
# default alwayls fallback to the user :as option, or to a :select when a
|
# default alwayls fallback to the user :as option, or to a :select when a
|
||||||
# collection is given.
|
# collection is given.
|
||||||
def default_input_type #:nodoc:
|
def default_input_type(attribute_name, column, options) #:nodoc:
|
||||||
return @options[:as].to_sym if @options[:as]
|
return options[:as].to_sym if options[:as]
|
||||||
return :select if @options[:collection]
|
return :select if options[:collection]
|
||||||
|
|
||||||
input_type = @column.try(:type)
|
input_type = column.try(:type)
|
||||||
|
|
||||||
case input_type
|
case input_type
|
||||||
when :timestamp
|
when :timestamp
|
||||||
:datetime
|
:datetime
|
||||||
when :string, nil
|
when :string, nil
|
||||||
match = case @attribute_name.to_s
|
match = case attribute_name.to_s
|
||||||
when /password/ then :password
|
when /password/ then :password
|
||||||
when /time_zone/ then :time_zone
|
when /time_zone/ then :time_zone
|
||||||
when /country/ then :country
|
when /country/ then :country
|
||||||
|
@ -266,21 +266,21 @@ module SimpleForm
|
||||||
when /url/ then :url
|
when /url/ then :url
|
||||||
end
|
end
|
||||||
|
|
||||||
match || input_type || file_method? || :string
|
match || input_type || file_method?(attribute_name) || :string
|
||||||
else
|
else
|
||||||
input_type
|
input_type
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks if attribute is a file_method.
|
# Checks if attribute is a file_method.
|
||||||
def file_method? #:nodoc:
|
def file_method?(attribute_name) #:nodoc:
|
||||||
file = @object.send(@attribute_name) if @object.respond_to?(@attribute_name)
|
file = @object.send(attribute_name) if @object.respond_to?(attribute_name)
|
||||||
:file if file && SimpleForm.file_methods.any? { |m| file.respond_to?(m) }
|
:file if file && SimpleForm.file_methods.any? { |m| file.respond_to?(m) }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Finds the database column for the given attribute
|
# Finds the database column for the given attribute
|
||||||
def find_attribute_column #:nodoc:
|
def find_attribute_column(attribute_name) #:nodoc:
|
||||||
@object.column_for_attribute(@attribute_name) if @object.respond_to?(:column_for_attribute)
|
@object.column_for_attribute(attribute_name) if @object.respond_to?(:column_for_attribute)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Find reflection related to association
|
# Find reflection related to association
|
||||||
|
|
|
@ -14,11 +14,16 @@ module SimpleForm
|
||||||
include SimpleForm::Components::LabelInput
|
include SimpleForm::Components::LabelInput
|
||||||
include SimpleForm::Components::Wrapper
|
include SimpleForm::Components::Wrapper
|
||||||
|
|
||||||
delegate :template, :object, :object_name, :attribute_name, :column,
|
attr_reader :attribute_name, :column, :input_type, :options
|
||||||
:reflection, :input_type, :options, :to => :@builder
|
|
||||||
|
|
||||||
def initialize(builder)
|
delegate :template, :object, :object_name, :reflection, :to => :@builder
|
||||||
|
|
||||||
|
def initialize(builder, attribute_name, column, input_type, options = {})
|
||||||
@builder = builder
|
@builder = builder
|
||||||
|
@attribute_name = attribute_name
|
||||||
|
@column = column
|
||||||
|
@input_type = input_type
|
||||||
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
def input
|
def input
|
||||||
|
|
Loading…
Reference in New Issue