Make custom mappings work with all attributes types
This commit is contained in:
parent
ced9e71af2
commit
b2420adce4
|
@ -251,6 +251,10 @@ module SimpleForm
|
|||
return options[:as].to_sym if options[:as]
|
||||
return :select if options[:collection]
|
||||
|
||||
custom_type = find_custom_type(attribute_name) if SimpleForm.input_mappings
|
||||
|
||||
return custom_type if custom_type
|
||||
|
||||
input_type = column.try(:type)
|
||||
|
||||
case input_type
|
||||
|
@ -264,10 +268,6 @@ module SimpleForm
|
|||
when /email/ then :email
|
||||
when /phone/ then :tel
|
||||
when /url/ then :url
|
||||
else
|
||||
SimpleForm.input_mappings.find { |match, type|
|
||||
attribute_name.to_s =~ match
|
||||
}.try(:last) if SimpleForm.input_mappings
|
||||
end
|
||||
|
||||
match || input_type || file_method?(attribute_name) || :string
|
||||
|
@ -276,6 +276,12 @@ module SimpleForm
|
|||
end
|
||||
end
|
||||
|
||||
def find_custom_type(attribute_name)
|
||||
SimpleForm.input_mappings.find { |match, type|
|
||||
attribute_name.to_s =~ match
|
||||
}.try(:last)
|
||||
end
|
||||
|
||||
# Checks if attribute is a file_method.
|
||||
def file_method?(attribute_name) #:nodoc:
|
||||
file = @object.send(attribute_name) if @object.respond_to?(attribute_name)
|
||||
|
|
|
@ -76,6 +76,14 @@ class FormBuilderTest < ActionView::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
test 'builder should allow adding custom input mappings for integer input types' do
|
||||
swap SimpleForm, :input_mappings => { /lock_version/ => :hidden } do
|
||||
with_form_for @user, :lock_version
|
||||
assert_no_select 'form input#user_lock_version.integer'
|
||||
assert_select 'form input#user_lock_version.hidden'
|
||||
end
|
||||
end
|
||||
|
||||
test 'builder uses the first matching custom input map when more than one match' do
|
||||
swap SimpleForm, :input_mappings => { /count$/ => :integer, /^post_/ => :password } do
|
||||
with_form_for @user, :post_count
|
||||
|
@ -84,6 +92,14 @@ class FormBuilderTest < ActionView::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
test 'builder uses the custom map only for matched attributes' do
|
||||
swap SimpleForm, :input_mappings => { /lock_version/ => :hidden } do
|
||||
with_form_for @user, :post_count
|
||||
assert_no_select 'form input#user_post_count.hidden'
|
||||
assert_select 'form input#user_post_count.string'
|
||||
end
|
||||
end
|
||||
|
||||
# INPUT TYPES
|
||||
test 'builder should generate text fields for string columns' do
|
||||
with_form_for @user, :name
|
||||
|
|
|
@ -40,7 +40,7 @@ class User
|
|||
|
||||
attr_accessor :id, :name, :company, :company_id, :time_zone, :active, :description, :created_at, :updated_at,
|
||||
:credit_limit, :age, :password, :delivery_time, :born_at, :special_company_id, :country, :url, :tag_ids,
|
||||
:avatar, :email, :status, :residence_country, :phone_number, :post_count
|
||||
:avatar, :email, :status, :residence_country, :phone_number, :post_count, :lock_version
|
||||
|
||||
def initialize(options={})
|
||||
options.each do |key, value|
|
||||
|
@ -70,6 +70,7 @@ class User
|
|||
when :delivery_time then :time
|
||||
when :created_at then :datetime
|
||||
when :updated_at then :timestamp
|
||||
when :lock_version then :integer
|
||||
end
|
||||
Column.new(attribute, column_type, limit)
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue