1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

Added custom matchers for defining default input types on regexp

Signed-off-by: Carlos Antonio da Silva <carlosantoniodasilva@gmail.com>
This commit is contained in:
Subhash Chandra 2010-11-25 11:55:31 +05:30 committed by Carlos Antonio da Silva
parent f4d6cc4b52
commit c5850a1e68
3 changed files with 24 additions and 1 deletions

View file

@ -13,6 +13,8 @@ module SimpleForm
map_type :country, :time_zone, :to => SimpleForm::Inputs::PriorityInput
map_type :boolean, :to => SimpleForm::Inputs::BooleanInput
@@custom_matchers = Hash.new
# Basic input helper, combines all components in the stack to generate
# input html based on options the user define and some guesses through
# database column information. By default a call to input will generate
@ -242,6 +244,16 @@ module SimpleForm
SimpleForm::ErrorNotification.new(self, options).render
end
# Add custom matchers for selecting default input type
#
# == Examples
#
# SimpleForm::FormBuilder.add_matcher( /regexp/ => :input_type )
#
def self.add_matcher(*args)
@@custom_matchers.reverse_merge!(*args)
end
private
# Attempt to guess the better input type given the defined options. By
@ -264,6 +276,10 @@ module SimpleForm
when /email/ then :email
when /phone/ then :tel
when /url/ then :url
else
@@custom_matchers.reduce(nil) { |memo, a|
a[0].match(attribute_name.to_s) ? a[1] : memo
}
end
match || input_type || file_method?(attribute_name) || :string

View file

@ -167,6 +167,13 @@ class FormBuilderTest < ActionView::TestCase
assert_select 'form input#user_born_at.string'
end
test 'builder should allow adding custom matchers for default input types' do
SimpleForm::FormBuilder.add_matcher( /count/ => :integer )
with_form_for @user, :post_count
assert_no_select 'form input#user_post_count.string'
assert_select 'form input#user_post_count.numeric.integer'
end
# COMMON OPTIONS
test 'builder should allow passing options to input' do
with_form_for @user, :name, :input_html => { :class => 'my_input', :id => 'my_input' }

View file

@ -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
:avatar, :email, :status, :residence_country, :phone_number, :post_count
def initialize(options={})
options.each do |key, value|