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/CHANGELOG.md | 4 ++++ actionpack/lib/action_view/helpers/url_helper.rb | 21 ++++++++++++++++----- actionpack/test/template/url_helper_test.rb | 10 ++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 798c34e87c..bb84a83208 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -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 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+. diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 88c4b72ad7..e7b04d40c0 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -538,6 +538,16 @@ class UrlHelperTest < ActiveSupport::TestCase assert mail_to("david@loudthinking.com").html_safe? end + def test_mail_to_with_block + assert_dom_equal %{Email me}, + mail_to('me@example.com') { content_tag(:span, 'Email me') } + end + + def test_link_tag_with_block_and_options + assert_dom_equal %{Email me}, + 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 -- cgit v1.2.3 From f5644b0cd692715774aa04e1fd692597ca68e5ec Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 3 Apr 2013 20:56:12 -0300 Subject: Fix typo in view name Introduced in 2c22376fe04b89e8f34620139720b85a85ce3428 --- actionpack/test/fixtures/test/change_priority.html.erb | 2 ++ actionpack/test/fixtures/test/change_priorty.html.erb | 2 -- actionpack/test/template/render_test.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 actionpack/test/fixtures/test/change_priority.html.erb delete mode 100644 actionpack/test/fixtures/test/change_priorty.html.erb (limited to 'actionpack') diff --git a/actionpack/test/fixtures/test/change_priority.html.erb b/actionpack/test/fixtures/test/change_priority.html.erb new file mode 100644 index 0000000000..5618977d05 --- /dev/null +++ b/actionpack/test/fixtures/test/change_priority.html.erb @@ -0,0 +1,2 @@ +<%= render :partial => "test/json_change_priority", formats: :json %> +HTML Template, but <%= render :partial => "test/changing_priority" %> partial \ No newline at end of file diff --git a/actionpack/test/fixtures/test/change_priorty.html.erb b/actionpack/test/fixtures/test/change_priorty.html.erb deleted file mode 100644 index 5618977d05..0000000000 --- a/actionpack/test/fixtures/test/change_priorty.html.erb +++ /dev/null @@ -1,2 +0,0 @@ -<%= render :partial => "test/json_change_priority", formats: :json %> -HTML Template, but <%= render :partial => "test/changing_priority" %> partial \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 8111e58527..2237d747be 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -61,7 +61,7 @@ module RenderTestCases def test_render_partial_use_last_prepended_format_for_partials_with_the_same_names @view.lookup_context.formats = [:html] - assert_equal "\nHTML Template, but JSON partial", @view.render(:template => "test/change_priorty") + assert_equal "\nHTML Template, but JSON partial", @view.render(:template => "test/change_priority") end def test_render_template_with_a_missing_partial_of_another_format -- 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 +++--- actionpack/test/template/url_helper_test.rb | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'actionpack') 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) diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index e7b04d40c0..9b4c419807 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -543,11 +543,17 @@ class UrlHelperTest < ActiveSupport::TestCase mail_to('me@example.com') { content_tag(:span, 'Email me') } end - def test_link_tag_with_block_and_options + def test_mail_to_with_block_and_options assert_dom_equal %{Email me}, mail_to('me@example.com', cc: "ccaddress@example.com", class: "special") { content_tag(:span, 'Email me') } end + def test_mail_to_does_not_modify_html_options_hash + options = { class: 'special' } + mail_to 'me@example.com', 'ME!', options + assert_equal({ class: 'special' }, options) + end + def protect_against_forgery? self.request_forgery end -- 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') 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 bbfddf8470fcea21f31b285f6d1ef1bb723e07e1 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 4 Apr 2013 22:37:49 -0300 Subject: Fix indent and remove extra white spaces --- .../test/template/form_options_helper_test.rb | 32 ++++++++++++---------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'actionpack') diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 1437ff7285..94ae8549f7 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -1110,15 +1110,15 @@ class FormOptionsHelperTest < ActionView::TestCase "", html end - + def test_time_zone_select_with_priority_zones_as_regexp_using_grep_finds_no_zones @firm = Firm.new("D") - + priority_zones = /A|D/ @fake_timezones.each do |tz| priority_zones.stubs(:===).with(tz).raises(Exception) end - + html = time_zone_select("firm", "time_zone", priority_zones) assert_dom_equal "" + + + html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) + assert_dom_equal "" + - "\n" + - "\n" + - "\n" + - "\n" + - "" + - "", - html + @firm = Firm.new('D') + + html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) + assert_dom_equal "", + html end def test_options_for_select_with_element_attributes -- 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/CHANGELOG.md | 7 +++++++ actionpack/lib/action_view/helpers/tags/base.rb | 15 +++++++-------- actionpack/test/template/form_helper_test.rb | 10 ++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index bb84a83208..3121167d2d 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,12 @@ ## Rails 4.0.0 (unreleased) ## +* 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 + + *Ryan McGeary* + * Add block support for the `mail_to` helper, similar to the `link_to` helper. *Sam Pohlenz* 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 diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index dff0b8bdc2..1ff320224d 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -361,6 +361,16 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, file_field("user", "avatar") end + def test_file_field_with_multiple_behavior + expected = '' + assert_dom_equal expected, file_field("import", "file", :multiple => true) + end + + def test_file_field_with_multiple_behavior_and_explicit_name + expected = '' + assert_dom_equal expected, file_field("import", "file", :multiple => true, :name => "custom") + end + def test_hidden_field assert_dom_equal( '', -- cgit v1.2.3 From 096ee1594d329f53ef47d0aff9bb6eef25a18b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 5 Apr 2013 16:43:04 -0300 Subject: Improve the changelog entry [ci skip] --- actionpack/CHANGELOG.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 3121167d2d..913edbd8df 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,9 +1,10 @@ ## Rails 4.0.0 (unreleased) ## -* Fix explicit names on multiple file fields. If a file field tag is passed +* Fix explicit names on multiple file fields. If a file field tag has 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 + but if an explicit name is passed to `file_field` the `[]` is not + appended. + Fixes #9830. *Ryan McGeary* -- cgit v1.2.3