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

Added option to pass block to link_to_function #5886

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions actionpack/lib/action_view/helpers/javascript_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def button_to_function(name, function=nil, html_options={})

# Returns a link whose +onclick+ handler triggers the passed JavaScript.
#
# The helper receives a name, JavaScript code, and an optional hash of HTML options. The
# name is used as the link text and the JavaScript code goes into the +onclick+ attribute.
# The helper receives a name or block, JavaScript code, and an optional hash of HTML options. The
# name is used as the link text or you can pass a block, and the JavaScript code goes into the +onclick+ attribute.
# If +html_options+ has an <tt>:onclick</tt>, that one is put before +function+. Once all
# the JavaScript is set, the helper appends "; return false;".
#
Expand All @@ -96,7 +96,24 @@ def button_to_function(name, function=nil, html_options={})
# link_to_function "Greeting", "alert('Hello world!')", :class => "nav_link"
# # => <a class="nav_link" href="#" onclick="alert('Hello world!'); return false;">Greeting</a>
#
def link_to_function(name, function, html_options={})
# You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
#
# <%= link_to_function "alert('Hello there!')", :class => "nav_link" do %>
# Hello, <strong><%= @profile.name %></strong>
# <% end %>
# # => <a class="nav_link" href="#" onclick="alert('Hello there!'); return false;">
# Hello, <strong>David</strong>
# </a>
#
def link_to_function(*args, &block)
if block_given?
return link_to_function(capture(&block), *args)
end

name = args.first
function = args.second
html_options = args.third || {}

onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;"
href = html_options[:href] || '#'

Expand Down
7 changes: 7 additions & 0 deletions actionpack/test/template/javascript_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class JavaScriptHelperTest < ActionView::TestCase
tests ActionView::Helpers::JavaScriptHelper

include RenderERBUtils

def _evaluate_assigns_and_ivars() end

attr_accessor :formats, :output_buffer
Expand Down Expand Up @@ -72,6 +74,11 @@ def test_function_with_href
link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
end

def test_link_to_function_tag_using_block_in_erb
out = render_erb %{<%= link_to_function("alert('Hello world!')", :href => "http://example.com") do %>Example site<% end %>}
assert_equal '<a href="http://example.com" onclick="alert(\'Hello world!\'); return false;">Example site</a>', out
end

def test_javascript_tag
self.output_buffer = 'foo'

Expand Down