From 4bb26dd7b298b08f53c7e157ddc19dc7febcf37e Mon Sep 17 00:00:00 2001 From: Sam Pohlenz Date: Wed, 3 Apr 2013 14:42:48 +1030 Subject: Add block support for the helper --- actionpack/lib/action_view/helpers/url_helper.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index 775d93ed39..a1468b6e90 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -439,18 +439,29 @@ module ActionView # mail_to "me@domain.com", "My email", cc: "ccaddress@domain.com", # subject: "This is an example email" # # => My email - 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 %> + # Email me: me@domain.com + # <% end %> + # # => + # Email me: me@domain.com + # + 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+. -- cgit v1.2.3 From e936cf9c47e716a32b75703cb1a8badf96be8a6b Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 4 Apr 2013 21:25:44 -0300 Subject: Ensure mail_to helper does not modify the given html options hash --- actionpack/lib/action_view/helpers/url_helper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index a1468b6e90..bfe11fc1d7 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -425,8 +425,8 @@ module ActionView # * :bcc - Blind Carbon Copy additional recipients on the email. # # ==== Obfuscation - # Prior to Rails 4.0, +mail_to+ provided options for encoding the address - # in order to hinder email harvesters. To take advantage of these options, + # Prior to Rails 4.0, +mail_to+ provided options for encoding the address + # in order to hinder email harvesters. To take advantage of these options, # install the +actionview-encoded_mail_to+ gem. # # ==== Examples @@ -451,7 +451,7 @@ module ActionView 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 = html_options.stringify_keys email_address = ERB::Util.html_escape(email_address) -- cgit v1.2.3 From 797fcdf738a2a2772544731027d4fc5ca9d358bc Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 4 Apr 2013 21:29:50 -0300 Subject: Refactor mail_to to not generate intermediate hashes when adding href There's no need to use Hash#merge with a new hash just for setting the href option to pass it through. Since we're always dealing with a new html_options hash, we're free to just set the value instead. --- actionpack/lib/action_view/helpers/url_helper.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index bfe11fc1d7..22059a0170 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -449,19 +449,20 @@ module ActionView # Email me: me@domain.com # def mail_to(email_address, name = nil, html_options = {}, &block) - html_options, name = name, nil if block_given? - html_options ||= {} - html_options = html_options.stringify_keys - email_address = ERB::Util.html_escape(email_address) + html_options, name = name, nil if block_given? + html_options = (html_options || {}).stringify_keys + 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), &block) + html_options["href"] = "mailto:#{email_address}#{extras}".html_safe + + content_tag(:a, name || email_address.html_safe, html_options, &block) end # True if the current request URI was generated by the given +options+. -- cgit v1.2.3 From 48dc5192eff45fce5ce39c41cdc3188be97ca614 Mon Sep 17 00:00:00 2001 From: Ryan McGeary Date: Fri, 5 Apr 2013 14:51:15 -0400 Subject: Fix explicit names on multiple file fields If a file field tag is passed the multiple option, it is turned into an array field (appending "[]"), but if the file field is passed an explicit name as an option, leave the name alone (do not append "[]"). Fixes #9830 --- actionpack/lib/action_view/helpers/tags/base.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/tags/base.rb b/actionpack/lib/action_view/helpers/tags/base.rb index aef1572290..10dec66b4f 100644 --- a/actionpack/lib/action_view/helpers/tags/base.rb +++ b/actionpack/lib/action_view/helpers/tags/base.rb @@ -73,27 +73,26 @@ module ActionView def add_default_name_and_id(options) if options.has_key?("index") - options["name"] ||= options.fetch("name"){ tag_name_with_index(options["index"]) } + options["name"] ||= options.fetch("name"){ tag_name_with_index(options["index"], options["multiple"]) } options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) } options.delete("index") elsif defined?(@auto_index) - options["name"] ||= options.fetch("name"){ tag_name_with_index(@auto_index) } + options["name"] ||= options.fetch("name"){ tag_name_with_index(@auto_index, options["multiple"]) } options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) } else - options["name"] ||= options.fetch("name"){ tag_name } + options["name"] ||= options.fetch("name"){ tag_name(options["multiple"]) } options["id"] = options.fetch("id"){ tag_id } end - options["name"] += "[]" if options["multiple"] && !options["name"].ends_with?("[]") options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence end - def tag_name - "#{@object_name}[#{sanitized_method_name}]" + def tag_name(multiple = false) + "#{@object_name}[#{sanitized_method_name}]#{"[]" if multiple}" end - def tag_name_with_index(index) - "#{@object_name}[#{index}][#{sanitized_method_name}]" + def tag_name_with_index(index, multiple = false) + "#{@object_name}[#{index}][#{sanitized_method_name}]#{"[]" if multiple}" end def tag_id -- cgit v1.2.3