Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate data-disable-with attributes #27548

Merged
merged 1 commit into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 18 additions & 14 deletions actionview/lib/action_view/helpers/form_tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,21 +442,9 @@ def radio_button_tag(name, value, checked = false, options = {})
# # => <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

Expand Down Expand Up @@ -898,6 +886,22 @@ def form_tag_with_body(html_options, content)
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
7 changes: 7 additions & 0 deletions actionview/test/template/form_tag_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,13 @@ def test_submit_tag_doesnt_have_data_disable_with_twice
)
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" />),
Expand Down