aboutsummaryrefslogtreecommitdiffstats
path: root/actionview
diff options
context:
space:
mode:
authorTyler Hunt <tyler@tylerhunt.com>2017-01-02 23:46:24 -0500
committerTyler Hunt <tyler@tylerhunt.com>2017-01-03 09:27:57 -0500
commit3d2645ab138c9158ca4224458de74c723d4441cc (patch)
treed602b1b10f22e494f3c31bbc34e2c9231983381d /actionview
parent33e60514aed85b3076f2636d5f1ccfb513aace1c (diff)
downloadrails-3d2645ab138c9158ca4224458de74c723d4441cc.tar.gz
rails-3d2645ab138c9158ca4224458de74c723d4441cc.tar.bz2
rails-3d2645ab138c9158ca4224458de74c723d4441cc.zip
Prevent duplicate data-disable-with attributes
When using the hash form of the data option, prevent duplicate data-disable-with attributes from appearing in the submit tag due to both string and symbol forms of the hash key being present.
Diffstat (limited to 'actionview')
-rw-r--r--actionview/lib/action_view/helpers/form_tag_helper.rb32
-rw-r--r--actionview/test/template/form_tag_helper_test.rb7
2 files changed, 25 insertions, 14 deletions
diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb
index ffc52569f2..ffc64e7118 100644
--- a/actionview/lib/action_view/helpers/form_tag_helper.rb
+++ b/actionview/lib/action_view/helpers/form_tag_helper.rb
@@ -442,21 +442,9 @@ module ActionView
# # => <input name='commit' type='submit' value='Save' data-disable-with="Save" data-confirm="Are you sure?" />
#
def submit_tag(value = "Save changes", options = {})
- options = options.stringify_keys
+ options = options.deep_stringify_keys
tag_options = { "type" => "submit", "name" => "commit", "value" => value }.update(options)
-
- if ActionView::Base.automatically_disable_submit_tag
- unless tag_options["data-disable-with"] == false || (tag_options["data"] && tag_options["data"][:disable_with] == false)
- disable_with_text = tag_options["data-disable-with"]
- disable_with_text ||= tag_options["data"][:disable_with] if tag_options["data"]
- disable_with_text ||= value.to_s.clone
- tag_options.deep_merge!("data" => { "disable_with" => disable_with_text })
- else
- tag_options["data"].delete(:disable_with) if tag_options["data"]
- end
- tag_options.delete("data-disable-with")
- end
-
+ set_default_disable_with value, tag_options
tag :input, tag_options
end
@@ -898,6 +886,22 @@ module ActionView
def sanitize_to_id(name)
name.to_s.delete("]").tr("^-a-zA-Z0-9:.", "_")
end
+
+ def set_default_disable_with(value, tag_options)
+ return unless ActionView::Base.automatically_disable_submit_tag
+ data = tag_options["data"]
+
+ unless tag_options["data-disable-with"] == false || (data && data["disable_with"] == false)
+ disable_with_text = tag_options["data-disable-with"]
+ disable_with_text ||= data["disable_with"] if data
+ disable_with_text ||= value.to_s.clone
+ tag_options.deep_merge!("data" => { "disable_with" => disable_with_text })
+ else
+ data.delete("disable_with") if data
+ end
+
+ tag_options.delete("data-disable-with")
+ end
end
end
end
diff --git a/actionview/test/template/form_tag_helper_test.rb b/actionview/test/template/form_tag_helper_test.rb
index 1248a0bb09..084c540139 100644
--- a/actionview/test/template/form_tag_helper_test.rb
+++ b/actionview/test/template/form_tag_helper_test.rb
@@ -510,6 +510,13 @@ class FormTagHelperTest < ActionView::TestCase
)
end
+ def test_submit_tag_doesnt_have_data_disable_with_twice_with_hash
+ assert_equal(
+ %(<input type="submit" name="commit" value="Save" data-disable-with="Processing..." />),
+ submit_tag("Save", data: { disable_with: "Processing..." })
+ )
+ end
+
def test_submit_tag_with_symbol_value
assert_dom_equal(
%(<input data-disable-with="Save" name='commit' type="submit" value="Save" />),