Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support for form helper options :as, :namespace #272

Merged
merged 2 commits into from

2 participants

@carlosramireziii

In Rails 3.2 the way in which form IDs were generated when using the :as option was changed (e29773f885)

The :namespace form option was also introduced in 3.2 (ad5c8b0d52)

Both of these changes were causing the client_side_validations settings object to be undefined because the form ID was different than the name that the gem was giving to the DOM window attribute which stored the settings.

I attempted to maintain backwards compatibility with Rails 3.1 to adhere to your "support two minor versions" rule. Tests are included.

Thank you!

@bcardarella
Owner

Thank-you. I'm not a fan of the conditional tests but I don't think there is any way around it. Nice pull request.

@bcardarella bcardarella merged commit bf59324 into DavyJonesLocker:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
18 lib/client_side_validations/action_view/form_helper.rb
@@ -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
View
2  test/action_view/cases/helper.rb
@@ -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
View
66 test/action_view/cases/test_helpers.rb
@@ -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
Something went wrong with that request. Please try again.