From c5850a1e68cf023a430550677bbef019be051607 Mon Sep 17 00:00:00 2001 From: Subhash Chandra Date: Thu, 25 Nov 2010 11:55:31 +0530 Subject: [PATCH] Added custom matchers for defining default input types on regexp Signed-off-by: Carlos Antonio da Silva --- lib/simple_form/form_builder.rb | 16 ++++++++++++++++ test/form_builder_test.rb | 7 +++++++ test/support/models.rb | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 51d05dce..5ee388b8 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -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 diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index cc1cd8d8..9d1bdeeb 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -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' } diff --git a/test/support/models.rb b/test/support/models.rb index b11c324d..bfd5c79d 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -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|