From 9345a116af134e6963cda261c448c9ecdb3832ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Galdino=20+=20Rafael=20Mendon=C3=A7a=20Fran=C3=A7?= =?UTF-8?q?a?= Date: Sat, 21 Jul 2012 15:20:26 -0300 Subject: [PATCH] Add back `:confirm` and change deprecation horizon to 4.1 --- actionpack/CHANGELOG.md | 4 +- .../action_view/helpers/form_tag_helper.rb | 37 ++++++++++++ .../lib/action_view/helpers/url_helper.rb | 19 ++++++ .../test/template/form_tag_helper_test.rb | 28 +++++++++ actionpack/test/template/url_helper_test.rb | 58 +++++++++++++++++++ 5 files changed, 144 insertions(+), 2 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index da89a7a43c..38e48037fc 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,8 +1,8 @@ ## Rails 4.0.0 (unreleased) ## -* Remove `:confirm` in favor of `:data => { :confirm => "Text" }` option for `button_to`, `button_tag`, `image_submit_tag`, `link_to` and `submit_tag` helpers. +* Deprecate `:confirm` in favor of `:data => { :confirm => "Text" }` option for `button_to`, `button_tag`, `image_submit_tag`, `link_to` and `submit_tag` helpers. - *Carlos Galdino* + *Carlos Galdino + Rafael Mendonça França* * Show routes in exception page while debugging a `RoutingError` in development. *Richard Schneeman and Mattt Thompson* diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 4b8484bfb5..87f1f4d92f 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -386,6 +386,12 @@ def radio_button_tag(name, value, checked = false, options = {}) # * :disabled - If true, the user will not be able to use this input. # * Any other key creates standard HTML options for the tag. # + # ==== Data attributes + # + # * :confirm => 'question?' - If present the unobtrusive JavaScript + # drivers will provide a prompt with the question specified. If the user accepts, + # the form is processed normally, otherwise no action is taken. + # # ==== Examples # submit_tag # # => @@ -411,6 +417,12 @@ def radio_button_tag(name, value, checked = false, options = {}) def submit_tag(value = "Save changes", options = {}) options = options.stringify_keys + if confirm = options.delete("confirm") + ActiveSupport::Deprecation.warn ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead'" + + options["data-confirm"] = confirm + end + tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options) end @@ -427,6 +439,13 @@ def submit_tag(value = "Save changes", options = {}) # use this input. # * Any other key creates standard HTML options for the tag. # + # ==== Data attributes + # + # * :confirm => 'question?' - If present, the + # unobtrusive JavaScript drivers will provide a prompt with + # the question specified. If the user accepts, the form is + # processed normally, otherwise no action is taken. + # # ==== Examples # button_tag # # => @@ -443,6 +462,12 @@ def button_tag(content_or_options = nil, options = nil, &block) options ||= {} options = options.stringify_keys + if confirm = options.delete("confirm") + ActiveSupport::Deprecation.warn ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead'" + + options["data-confirm"] = confirm + end + options.reverse_merge! 'name' => 'button', 'type' => 'submit' content_tag :button, content_or_options || 'Button', options, &block @@ -457,6 +482,12 @@ def button_tag(content_or_options = nil, options = nil, &block) # * :disabled - If set to true, the user will not be able to use this input. # * Any other key creates standard HTML options for the tag. # + # ==== Data attributes + # + # * :confirm => 'question?' - This will add a JavaScript confirm + # prompt with the question specified. If the user accepts, the form is + # processed normally, otherwise no action is taken. + # # ==== Examples # image_submit_tag("login.png") # # => @@ -475,6 +506,12 @@ def button_tag(content_or_options = nil, options = nil, &block) def image_submit_tag(source, options = {}) options = options.stringify_keys + if confirm = options.delete("confirm") + ActiveSupport::Deprecation.warn ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead'" + + options["data-confirm"] = confirm + end + tag :input, { "type" => "image", "src" => path_to_image(source) }.update(options) end diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index b4eb3d4826..04d9c69e43 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -160,6 +160,12 @@ def url_for(options = nil) # completion of the Ajax request and performing JavaScript operations once # they're complete # + # ==== Data attributes + # + # * :confirm => 'question?' - This will allow the unobtrusive JavaScript + # driver to prompt with the question specified. If the user accepts, the link is + # processed normally, otherwise no action is taken. + # # ==== Examples # Because it relies on +url_for+, +link_to+ supports both older-style controller/action/id arguments # and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base @@ -274,6 +280,12 @@ def link_to(name = nil, options = nil, html_options = nil, &block) # * :form_class - This controls the class of the form within which the submit button will # be placed # + # ==== Data attributes + # + # * :confirm - This will use the unobtrusive JavaScript driver to + # prompt with the question specified. If the user accepts, the link is + # processed normally, otherwise no action is taken. + # # ==== Examples # <%= button_to "New", :action => "new" %> # # => "
@@ -623,8 +635,15 @@ def convert_options_to_data_attributes(options, html_options) html_options = html_options.stringify_keys html_options['data-remote'] = 'true' if link_to_remote_options?(options) || link_to_remote_options?(html_options) + confirm = html_options.delete('confirm') method = html_options.delete('method') + if confirm + ActiveSupport::Deprecation.warn ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead" + + html_options["data-confirm"] = confirm + end + add_method_to_attributes!(html_options, method) if method html_options diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 05baaa5a85..72edb047cf 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -386,6 +386,15 @@ def test_submit_tag_with_confirmation ) end + def test_submit_tag_with_deprecated_confirmation + assert_deprecated ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead" do + assert_dom_equal( + %(), + submit_tag("Save", :confirm => "Are you sure?") + ) + end + end + def test_button_tag assert_dom_equal( %(), @@ -444,6 +453,15 @@ def test_button_tag_with_confirmation ) end + def test_button_tag_with_deprecated_confirmation + assert_deprecated ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead" do + assert_dom_equal( + %(), + button_tag("Save", :type => "submit", :confirm => "Are you sure?") + ) + end + end + def test_image_submit_tag_with_confirmation assert_dom_equal( %(), @@ -451,6 +469,16 @@ def test_image_submit_tag_with_confirmation ) end + def test_image_submit_tag_with_deprecated_confirmation + assert_deprecated ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead" do + assert_dom_equal( + %(), + image_submit_tag("save.gif", :confirm => "Are you sure?") + ) + end + end + + def test_color_field_tag expected = %{} assert_dom_equal(expected, color_field_tag("car")) diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index a09409635d..893197a115 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -94,6 +94,15 @@ def test_button_to_with_javascript_confirm ) end + def test_button_to_with_deprecated_confirm + assert_deprecated ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead" do + assert_dom_equal( + "
", + button_to("Hello", "http://www.example.com", :confirm => "Are you sure?") + ) + end + end + def test_button_to_with_javascript_disable_with assert_dom_equal( "
", @@ -112,6 +121,15 @@ def test_button_to_with_remote_and_javascript_confirm ) end + def test_button_to_with_remote_and_javascript_with_deprecated_confirm + assert_deprecated ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead" do + assert_dom_equal( + "
", + button_to("Hello", "http://www.example.com", :remote => true, :confirm => "Are you sure?") + ) + end + end + def test_button_to_with_remote_false assert_dom_equal( "
", @@ -220,6 +238,27 @@ def test_link_tag_with_javascript_confirm ) end + def test_link_tag_with_deprecated_confirm + assert_deprecated ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead" do + assert_dom_equal( + "Hello", + link_to("Hello", "http://www.example.com", :confirm => "Are you sure?") + ) + end + assert_deprecated ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead" do + assert_dom_equal( + "Hello", + link_to("Hello", "http://www.example.com", :confirm => "You can't possibly be sure, can you?") + ) + end + assert_deprecated ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead" do + assert_dom_equal( + "Hello", + link_to("Hello", "http://www.example.com", :confirm => "You can't possibly be sure,\n can you?") + ) + end + end + def test_link_to_with_remote assert_dom_equal( "Hello", @@ -269,6 +308,15 @@ def test_link_tag_using_post_javascript_and_confirm ) end + def test_link_tag_using_post_javascript_and_with_deprecated_confirm + assert_deprecated ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead" do + assert_dom_equal( + "Hello", + link_to("Hello", "http://www.example.com", :method => :post, :confirm => "Are you serious?") + ) + end + end + def test_link_tag_using_delete_javascript_and_href_and_confirm assert_dom_equal( "Destroy", @@ -277,6 +325,16 @@ def test_link_tag_using_delete_javascript_and_href_and_confirm ) end + def test_link_tag_using_delete_javascript_and_href_and_with_deprecated_confirm + assert_deprecated ":confirm option is deprecated and will be removed from Rails 4.1. Use ':data => { :confirm => \'Text\' }' instead" do + assert_dom_equal( + "Destroy", + link_to("Destroy", "http://www.example.com", :method => :delete, :href => '#', :confirm => "Are you serious?"), + "When specifying url, form should be generated with it, but not this.href" + ) + end + end + def test_link_tag_with_block assert_dom_equal 'Example site', link_to('/') { content_tag(:span, 'Example site') } -- GitLab