Add block support for the helper
This commit is contained in:
parent
37683266d8
commit
4bb26dd7b2
@ -1,5 +1,9 @@
|
||||
## 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
|
||||
`secret_key_base` is set, falling back to signed if only `secret_token`
|
||||
is set. Automatically upgrade existing signed cookie-based sessions from
|
||||
|
@ -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",
|
||||
# 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>
|
||||
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!
|
||||
|
||||
email_address = ERB::Util.html_escape(email_address)
|
||||
|
||||
extras = %w{ cc bcc body subject }.map { |item|
|
||||
option = html_options.delete(item) || next
|
||||
"#{item}=#{Rack::Utils.escape_path(option)}"
|
||||
}.compact
|
||||
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
|
||||
|
||||
# True if the current request URI was generated by the given +options+.
|
||||
|
@ -538,6 +538,16 @@ def test_mail_to_returns_html_safe_string
|
||||
assert mail_to("david@loudthinking.com").html_safe?
|
||||
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?
|
||||
self.request_forgery
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user