Permalink
Browse files

Merge pull request #272 from carlosramireziii/master

Support for form helper options :as, :namespace
  • Loading branch information...
bcardarella committed Feb 9, 2012
2 parents 7942a96 + 8bc627d commit bf593243aac11d0d2e65bae1e6271850f40a0241
@@ -75,11 +75,23 @@ def client_side_form_settings(object, options)
if options[:html] && options[:html][:id]
var_name = options[:html][:id]
else
- var_name = if object.respond_to?(:persisted?) && object.persisted?
- options[:as] ? "#{options[:as]}_edit" : dom_id(object, :edit)
+ if Rails.version >= '3.2.0'
+ var_name = if object.respond_to?(:persisted?) && object.persisted?
+ options[:as] ? "edit_#{options[:as]}" : [options[:namespace], dom_id(object, :edit)].compact.join("_")
+ else
+ options[:as] ? "new_#{options[:as]}" : [options[:namespace], dom_id(object)].compact.join("_")
+ end
else
- options[:as] ? "#{options[:as]}_new" : dom_id(object)
+ # This is to maintain backward compatibility with Rails 3.1
+ # see: https://github.com/rails/rails/commit/e29773f885fd500189ffd964550ae20061d745ba#commitcomment-948052
+ var_name = if object.respond_to?(:persisted?) && object.persisted?
+ options[:as] ? "#{options[:as]}_edit" : dom_id(object, :edit)
+ else
+ options[:as] ? "#{options[:as]}_new" : dom_id(object)
+ end
end
+
+
end
content_tag(:script) do
@@ -169,7 +169,7 @@ def admin_comment_path(post, comment)
end
alias_method :admin_post_comment_path, :admin_comment_path
- def posts_path
+ def posts_path(options={})
"/posts"
end
@@ -640,5 +640,71 @@ def test_pushing_script_to_content_for
assert_equal expected, output_buffer
assert_equal build_script_tag(nil, "edit_post_123", validators), content_for(:post)
end
+
+ def test_as_form_option_with_new_record_rails_3_2
+ skip("This test is only applicable for Rails ~> v3.2.0") unless Rails.version >= '3.2.0'
+ test_buffer = form_for(Post.new, :as => :article, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts", "new_article", "new_article", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
+
+ def test_as_form_option_with_existing_record_rails_3_2
+ skip("This test is only applicable for Rails ~> v3.2.0") unless Rails.version >= '3.2.0'
+ test_buffer = form_for(@post, :as => :article, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts/123", "edit_article", "edit_article", :method => "put", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
+
+ def test_as_form_option_with_new_record_rails_3_1
+ skip("This test is only applicable for Rails ~> v3.1.0") if Rails.version >= '3.2.0'
+ test_buffer = form_for(Post.new, :as => :article, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts", "article_new", "article_new", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
+
+ def test_as_form_option_with_existing_record_rails_3_1
+ skip("This test is only applicable for Rails ~> v3.1.0") if Rails.version >= '3.2.0'
+ test_buffer = form_for(@post, :as => :article, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts/123", "article_edit", "article_edit", :method => "put", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
+
+ def test_namespace_form_option_with_new_record
+ skip("This test is only applicable for Rails ~> v3.2.0") unless Rails.version >= '3.2.0'
+ test_buffer = form_for(Post.new, :namespace => :blog, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts", "blog_new_post", "new_post", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
+
+ def test_namespace_form_option_with_existing_record
+ skip("This test is only applicable for Rails ~> v3.2.0") unless Rails.version >= '3.2.0'
+ test_buffer = form_for(@post, :namespace => :blog, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts/123", "blog_edit_post_123", "edit_post", :method => "put", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
end

0 comments on commit bf59324

Please sign in to comment.