Permalink
Browse files

Merge branch 'master' of git://github.com/negonicrac/formtastic into …

…negonicrac/master
  • Loading branch information...
2 parents 15ff964 + 3ca56c2 commit a9a1f3bbc7947ccd31896a142e6b00047493228a @justinfrench committed Jan 5, 2009
Showing with 92 additions and 35 deletions.
  1. +16 −10 lib/justin_french/formtastic.rb
  2. +76 −25 spec/formtastic_spec.rb
@@ -108,7 +108,7 @@ def input(method, options = {})
raise NoMethodError unless @object.respond_to?(method)
options[:required] = method_required?(method, options[:required])
- options[:label] ||= method.to_s.humanize
+ options[:label] ||= method.to_s.titleize
options[:as] ||= default_input_type(@object, method)
input_method = "#{options[:as]}_input"
@@ -254,13 +254,12 @@ def method_required?(attribute, required_option) #:nodoc:
# f.input :author_id, :as => :select, :label_method => :to_s
# f.input :author_id, :as => :select, :label_method => :label
def select_input(method, options)
- options[:label_method] ||= :to_label
options[:collection] ||= find_parent_objects_for_column(method)
- options[:include_blank] ||= false
- options[:prompt] ||= nil
- choices = options[:collection].map {|o| [o.send(options[:label_method]), o.id]}
- input_label(method, options) + template.select(@object_name, method, choices, {:include_blank => options[:include_blank], :prompt => options[:prompt]})
+ choices = options[:collection].map { |o|
+ collection_option(o, options[:label_method] || :to_label)
+ }
+ input_label(method, options) + template.select(@object_name, method, choices, options)
end
# Outputs a fieldset containing a legend for the label text, and an ordered list (ol) of list
@@ -437,7 +436,7 @@ def date_or_datetime_input(method, options)
else
list_items_capture << template.content_tag(:li,
template.content_tag(:label, input.to_s.humanize, :for => "#{@object_name}_#{method}_#{position[input]}i") +
- template.send("select_#{input}".intern, @object.send(method), :prefix => @object_name, :field_name => "#{method}(#{position[input]}i)")
+ template.send("select_#{input}".intern, @object.send(method), :prefix => @object_name, :field_name => "#{method}(#{position[input]}i)", :include_blank => options[:include_blank])
)
end
end
@@ -482,7 +481,7 @@ def boolean_select_input(method, options)
options[:false] ||= "No"
choices = [ [options[:true],true], [options[:false],false] ]
- input_label(method, options) + template.select(@object_name, method, choices)
+ input_label(method, options) + template.select(@object_name, method, choices, options)
end
# Outputs a fieldset containing two radio buttons (with labels) for "true" and "false". The
@@ -602,8 +601,15 @@ def default_string_options(method) #:nodoc:
{ :maxlength => column.limit, :size => [column.limit, DEFAULT_TEXT_FIELD_SIZE].min }
end
end
-
+
+ private
+
+ def collection_option(item, method = :to_label)
+ [
+ item.respond_to?(method) ? item.send(method) : item.to_s,
+ item.respond_to?(method) ? item.id : item.to_s
+ ]
+ end
end
-
end
end
View
@@ -399,8 +399,8 @@ class Author; end
semantic_form_for(@new_post) do |builder|
concat(builder.input(:meta_description))
end
- output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
- output_buffer.should have_tag("form li label", /Meta description/)
+ output_buffer.should have_tag("form li label", /#{'meta_description'.titleize}/)
+ output_buffer.should have_tag("form li label", /Meta Description/)
end
end
@@ -718,22 +718,23 @@ class Author; end
end
end
-
- end
- describe ':as => :select, :include_blank => true, :prompt => "choose something"' do
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:author_id, :as => :select, :include_blank => true, :prompt => "choose author"))
+ describe 'when :include_blank => true, :prompt => "choose something" is set' do
+ before do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author_id, :as => :select, :include_blank => true, :prompt => "choose author"))
+ end
+ end
+
+ it 'should have a blank select option' do
+ output_buffer.should have_tag("form li select option[@value='']", / /)
+ end
+
+ it 'should have a select with prompt' do
+ output_buffer.should have_tag("form li select option[@value='']", /choose author/)
end
end
- it 'should have a blank select option' do
- output_buffer.should have_tag("form li select option[@value='']", / /)
- end
- it 'should have a select with prompt' do
- output_buffer.should have_tag("form li select option[@value='']", /choose author/)
- end
end
end
@@ -759,7 +760,7 @@ class Author; end
it 'should generate a label for the input' do
output_buffer.should have_tag('form li label')
output_buffer.should have_tag('form li label[@for="post_password_hash"')
- output_buffer.should have_tag('form li label', /Password hash/)
+ output_buffer.should have_tag('form li label', /Password Hash/)
end
it 'should generate a password input' do
@@ -840,7 +841,7 @@ class Author; end
end
it 'should have a legend containing the label text inside the fieldset' do
- output_buffer.should have_tag('form li.date fieldset legend', /Publish at/)
+ output_buffer.should have_tag('form li.date fieldset legend', /Publish At/)
end
it 'should have an ordered list of three items inside the fieldset' do
@@ -859,6 +860,17 @@ class Author; end
output_buffer.should have_tag('form li.date fieldset ol li select', :count => 3)
end
+ describe 'when :include_blank => true is set' do
+ before do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :date, :include_blank => true))
+ end
+ end
+
+ it 'should have a blank select option' do
+ output_buffer.should have_tag("option[@value='']", "")
+ end
+ end
end
@@ -882,7 +894,7 @@ class Author; end
end
it 'should have a legend containing the label text inside the fieldset' do
- output_buffer.should have_tag('form li.datetime fieldset legend', /Publish at/)
+ output_buffer.should have_tag('form li.datetime fieldset legend', /Publish At/)
end
it 'should have an ordered list of five items inside the fieldset' do
@@ -902,6 +914,18 @@ class Author; end
it 'should have five selects for year, month, day, hour and minute' do
output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
end
+
+ describe 'when :include_blank => true is set' do
+ before do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :datetime, :include_blank => true))
+ end
+ end
+
+ it 'should have a blank select option' do
+ output_buffer.should have_tag("option[@value='']", "")
+ end
+ end
end
@@ -925,7 +949,7 @@ class Author; end
end
it 'should have a legend containing the label text inside the fieldset' do
- output_buffer.should have_tag('form li.time fieldset legend', /Publish at/)
+ output_buffer.should have_tag('form li.time fieldset legend', /Publish At/)
end
it 'should have an ordered list of two items inside the fieldset' do
@@ -943,6 +967,18 @@ class Author; end
output_buffer.should have_tag('form li.time fieldset ol li select', :count => 2)
end
+ describe 'when :include_blank => true is set' do
+ before do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :time, :include_blank => true))
+ end
+ end
+
+ it 'should have a blank select option' do
+ output_buffer.should have_tag("option[@value='']", "")
+ end
+ end
+
end
describe ':as => :boolean' do
@@ -967,7 +1003,7 @@ class Author; end
it 'should generate a label containing the input' do
output_buffer.should have_tag('form li label')
output_buffer.should have_tag('form li label[@for="post_allow_comments"')
- output_buffer.should have_tag('form li label', /Allow comments/)
+ output_buffer.should have_tag('form li label', /Allow Comments/)
output_buffer.should have_tag('form li label input[@type="checkbox"]')
end
@@ -1000,7 +1036,7 @@ class Author; end
end
it 'should generate a fieldset containing a legend' do
- output_buffer.should have_tag('form li fieldset legend', /Allow comments/)
+ output_buffer.should have_tag('form li fieldset legend', /Allow Comments/)
end
it 'should generate a fieldset containing an ordered list of two items with true and false classes' do
@@ -1105,7 +1141,7 @@ class Author; end
it 'should generate a label containing the input' do
output_buffer.should have_tag('form li label')
output_buffer.should have_tag('form li label[@for="post_allow_comments"')
- output_buffer.should have_tag('form li label', /Allow comments/)
+ output_buffer.should have_tag('form li label', /Allow Comments/)
end
it 'should generate a select box with two options' do
@@ -1147,10 +1183,25 @@ class Author; end
output_buffer.should have_tag('form li select#post_allow_comments option[@value="false"]', /No/)
end
- end
+ end
+
+ describe 'when :include_blank => true, :prompt => "choose something" is set' do
+ before do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:allow_comments, :as => :boolean_select, :include_blank => true, :prompt => "choose something"))
+ end
+ end
+ it 'should have a blank select option' do
+ output_buffer.should have_tag("form li select option[@value='']", / /)
+ end
+ it 'should have a select with prompt' do
+ output_buffer.should have_tag("form li select option[@value='']", /choose something/)
+ end
+ end
+
end
-
+
describe ':as => :numeric' do
setup do
@@ -1174,7 +1225,7 @@ class Author; end
it 'should generate a label for the input' do
output_buffer.should have_tag('form li label')
output_buffer.should have_tag('form li label[@for="post_comments_count"')
- output_buffer.should have_tag('form li label', /Comments count/)
+ output_buffer.should have_tag('form li label', /Comments Count/)
end
it 'should generate a text input' do
@@ -1223,7 +1274,7 @@ class Author; end
it 'should generate a label for the input' do
output_buffer.should have_tag('form li label')
output_buffer.should have_tag('form li label[@for="post_some_file"')
- output_buffer.should have_tag('form li label', /Some file/)
+ output_buffer.should have_tag('form li label', /Some File/)
end
it 'should generate a file input' do

0 comments on commit a9a1f3b

Please sign in to comment.