Add map_type API.
This commit is contained in:
parent
f6ce729f5e
commit
43c5e7aeed
|
@ -2,6 +2,7 @@ require 'simple_form/label'
|
||||||
require 'simple_form/input'
|
require 'simple_form/input'
|
||||||
require 'simple_form/hint'
|
require 'simple_form/hint'
|
||||||
require 'simple_form/error'
|
require 'simple_form/error'
|
||||||
|
require 'simple_form/map_type'
|
||||||
|
|
||||||
module SimpleForm
|
module SimpleForm
|
||||||
class FormBuilder < ActionView::Helpers::FormBuilder
|
class FormBuilder < ActionView::Helpers::FormBuilder
|
||||||
|
@ -10,6 +11,20 @@ module SimpleForm
|
||||||
include SimpleForm::Hint
|
include SimpleForm::Hint
|
||||||
include SimpleForm::Error
|
include SimpleForm::Error
|
||||||
|
|
||||||
|
extend SimpleForm::MapType
|
||||||
|
|
||||||
|
map_type :boolean, :to => :check_box
|
||||||
|
map_type :text, :to => :text_area
|
||||||
|
map_type :datetime, :to => :datetime_select, :options => true
|
||||||
|
map_type :date, :to => :date_select, :options => true
|
||||||
|
map_type :time, :to => :time_select, :options => true
|
||||||
|
map_type :password, :to => :password_field
|
||||||
|
map_type :hidden, :to => :hidden_field
|
||||||
|
map_type :select, :to => :collection_select, :options => true, :collection => true
|
||||||
|
map_type :radio, :to => :collection_radio, :collection => true
|
||||||
|
map_type :numeric, :to => :text_field
|
||||||
|
map_type :string, :to => :text_field
|
||||||
|
|
||||||
def input(attribute, options={})
|
def input(attribute, options={})
|
||||||
@attribute, @options = attribute, options
|
@attribute, @options = attribute, options
|
||||||
@options.assert_valid_keys(:as, :label, :required, :hint, :options, :html,
|
@options.assert_valid_keys(:as, :label, :required, :hint, :options, :html,
|
||||||
|
|
|
@ -1,20 +1,5 @@
|
||||||
module SimpleForm
|
module SimpleForm
|
||||||
module Input
|
module Input
|
||||||
Mapping = Struct.new(:method, :collection, :options)
|
|
||||||
|
|
||||||
MAPPINGS = {
|
|
||||||
:boolean => Mapping.new(:check_box, false, false),
|
|
||||||
:text => Mapping.new(:text_area, false, false),
|
|
||||||
:datetime => Mapping.new(:datetime_select, false, true),
|
|
||||||
:date => Mapping.new(:date_select, false, true),
|
|
||||||
:time => Mapping.new(:time_select, false, true),
|
|
||||||
:password => Mapping.new(:password_field, false, false),
|
|
||||||
:hidden => Mapping.new(:hidden_field, false, false),
|
|
||||||
:select => Mapping.new(:collection_select, true, true),
|
|
||||||
:radio => Mapping.new(:collection_radio, true, false),
|
|
||||||
:numeric => Mapping.new(:text_field, false, false),
|
|
||||||
:string => Mapping.new(:text_field, false, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
@ -23,7 +8,7 @@ module SimpleForm
|
||||||
html_options[:class] = default_css_classes(html_options[:class])
|
html_options[:class] = default_css_classes(html_options[:class])
|
||||||
@options[:options] ||= {}
|
@options[:options] ||= {}
|
||||||
|
|
||||||
mapping = MAPPINGS[@input_type]
|
mapping = self.class.mappings[@input_type]
|
||||||
raise "Invalid input type #{@input_type.inspect}" unless mapping
|
raise "Invalid input type #{@input_type.inspect}" unless mapping
|
||||||
|
|
||||||
args = [ @attribute ]
|
args = [ @attribute ]
|
||||||
|
@ -62,11 +47,11 @@ module SimpleForm
|
||||||
|
|
||||||
def collection_radio(attribute, collection, value_method, text_method, html_options={})
|
def collection_radio(attribute, collection, value_method, text_method, html_options={})
|
||||||
collection.inject('') do |result, item|
|
collection.inject('') do |result, item|
|
||||||
value = item.send value_method
|
value = item.send value_method
|
||||||
text = item.send text_method
|
text = item.send text_method
|
||||||
|
|
||||||
result << radio_button(attribute, value, html_options) <<
|
result << radio_button(attribute, value, html_options) <<
|
||||||
label("#{attribute}_#{value}", text, :class => "radio")
|
label("#{attribute}_#{value}", text, :class => "radio")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
module SimpleForm
|
||||||
|
Mapping = Struct.new(:method, :collection, :options)
|
||||||
|
|
||||||
|
module MapType
|
||||||
|
def mappings
|
||||||
|
@mappings ||= {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def map_type(type, options)
|
||||||
|
raise ArgumentError, "You need to give :to as option to map_type" unless options[:to]
|
||||||
|
mappings[type] = Mapping.new(options[:to], options[:collection] || false, options[:options] || false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue