Commit 8d69183b authored by Robert May's avatar Robert May

Relocate method to utils

parent c99f7bd3
......@@ -44,7 +44,7 @@ module ApplicationHelper
# current_controller?('gitlab/application') # => false
def current_controller?(*args)
args.any? do |v|
safe_downcase!(v.to_s) == controller.controller_name || safe_downcase!(v.to_s) == controller.controller_path
Gitlab::Utils.safe_downcase!(v.to_s) == controller.controller_name || Gitlab::Utils.safe_downcase!(v.to_s) == controller.controller_path
end
end
......@@ -59,7 +59,7 @@ module ApplicationHelper
# current_action?(:create) # => false
# current_action?(:new, :create) # => true
def current_action?(*args)
args.any? { |v| safe_downcase!(v.to_s) == action_name }
args.any? { |v| Gitlab::Utils.safe_downcase!(v.to_s) == action_name }
end
def admin_section?
......@@ -397,19 +397,6 @@ module ApplicationHelper
end
end
# A safe alternative to String#downcase!
#
# This will make copies of frozen strings but downcase unfrozen
# strings in place, reducing allocations.
def safe_downcase!(str)
if str.frozen?
str.downcase
else
str.downcase! || str
end
end
private
def appearance
......
......@@ -174,6 +174,18 @@ module Gitlab
rescue IPAddr::InvalidAddressError
end
# A safe alternative to String#downcase!
#
# This will make copies of frozen strings but downcase unfrozen
# strings in place, reducing allocations.
def safe_downcase!(str)
if str.frozen?
str.downcase
else
str.downcase! || str
end
end
# Converts a string to an Addressable::URI object.
# If the string is not a valid URI, it returns nil.
# Param uri_string should be a String object.
......
......@@ -392,6 +392,23 @@ RSpec.describe Gitlab::Utils do
end
end
describe ".safe_downcase!" do
using RSpec::Parameterized::TableSyntax
where(:str, :result) do
"test".freeze | "test"
"Test".freeze | "test"
"test" | "test"
"Test" | "test"
end
with_them do
it "downcases the string" do
expect(described_class.safe_downcase!(str)).to eq(result)
end
end
end
describe '.parse_url' do
it 'returns Addressable::URI object' do
expect(described_class.parse_url('http://gitlab.com')).to be_instance_of(Addressable::URI)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment