Skip to content

Commit

Permalink
Added auto-guessing and updated some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Sanghi committed Oct 10, 2010
1 parent 73e71b6 commit e0a7874
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.textile
Expand Up @@ -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.
Expand Down
15 changes: 9 additions & 6 deletions lib/formtastic.rb
Expand Up @@ -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)
#
Expand All @@ -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 %>
Expand Down Expand Up @@ -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$/

This comment has been minimized.

Copy link
@sobrinho

sobrinho Oct 13, 2010

Member

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?

This comment has been minimized.

Copy link
@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?

This comment has been minimized.

Copy link
@sobrinho

sobrinho Oct 13, 2010

Member

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

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

This comment has been minimized.

Copy link
@asanghi

asanghi Oct 13, 2010

Contributor

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

This comment has been minimized.

Copy link
@sobrinho

sobrinho Oct 13, 2010

Member

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

This comment has been minimized.

Copy link
@asanghi

asanghi Oct 13, 2010

Contributor

Already done and sent the pull :)

This comment has been minimized.

Copy link
@mjonuschat

mjonuschat Oct 13, 2010

Contributor

/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.

This comment has been minimized.

Copy link
@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?

This comment has been minimized.

Copy link
@justinfrench

justinfrench Oct 13, 2010

Member

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.

This comment has been minimized.

Copy link
@sobrinho

sobrinho Oct 13, 2010

Member

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

This comment has been minimized.

Copy link
@dallas

dallas Oct 13, 2010

Contributor

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

This comment has been minimized.

Copy link
@justinfrench

justinfrench Oct 13, 2010

Member

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

This comment has been minimized.

Copy link
@justinfrench

justinfrench Oct 13, 2010

Member

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
Expand Down
2 changes: 1 addition & 1 deletion spec/inputs/email_input_spec.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/inputs/phone_input_spec.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/inputs/search_input_spec.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/inputs/url_input_spec.rb
Expand Up @@ -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

Expand Down
12 changes: 8 additions & 4 deletions spec/spec_helper.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit e0a7874

Please sign in to comment.