Skip to content

Commit 5d15287

Browse files
committed
Deprecate button_to_function and link_to_function helpers.
We recommend the use of Unobtrusive JavaScript instead. For example: link_to "Greeting", "#", :class => "nav_link" $(function() { $('.nav_link').click(function() { // Some complex code return false; }); }); or link_to "Greeting", '#', onclick: "alert('Hello world!'); return false", class: "nav_link" for simple cases. This reverts commit 3acdd65.
1 parent 7dedfde commit 5d15287

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

actionpack/CHANGELOG.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* Deprecate `button_to_function` and `link_to_function` helpers.
4+
5+
We recommend the use of Unobtrusive JavaScript instead. For example:
6+
7+
link_to "Greeting", "#", :class => "nav_link"
8+
9+
$(function() {
10+
$('.nav_link').click(function() {
11+
// Some complex code
12+
13+
return false;
14+
});
15+
});
16+
17+
or
18+
19+
link_to "Greeting", '#', onclick: "alert('Hello world!'); return false", class: "nav_link"
20+
21+
for simple cases.
22+
23+
*Rafael Mendonça França*
24+
325
* `javascript_include_tag :all` will now not include `application.js` if the file does not exists. *Prem Sichanugrist*
426

527
* Send an empty response body when call `head` with status between 100 and 199, 204, 205 or 304.
@@ -146,8 +168,6 @@
146168
* Replace `include_seconds` boolean argument with `:include_seconds => true` option
147169
in `distance_of_time_in_words` and `time_ago_in_words` signature. *Dmitriy Kiriyenko*
148170

149-
* Remove `button_to_function` and `link_to_function` helpers. *Rafael Mendonça França*
150-
151171
* Make current object and counter (when it applies) variables accessible when
152172
rendering templates with :object / :collection. *Carlos Antonio da Silva*
153173

actionpack/lib/action_view/helpers/javascript_helper.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,46 @@ def javascript_tag(content_or_options_with_block = nil, html_options = {}, &bloc
6767
def javascript_cdata_section(content) #:nodoc:
6868
"\n//#{cdata_section("\n#{content}\n//")}\n".html_safe
6969
end
70+
71+
# Returns a button whose +onclick+ handler triggers the passed JavaScript.
72+
#
73+
# The helper receives a name, JavaScript code, and an optional hash of HTML options. The
74+
# name is used as button label and the JavaScript code goes into its +onclick+ attribute.
75+
# If +html_options+ has an <tt>:onclick</tt>, that one is put before +function+.
76+
#
77+
# button_to_function "Greeting", "alert('Hello world!')", :class => "ok"
78+
# # => <input class="ok" onclick="alert('Hello world!');" type="button" value="Greeting" />
79+
#
80+
def button_to_function(name, function=nil, html_options={})
81+
message = "button_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead."
82+
ActiveSupport::Deprecation.warn message
83+
84+
onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function};"
85+
86+
tag(:input, html_options.merge(:type => 'button', :value => name, :onclick => onclick))
87+
end
88+
89+
# Returns a link whose +onclick+ handler triggers the passed JavaScript.
90+
#
91+
# The helper receives a name, JavaScript code, and an optional hash of HTML options. The
92+
# name is used as the link text and the JavaScript code goes into the +onclick+ attribute.
93+
# If +html_options+ has an <tt>:onclick</tt>, that one is put before +function+. Once all
94+
# the JavaScript is set, the helper appends "; return false;".
95+
#
96+
# The +href+ attribute of the tag is set to "#" unless +html_options+ has one.
97+
#
98+
# link_to_function "Greeting", "alert('Hello world!')", :class => "nav_link"
99+
# # => <a class="nav_link" href="#" onclick="alert('Hello world!'); return false;">Greeting</a>
100+
#
101+
def link_to_function(name, function, html_options={})
102+
message = "link_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead."
103+
ActiveSupport::Deprecation.warn message
104+
105+
onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;"
106+
href = html_options[:href] || '#'
107+
108+
content_tag(:a, name, html_options.merge(:href => href, :onclick => onclick))
109+
end
70110
end
71111
end
72112
end

actionpack/test/template/javascript_helper_test.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,48 @@ def test_escape_javascript_with_safebuffer
4242
assert_instance_of ActiveSupport::SafeBuffer, escape_javascript(ActiveSupport::SafeBuffer.new(given))
4343
end
4444

45+
def test_button_to_function
46+
assert_deprecated "button_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead." do
47+
assert_dom_equal %(<input type="button" onclick="alert(&#x27;Hello world!&#x27;);" value="Greeting" />),
48+
button_to_function("Greeting", "alert('Hello world!')")
49+
end
50+
end
51+
52+
def test_button_to_function_with_onclick
53+
assert_deprecated "button_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead." do
54+
assert_dom_equal "<input onclick=\"alert(&#x27;Goodbye World :(&#x27;); alert(&#x27;Hello world!&#x27;);\" type=\"button\" value=\"Greeting\" />",
55+
button_to_function("Greeting", "alert('Hello world!')", :onclick => "alert('Goodbye World :(')")
56+
end
57+
end
58+
59+
def test_button_to_function_without_function
60+
assert_deprecated "button_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead." do
61+
assert_dom_equal "<input onclick=\";\" type=\"button\" value=\"Greeting\" />",
62+
button_to_function("Greeting")
63+
end
64+
end
65+
66+
def test_link_to_function
67+
assert_deprecated "link_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead." do
68+
assert_dom_equal %(<a href="#" onclick="alert(&#x27;Hello world!&#x27;); return false;">Greeting</a>),
69+
link_to_function("Greeting", "alert('Hello world!')")
70+
end
71+
end
72+
73+
def test_link_to_function_with_existing_onclick
74+
assert_deprecated "link_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead." do
75+
assert_dom_equal %(<a href="#" onclick="confirm(&#x27;Sanity!&#x27;); alert(&#x27;Hello world!&#x27;); return false;">Greeting</a>),
76+
link_to_function("Greeting", "alert('Hello world!')", :onclick => "confirm('Sanity!')")
77+
end
78+
end
79+
80+
def test_function_with_href
81+
assert_deprecated "link_to_function is deprecated and will be removed from Rails 4.1. Use Unobtrusive JavaScript instead." do
82+
assert_dom_equal %(<a href="http://example.com/" onclick="alert(&#x27;Hello world!&#x27;); return false;">Greeting</a>),
83+
link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
84+
end
85+
end
86+
4587
def test_javascript_tag
4688
self.output_buffer = 'foo'
4789

0 commit comments

Comments
 (0)