Permalink
Browse files

Added backward compatibility with Rails 3.1 for form options

  • Loading branch information...
1 parent 82da2c2 commit 8bc627d8e8f271e4af99ac1bf2c66478f59eb831 @carlosramireziii carlosramireziii committed Feb 9, 2012
Showing with 43 additions and 7 deletions.
  1. +15 −5 lib/client_side_validations/action_view/form_helper.rb
  2. +28 −2 test/action_view/cases/test_helpers.rb
@@ -75,13 +75,23 @@ def client_side_form_settings(object, options)
if options[:html] && options[:html][:id]
var_name = options[:html][:id]
else
- # This might break 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] ? "edit_#{options[:as]}" : [options[:namespace], dom_id(object, :edit)].compact.join("_")
+ 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] ? "new_#{options[:as]}" : [options[:namespace], dom_id(object)].compact.join("_")
+ # 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
@@ -641,7 +641,8 @@ def test_pushing_script_to_content_for
assert_equal build_script_tag(nil, "edit_post_123", validators), content_for(:post)
end
- def test_as_form_option_with_new_record
+ 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
@@ -651,7 +652,8 @@ def test_as_form_option_with_new_record
assert_equal expected, output_buffer
end
- def test_as_form_option_with_existing_record
+ 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
@@ -661,7 +663,30 @@ def test_as_form_option_with_existing_record
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
@@ -672,6 +697,7 @@ def test_namespace_form_option_with_new_record
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

0 comments on commit 8bc627d

Please sign in to comment.