Skip to content

Commit

Permalink
Merge pull request #10065 from spohlenz/mail_to_block
Browse files Browse the repository at this point in the history
Add block support for the mail_to helper
  • Loading branch information
carlosantoniodasilva committed Apr 5, 2013
2 parents 9e4c25e + 4bb26dd commit aa3b683
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##


* Add block support for the `mail_to` helper, similar to the `link_to` helper.

*Sam Pohlenz*

* Automatically configure cookie-based sessions to be encrypted if * Automatically configure cookie-based sessions to be encrypted if
`secret_key_base` is set, falling back to signed if only `secret_token` `secret_key_base` is set, falling back to signed if only `secret_token`
is set. Automatically upgrade existing signed cookie-based sessions from is set. Automatically upgrade existing signed cookie-based sessions from
Expand Down
21 changes: 16 additions & 5 deletions actionpack/lib/action_view/helpers/url_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -439,18 +439,29 @@ def link_to_if(condition, name, options = {}, html_options = {}, &block)
# mail_to "me@domain.com", "My email", cc: "ccaddress@domain.com", # mail_to "me@domain.com", "My email", cc: "ccaddress@domain.com",
# subject: "This is an example email" # subject: "This is an example email"
# # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a> # # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
def mail_to(email_address, name = nil, html_options = {}) #
email_address = ERB::Util.html_escape(email_address) # You can use a block as well if your link target is hard to fit into the name parameter. ERB example:

#
# <%= mail_to "me@domain.com" do %>
# <strong>Email me:</strong> <span>me@domain.com</span>
# <% end %>
# # => <a href="mailto:me@domain.com">
# <strong>Email me:</strong> <span>me@domain.com</span>
# </a>
def mail_to(email_address, name = nil, html_options = {}, &block)
html_options, name = name, nil if block_given?
html_options ||= {}
html_options.stringify_keys! html_options.stringify_keys!


email_address = ERB::Util.html_escape(email_address)

extras = %w{ cc bcc body subject }.map { |item| extras = %w{ cc bcc body subject }.map { |item|
option = html_options.delete(item) || next option = html_options.delete(item) || next
"#{item}=#{Rack::Utils.escape_path(option)}" "#{item}=#{Rack::Utils.escape_path(option)}"
}.compact }.compact
extras = extras.empty? ? '' : '?' + ERB::Util.html_escape(extras.join('&')) extras = extras.empty? ? '' : '?' + ERB::Util.html_escape(extras.join('&'))

content_tag "a", name || email_address.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe) content_tag(:a, name || email_address.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe), &block)
end end


# True if the current request URI was generated by the given +options+. # True if the current request URI was generated by the given +options+.
Expand Down
10 changes: 10 additions & 0 deletions actionpack/test/template/url_helper_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -538,6 +538,16 @@ def test_mail_to_returns_html_safe_string
assert mail_to("david@loudthinking.com").html_safe? assert mail_to("david@loudthinking.com").html_safe?
end end


def test_mail_to_with_block
assert_dom_equal %{<a href="mailto:me@example.com"><span>Email me</span></a>},
mail_to('me@example.com') { content_tag(:span, 'Email me') }
end

def test_link_tag_with_block_and_options
assert_dom_equal %{<a class="special" href="mailto:me@example.com?cc=ccaddress%40example.com"><span>Email me</span></a>},
mail_to('me@example.com', cc: "ccaddress@example.com", class: "special") { content_tag(:span, 'Email me') }
end

def protect_against_forgery? def protect_against_forgery?
self.request_forgery self.request_forgery
end end
Expand Down

0 comments on commit aa3b683

Please sign in to comment.