Permalink
Browse files

Fixes deprecation warnings with form_for(:name, @resource) in rails3 (#…

…245).

* replaced lots of unnecessary `semantic_form_for(:post, ::Post.new...)` with `semantic_form_for(@new_post...)`
* added rails3? and rails2? helper methods to spec_helper
* for the test where it did matter, I added a check for Rails 2 or 3 so we test the right code under both version
  • Loading branch information...
1 parent 90f890d commit 82f6c8d59dc528d3127028569ed7db635e3f1a62 @justinfrench committed Apr 19, 2010
Showing with 30 additions and 13 deletions.
  1. +21 −13 spec/form_helper_spec.rb
  2. +9 −0 spec/spec_helper.rb
View
@@ -13,20 +13,20 @@
describe '#semantic_form_for' do
it 'yields an instance of SemanticFormBuilder' do
- semantic_form_for(:post, ::Post.new, :url => '/hello') do |builder|
+ semantic_form_for(@new_post, :url => '/hello') do |builder|
builder.class.should == ::Formtastic::SemanticFormBuilder
end
end
it 'adds a class of "formtastic" to the generated form' do
- form = semantic_form_for(:post, ::Post.new, :url => '/hello') do |builder|
+ form = semantic_form_for(@new_post, :url => '/hello') do |builder|
end
output_buffer.concat(form) if defined?(ActiveSupport::SafeBuffer)
output_buffer.should have_tag("form.formtastic")
end
it 'adds class matching the object name to the generated form when a symbol is provided' do
- form = semantic_form_for(:post, ::Post.new, :url => '/hello') do |builder|
+ form = semantic_form_for(@new_post, :url => '/hello') do |builder|
end
output_buffer.concat(form) if defined?(ActiveSupport::SafeBuffer)
output_buffer.should have_tag("form.post")
@@ -53,7 +53,7 @@
describe 'allows :html options' do
before(:each) do
- @form = semantic_form_for(:post, ::Post.new, :url => '/hello', :html => { :id => "something-special", :class => "something-extra", :multipart => true }) do |builder|
+ @form = semantic_form_for(@new_post, :url => '/hello', :html => { :id => "something-special", :class => "something-extra", :multipart => true }) do |builder|
end
end
@@ -79,16 +79,24 @@
builder.object_name.should == "post"
end
end
-
+
it 'can be called with a generic style and instance variable' do
- semantic_form_for(:post, @new_post, :url => new_post_path) do |builder|
- builder.object.class.should == ::Post
- builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
+ if rails3?
+ semantic_form_for(@new_post, :as => :post, :url => new_post_path) do |builder|
+ builder.object.class.should == ::Post
+ builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
+ end
+ end
+ if rails2?
+ semantic_form_for(:post, @new_post, :url => new_post_path) do |builder|
+ builder.object.class.should == ::Post
+ builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
+ end
end
end
it 'can be called with a generic style and inline object' do
- semantic_form_for(:post, ::Post.new, :url => new_post_path) do |builder|
+ semantic_form_for(@new_post, :url => new_post_path) do |builder|
builder.object.class.should == ::Post
builder.object_name.to_s.should == "post" # TODO: is this forced .to_s a bad assumption somewhere?
end
@@ -98,7 +106,7 @@
it "yields an instance of the given builder" do
class MyAwesomeCustomBuilder < ::Formtastic::SemanticFormBuilder
end
- semantic_form_for(:post, ::Post.new, :url => '/hello', :builder => MyAwesomeCustomBuilder) do |builder|
+ semantic_form_for(@new_post, :url => '/hello', :builder => MyAwesomeCustomBuilder) do |builder|
builder.class.should == MyAwesomeCustomBuilder
end
end
@@ -108,23 +116,23 @@ class MyAwesomeCustomBuilder < ::Formtastic::SemanticFormBuilder
describe '#semantic_fields_for' do
it 'yields an instance of SemanticFormBuilder' do
- semantic_fields_for(:post, ::Post.new, :url => '/hello') do |builder|
+ semantic_fields_for(@new_post, :url => '/hello') do |builder|
builder.class.should == ::Formtastic::SemanticFormBuilder
end
end
end
describe '#semantic_form_remote_for' do
it 'yields an instance of SemanticFormBuilder' do
- semantic_form_remote_for(:post, ::Post.new, :url => '/hello') do |builder|
+ semantic_form_remote_for(@new_post, :url => '/hello') do |builder|
builder.class.should == ::Formtastic::SemanticFormBuilder
end
end
end
describe '#semantic_form_for_remote' do
it 'yields an instance of SemanticFormBuilder' do
- semantic_remote_form_for(:post, ::Post.new, :url => '/hello') do |builder|
+ semantic_remote_form_for(@new_post, :url => '/hello') do |builder|
builder.class.should == ::Formtastic::SemanticFormBuilder
end
end
View
@@ -45,6 +45,7 @@ class OutputBuffer < Formtastic::Util.rails_safe_buffer_class
end
module FormtasticSpecHelper
+ include ActionPack
include ActionView::Context if defined?(ActionView::Context)
include ActionView::Helpers::FormHelper
include ActionView::Helpers::FormTagHelper
@@ -63,6 +64,14 @@ module FormtasticSpecHelper
include Formtastic::SemanticFormHelper
+ def rails3?
+ ActionPack::VERSION::MAJOR > 2
+ end
+
+ def rails2?
+ ActionPack::VERSION::MAJOR == 2
+ end
+
def default_input_type(column_type, column_name = :generic_column_name)
@new_post.stub!(column_name)
@new_post.stub!(:column_for_attribute).and_return(mock('column', :type => column_type)) unless column_type.nil?

0 comments on commit 82f6c8d

Please sign in to comment.