Skip to content

Commit aa3b683

Browse files
Merge pull request rails#10065 from spohlenz/mail_to_block
Add block support for the mail_to helper
2 parents 9e4c25e + 4bb26dd commit aa3b683

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

actionpack/CHANGELOG.md

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

3+
* Add block support for the `mail_to` helper, similar to the `link_to` helper.
4+
5+
*Sam Pohlenz*
6+
37
* Automatically configure cookie-based sessions to be encrypted if
48
`secret_key_base` is set, falling back to signed if only `secret_token`
59
is set. Automatically upgrade existing signed cookie-based sessions from

actionpack/lib/action_view/helpers/url_helper.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,18 +439,29 @@ def link_to_if(condition, name, options = {}, html_options = {}, &block)
439439
# mail_to "me@domain.com", "My email", cc: "ccaddress@domain.com",
440440
# subject: "This is an example email"
441441
# # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
442-
def mail_to(email_address, name = nil, html_options = {})
443-
email_address = ERB::Util.html_escape(email_address)
444-
442+
#
443+
# You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
444+
#
445+
# <%= mail_to "me@domain.com" do %>
446+
# <strong>Email me:</strong> <span>me@domain.com</span>
447+
# <% end %>
448+
# # => <a href="mailto:me@domain.com">
449+
# <strong>Email me:</strong> <span>me@domain.com</span>
450+
# </a>
451+
def mail_to(email_address, name = nil, html_options = {}, &block)
452+
html_options, name = name, nil if block_given?
453+
html_options ||= {}
445454
html_options.stringify_keys!
446455

456+
email_address = ERB::Util.html_escape(email_address)
457+
447458
extras = %w{ cc bcc body subject }.map { |item|
448459
option = html_options.delete(item) || next
449460
"#{item}=#{Rack::Utils.escape_path(option)}"
450461
}.compact
451462
extras = extras.empty? ? '' : '?' + ERB::Util.html_escape(extras.join('&'))
452-
453-
content_tag "a", name || email_address.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe)
463+
464+
content_tag(:a, name || email_address.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe), &block)
454465
end
455466

456467
# True if the current request URI was generated by the given +options+.

actionpack/test/template/url_helper_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,16 @@ def test_mail_to_returns_html_safe_string
538538
assert mail_to("david@loudthinking.com").html_safe?
539539
end
540540

541+
def test_mail_to_with_block
542+
assert_dom_equal %{<a href="mailto:me@example.com"><span>Email me</span></a>},
543+
mail_to('me@example.com') { content_tag(:span, 'Email me') }
544+
end
545+
546+
def test_link_tag_with_block_and_options
547+
assert_dom_equal %{<a class="special" href="mailto:me@example.com?cc=ccaddress%40example.com"><span>Email me</span></a>},
548+
mail_to('me@example.com', cc: "ccaddress@example.com", class: "special") { content_tag(:span, 'Email me') }
549+
end
550+
541551
def protect_against_forgery?
542552
self.request_forgery
543553
end

0 commit comments

Comments
 (0)