Permalink
Browse files

Added auto-guessing and updated some docs

  • Loading branch information...
1 parent 73e71b6 commit e0a787476af934fa285d12a6a9208278daafd6e0 Aditya Sanghi committed Oct 10, 2010
View
@@ -275,6 +275,10 @@ The Formtastic input types:
* @:numeric@ - a text field (just like string). Default for column types: @:integer@, @:float@, and @:decimal@.
* @:file@ - a file field. Default for file-attachment attributes matching: "paperclip":http://github.com/thoughtbot/paperclip or "attachment_fu":http://github.com/technoweenie/attachment_fu.
* @:country@ - a select menu of country names. Default for column types: :string with name @"country"@ - requires a *country_select* plugin to be installed.
+* @:email@ - a text field (just like string). Default for columns with name matching @"email"@. New in HTML5. Works on some mobile browsers already.
+* @:url@ - a text field (just like string). Default for columns with name matching @"url"@. New in HTML5. Works on some mobile browsers already.
+* @:phone@ - a text field (just like string). Default for columns with name matching @"phone"@ or @"fax"@. New in HTML5.
+* @:search@ - a text field (just like string). Default for columns with name matching @"search"@. New in HTML5. Works on Safari.
* @:hidden@ - a hidden field. Creates a hidden field (added for compatibility).
The comments in the code are pretty good for each of these (what it does, what the output is, what the options are, etc.) so go check it out.
View
@@ -65,11 +65,10 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
# * :boolean (a checkbox) - default for :boolean column types (you can also have booleans as :select and :radio)
# * :string (a text field) - default for :string column types
# * :numeric (a text field, like string) - default for :integer, :float and :decimal column types
- # * :country (a select menu of country names) - requires a country_select plugin to be installed
- # * :email (an email input) - New in HTML5 - needs to be explicitly provided with :as => :email
- # * :url (a url input) - New in HTML5 - needs to be explicitly provided with :as => :url
- # * :phone (a tel input) - New in HTML5 - needs to be explicitly provided with :as => :phone
- # * :search (a search input) - New in HTML5 - needs to be explicity provided with :as => :search
+ # * :email (an email input) - default for :string column types with 'email' as the method name.
+ # * :url (a url input) - default for :string column types with 'url' as the method name.
+ # * :phone (a tel input) - default for :string column types with 'phone' or 'fax' in the method name.
+ # * :search (a search input) - default for :string column types with 'search' as the method name.
# * :country (a select menu of country names) - requires a country_select plugin to be installed
# * :hidden (a hidden field) - creates a hidden field (added for compatibility)
#
@@ -82,7 +81,7 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
# <%= form.input :manager_id, :as => :radio %>
# <%= form.input :hired_at, :as => :date, :label => "Date Hired" %>
# <%= form.input :phone, :required => false, :hint => "Eg: +1 555 1234" %>
- # <%= form.input :email, :as => :email %>
+ # <%= form.input :email %>
# <%= form.input :website, :as => :url, :hint => "You may wish to omit the http://" %>
# <% end %>
# <% end %>
@@ -1465,6 +1464,10 @@ def default_input_type(method, options = {}) #:nodoc:
return :password if method.to_s =~ /password/
return :country if method.to_s =~ /country$/
return :time_zone if method.to_s =~ /time_zone/
+ return :email if method.to_s =~ /^email$/
@sobrinho

sobrinho Oct 13, 2010

Collaborator

I think this will be better if is /email/ or /email$/ to match fields like customer_email, secundary_email or like that. Same for urls...

What you think?

@asanghi

asanghi Oct 13, 2010

Contributor

Sure! i was on the fence for most of the defaults anyway. Want me to send a pull or you'll update and commit?

@sobrinho

sobrinho Oct 13, 2010

Collaborator

Send a pull request please, I need to know if justin and morton agree too ;)

+ return :url if method.to_s =~ /^url$/
@asanghi

asanghi Oct 13, 2010

Contributor

Probably here too, could be url, website? customer_website, public_website, public_url etc.

@sobrinho

sobrinho Oct 13, 2010

Collaborator

I guess /(url|website)/ would be great

@asanghi

asanghi Oct 13, 2010

Contributor

Already done and sent the pull :)

@mjonuschat

mjonuschat Oct 13, 2010

Collaborator

/url/ is way too generic for my taste. That will match any one of a few hundred words with "url" in them, and that's only considering english language.

I would prefer something along the lines of /(\A|_)(url|website)$/ which detects a lot of common method names while having a far lower rate of false positives.

@asanghi

asanghi Oct 13, 2010

Contributor

..or we could just leave it as ^url$ and not second guess the user too much? perhaps /^(url|website)$/ atmost?

@justinfrench

justinfrench Oct 13, 2010

Owner

You're all right :) Perhaps...

(^url$|^website$|_url$)

To get this into master, I'd prefer more restrictive than less restrictive. Over time we can relax the matching to cover more cases, but it's probably something we best chip away at slowly.

I've also asked for some coverage on the pull request issue.

@sobrinho

sobrinho Oct 13, 2010

Collaborator

Sure... So, I guess the last suggestion by asanghi is good.

@dallas

dallas Oct 13, 2010

Contributor

what about attributes like url_path or website_location? These things happen!

@justinfrench

justinfrench Oct 13, 2010

Owner

It's better to err on the cautious side. Patches accepted.

@justinfrench

justinfrench Oct 13, 2010

Owner

To clarify, these are guesses, which can always be overridden with :as => :url. It's better to not always guess correctly than it is to sometimes guess incorrectly.

+ return :phone if method.to_s =~ /(phone|fax)/
+ return :search if method.to_s =~ /^search$/
when :integer
return :select if method.to_s =~ /_id$/
return :numeric
@@ -13,7 +13,7 @@
describe "when object is provided" do
before do
@form = semantic_form_for(@new_post) do |builder|
- concat(builder.input(:email, :as => :email))
+ concat(builder.input(:email))
end
end
@@ -13,7 +13,7 @@
describe "when object is provided" do
before do
@form = semantic_form_for(@new_post) do |builder|
- concat(builder.input(:phone, :as => :phone))
+ concat(builder.input(:phone))
end
end
@@ -13,7 +13,7 @@
describe "when object is provided" do
before do
@form = semantic_form_for(@new_post) do |builder|
- concat(builder.input(:search, :as => :search))
+ concat(builder.input(:search))
end
end
@@ -13,7 +13,7 @@
describe "when object is provided" do
before do
@form = semantic_form_for(@new_post) do |builder|
- concat(builder.input(:url, :as => :url))
+ concat(builder.input(:url))
end
end
View
@@ -231,16 +231,16 @@ def new_author_path; "/authors/new"; end
::Post.stub!(:to_ary)
@new_post.stub!(:title)
- @new_post.stub!(:email)
- @new_post.stub!(:url)
- @new_post.stub!(:phone)
- @new_post.stub!(:search)
@new_post.stub!(:to_ary)
@new_post.stub!(:body)
@new_post.stub!(:published)
@new_post.stub!(:publish_at)
@new_post.stub!(:created_at)
@new_post.stub!(:secret)
+ @new_post.stub!(:url)
+ @new_post.stub!(:email)
+ @new_post.stub!(:search)
+ @new_post.stub!(:phone)
@new_post.stub!(:time_zone)
@new_post.stub!(:category_name)
@new_post.stub!(:allow_comments)
@@ -258,6 +258,10 @@ def new_author_path; "/authors/new"; end
@new_post.stub!(:column_for_attribute).with(:country).and_return(mock('column', :type => :string, :limit => 255))
@new_post.stub!(:column_for_attribute).with(:country_subdivision).and_return(mock('column', :type => :string, :limit => 255))
@new_post.stub!(:column_for_attribute).with(:country_code).and_return(mock('column', :type => :string, :limit => 255))
+ @new_post.stub!(:column_for_attribute).with(:email).and_return(mock('column', :type => :string, :limit => 255))
+ @new_post.stub!(:column_for_attribute).with(:url).and_return(mock('column', :type => :string, :limit => 255))
+ @new_post.stub!(:column_for_attribute).with(:phone).and_return(mock('column', :type => :string, :limit => 255))
+ @new_post.stub!(:column_for_attribute).with(:search).and_return(mock('column', :type => :string, :limit => 255))
@new_post.stub!(:author).and_return(@bob)
@new_post.stub!(:author_id).and_return(@bob.id)

0 comments on commit e0a7874

Please sign in to comment.