aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionview/lib/action_view/helpers/url_helper.rb6
-rw-r--r--test.rb110
2 files changed, 113 insertions, 3 deletions
diff --git a/actionview/lib/action_view/helpers/url_helper.rb b/actionview/lib/action_view/helpers/url_helper.rb
index 4e436433c4..71fed39351 100644
--- a/actionview/lib/action_view/helpers/url_helper.rb
+++ b/actionview/lib/action_view/helpers/url_helper.rb
@@ -590,10 +590,10 @@ module ActionView
def add_method_to_attributes!(html_options, method)
if method && method.to_s.downcase != "get" && html_options["rel"] !~ /nofollow/
- if html_options.key?("rel")
- html_options["rel"] = "#{html_options["rel"]} nofollow".lstrip
- else
+ if html_options.("rel").blank?
html_options["rel"] = "nofollow"
+ else
+ html_options["rel"] = "#{html_options["rel"]} nofollow"
end
end
html_options["data-method"] = method
diff --git a/test.rb b/test.rb
new file mode 100644
index 0000000000..3d5e508fa7
--- /dev/null
+++ b/test.rb
@@ -0,0 +1,110 @@
+# frozen_string_literal: true
+begin
+ require "bundler/inline"
+rescue LoadError => e
+ $stderr.puts "Bundler version 1.10 or later is required. Please update
+ your Bundler"
+ raise e
+end
+
+gemfile(true) do
+ source "https://rubygems.org"
+
+ gem "benchmark-ips"
+ gem "rails"
+end
+
+def allocate_count
+ GC.disable
+ before = ObjectSpace.count_objects
+ yield
+ after = ObjectSpace.count_objects
+ after.each { |k,v| after[k] = v - before[k] }
+ after[:T_HASH] -= 1 # probe effect - we created the before hash.
+ GC.enable
+ result = after.reject { |k,v| v == 0 }
+ GC.start
+ result
+end
+
+@hash = {}
+
+def master_version
+ "#{@hash["rel"]} nofollow".lstrip
+end
+
+def key_version
+ if @hash.key?("rel")
+ "#{@hash["rel"]} nofollow".lstrip
+ else
+ "nofollow"
+ end
+end
+
+def present_version
+ if @hash["rel"].present?
+ "#{@hash["rel"]} nofollow"
+ else
+ "nofollow".freeze
+ end
+end
+
+def nil_version
+ if @hash["rel"].nil?
+ "nofollow".freeze
+ else
+ "#{@hash["rel"]} nofollow"
+ end
+end
+
+def blank_version
+ if @hash["rel"].blank?
+ "nofollow".freeze
+ else
+ "#{@hash["rel"]} nofollow"
+ end
+end
+
+def test
+ puts "master_version"
+ puts allocate_count { 1000.times { master_version } }
+ puts "key_version"
+ puts allocate_count { 1000.times { key_version } }
+ puts "present_version"
+ puts allocate_count { 1000.times { present_version } }
+ puts "nil_version"
+ puts allocate_count { 1000.times { nil_version } }
+ puts "blank_version"
+ puts allocate_count { 1000.times { blank_version } }
+
+ Benchmark.ips do |x|
+ x.report("master_version") { master_version }
+ x.report("key_version") { key_version }
+ x.report("present_version") { present_version }
+ x.report("nil_version") { nil_version }
+ x.report("blank_version") { blank_version }
+ x.compare!
+ end
+end
+
+puts 'no rel key'
+
+test
+
+puts 'rel key with real stuff'
+
+@hash['rel'] = 'hi'.freeze
+
+test
+
+puts 'rel key with nil'
+
+@hash['rel'] = nil
+
+test
+
+puts 'rel key with ""'
+
+@hash['rel'] = ""
+
+test