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/hint'
|
||||
require 'simple_form/error'
|
||||
require 'simple_form/map_type'
|
||||
|
||||
module SimpleForm
|
||||
class FormBuilder < ActionView::Helpers::FormBuilder
|
||||
|
@ -10,6 +11,20 @@ module SimpleForm
|
|||
include SimpleForm::Hint
|
||||
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={})
|
||||
@attribute, @options = attribute, options
|
||||
@options.assert_valid_keys(:as, :label, :required, :hint, :options, :html,
|
||||
|
|
|
@ -1,20 +1,5 @@
|
|||
module SimpleForm
|
||||
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
|
||||
|
||||
|
@ -23,7 +8,7 @@ module SimpleForm
|
|||
html_options[:class] = default_css_classes(html_options[:class])
|
||||
@options[:options] ||= {}
|
||||
|
||||
mapping = MAPPINGS[@input_type]
|
||||
mapping = self.class.mappings[@input_type]
|
||||
raise "Invalid input type #{@input_type.inspect}" unless mapping
|
||||
|
||||
args = [ @attribute ]
|
||||
|
@ -62,11 +47,11 @@ module SimpleForm
|
|||
|
||||
def collection_radio(attribute, collection, value_method, text_method, html_options={})
|
||||
collection.inject('') do |result, item|
|
||||
value = item.send value_method
|
||||
text = item.send text_method
|
||||
value = item.send value_method
|
||||
text = item.send text_method
|
||||
|
||||
result << radio_button(attribute, value, html_options) <<
|
||||
label("#{attribute}_#{value}", text, :class => "radio")
|
||||
result << radio_button(attribute, value, html_options) <<
|
||||
label("#{attribute}_#{value}", text, :class => "radio")
|
||||
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