Skip to content
Browse files

Merge branch 'master' into rails3

Conflicts:
	spec/inputs/select_input_spec.rb
  • Loading branch information...
2 parents 9c9dce8 + 3ae6b20 commit 28140117780a33d9a11669d9708ce0432c77dd19 @yabawock yabawock committed May 4, 2010
Showing with 93 additions and 7 deletions.
  1. +1 −1 README.textile
  2. +8 −2 lib/formtastic.rb
  3. +37 −1 spec/inputs/country_input_spec.rb
  4. +33 −2 spec/inputs/select_input_spec.rb
  5. +0 −1 spec/inputs_spec.rb
  6. +14 −0 spec/spec_helper.rb
View
2 README.textile
@@ -206,7 +206,7 @@ When working in has many association, you can even supply @"%i"@ in your fieldse
</pre>
-Customize HTML attributes for any input using the @:input_html@ option. Typically his is used to disable the input, change the size of a text field, change the rows in a textarea, or even to add a special class to an input to attach special behavior like "autogrow":http://plugins.jquery.com/project/autogrow textareas:
+Customize HTML attributes for any input using the @:input_html@ option. Typically this is used to disable the input, change the size of a text field, change the rows in a textarea, or even to add a special class to an input to attach special behavior like "autogrow":http://plugins.jquery.com/project/autogrow textareas:
<pre>
<% semantic_form_for @post do |form| %>
View
10 lib/formtastic.rb
@@ -1357,7 +1357,7 @@ def default_input_type(method, options = {}) #:nodoc:
case column.type
when :string
return :password if method.to_s =~ /password/
- return :country if method.to_s =~ /country/
+ return :country if method.to_s =~ /country$/
return :time_zone if method.to_s =~ /time_zone/
when :integer
return :select if method.to_s =~ /_id$/
@@ -1414,7 +1414,13 @@ def find_raw_collection_for_column(column, options) #:nodoc:
collection = if options[:collection]
options.delete(:collection)
elsif reflection = self.reflection_for(column)
- reflection.klass.find(:all, options[:find_options] || {})
+ options[:find_options] ||= {}
+
+ if conditions = reflection.options[:conditions]
+ options[:find_options][:conditions] = reflection.klass.merge_conditions(conditions, options[:find_options][:conditions])
+ end
+
+ reflection.klass.find(:all, options[:find_options])
else
create_boolean_collection(options)
end
View
38 spec/inputs/country_input_spec.rb
@@ -77,6 +77,42 @@
end
end
-
+
+ describe "matching" do
+
+ describe "when the attribute is 'country'" do
+
+ before do
+ @form = semantic_form_for(@new_post) do |builder|
+ builder.stub!(:country_select).and_return("<select><option>...</option></select>")
+ concat(builder.input(:country))
+ end
+ end
+
+ it "should render a country input" do
+ output_buffer.concat(@form) if defined?(ActiveSupport::SafeBuffer)
+ output_buffer.should have_tag "form li.country"
+ end
+ end
+
+ describe "whent the attribute is 'country_something'" do
+
+ before do
+ @form = semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:country_subdivision))
+ concat(builder.input(:country_code))
+ end
+ end
+
+ it "should render a country input" do
+ output_buffer.concat(@form) if defined?(ActiveSupport::SafeBuffer)
+ output_buffer.should_not have_tag "form li.country"
+ output_buffer.should have_tag "form li.string", :count => 2
+ end
+
+ end
+
+ end
+
end
View
35 spec/inputs/select_input_spec.rb
@@ -98,6 +98,7 @@
before do
@form = semantic_form_for(@new_post) do |builder|
concat(builder.input(:author, :as => :select))
+ concat(builder.input(:reviewer, :as => :select))
end
end
@@ -113,12 +114,14 @@
output_buffer.concat(@form) if defined?(ActiveSupport::SafeBuffer)
output_buffer.should have_tag('form li select')
output_buffer.should have_tag('form li select#post_author_id')
+ output_buffer.should have_tag('form li select#post_reviewer_id')
end
it 'should have a valid name' do
output_buffer.concat(@form) if defined?(ActiveSupport::SafeBuffer)
output_buffer.should have_tag("form li select[@name='post[author_id]']")
output_buffer.should_not have_tag("form li select[@name='post[author_id][]']")
+ output_buffer.should_not have_tag("form li select[@name='post[reviewer_id][]']")
end
it 'should not create a multi-select' do
@@ -138,15 +141,15 @@
it 'should have a select option for each Author' do
output_buffer.concat(@form) if defined?(ActiveSupport::SafeBuffer)
- output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size + 1)
+ output_buffer.should have_tag("form li select[@name='post[author_id]'] option", :count => ::Author.find(:all).size + 1)
::Author.find(:all).each do |author|
output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
end
end
it 'should have one option with a "selected" attribute' do
output_buffer.concat(@form) if defined?(ActiveSupport::SafeBuffer)
- output_buffer.should have_tag('form li select option[@selected]', :count => 1)
+ output_buffer.should have_tag("form li select[@name='post[author_id]'] option[@selected]", :count => 1)
end
it 'should not singularize the association name' do
@@ -174,6 +177,34 @@
end
end
+ describe "for a belongs_to association with :conditions" do
+ before do
+ ::Post.stub!(:reflect_on_association).with(:author).and_return do
+ mock = mock('reflection', :options => {:conditions => {:active => true}}, :klass => ::Author, :macro => :belongs_to)
+ mock.stub!(:[]).with(:class_name).and_return("Author")
+ mock
+ end
+ end
+
+ it "should call author.find with association conditions" do
+ ::Author.should_receive(:merge_conditions).with({:active => true}, nil).and_return(:active => true)
+ ::Author.should_receive(:find).with(:all, :conditions => {:active => true})
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author, :as => :select))
+ end
+ end
+
+ it "should call author.find with association conditions and find_options conditions" do
+ ::Author.should_receive(:merge_conditions).with({:active => true}, {:publisher => true}).and_return(:active => true, :publisher => true)
+ ::Author.should_receive(:find).with(:all, :conditions => {:active => true, :publisher => true})
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author, :as => :select, :find_options => {:conditions => {:publisher => true}}))
+ end
+ end
+ end
+
describe 'for a belongs_to association with :group_by => :continent' do
before do
@authors = [@bob, @fred, @fred, @fred]
View
1 spec/inputs_spec.rb
@@ -438,5 +438,4 @@
end
end
-
end
View
14 spec/spec_helper.rb
@@ -186,6 +186,7 @@ def new_author_path; "/authors/new"; end
@new_post.stub!(:new_record?).and_return(true)
@new_post.stub!(:errors).and_return(mock('errors', :[] => nil))
@new_post.stub!(:author).and_return(nil)
+ @new_post.stub!(:reviewer).and_return(nil)
@new_post.stub!(:main_post).and_return(nil)
@new_post.stub!(:sub_posts).and_return([]) #TODO should be a mock with methods for adding sub posts
@new_post.stub!(:to_key).and_return(nil)
@@ -218,6 +219,10 @@ def new_author_path; "/authors/new"; end
mock = mock('reflection', :options => {}, :klass => ::Author, :macro => :belongs_to)
mock.stub!(:[]).with(:class_name).and_return("Author")
mock
+ when :reviewer
+ mock = mock('reflection', :options => {:class_name => 'Author'}, :klass => ::Author, :macro => :belongs_to)
+ mock.stub!(:[]).with(:class_name).and_return("Author")
+ mock
when :authors
mock('reflection', :options => {}, :klass => ::Author, :macro => :has_and_belongs_to_many)
when :sub_posts
@@ -241,6 +246,9 @@ def new_author_path; "/authors/new"; end
@new_post.stub!(:time_zone)
@new_post.stub!(:category_name)
@new_post.stub!(:allow_comments)
+ @new_post.stub!(:country)
+ @new_post.stub!(:country_subdivision)
+ @new_post.stub!(:country_code)
@new_post.stub!(:column_for_attribute).with(:meta_description).and_return(mock('column', :type => :string, :limit => 255))
@new_post.stub!(:column_for_attribute).with(:title).and_return(mock('column', :type => :string, :limit => 50))
@new_post.stub!(:column_for_attribute).with(:body).and_return(mock('column', :type => :text))
@@ -249,10 +257,16 @@ def new_author_path; "/authors/new"; end
@new_post.stub!(:column_for_attribute).with(:time_zone).and_return(mock('column', :type => :string))
@new_post.stub!(:column_for_attribute).with(:allow_comments).and_return(mock('column', :type => :boolean))
@new_post.stub!(:column_for_attribute).with(:author).and_return(mock('column', :type => :integer))
+ @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!(:author).and_return(@bob)
@new_post.stub!(:author_id).and_return(@bob.id)
+ @new_post.stub!(:reviewer).and_return(@fred)
+ @new_post.stub!(:reviewer_id).and_return(@fred.id)
+
@new_post.should_receive(:publish_at=).any_number_of_times
@new_post.should_receive(:title=).any_number_of_times
@new_post.stub!(:main_post_id).and_return(nil)

0 comments on commit 2814011

Please sign in to comment.
Something went wrong with that request. Please try again.