From 7ebef567ce30d848dd8f9374fd70699fd91acb29 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 25 Jul 2016 00:31:35 +0200 Subject: systematic revision of =~ usage in AV Where appropriate, prefer the more concise Regexp#match?, String#include?, String#start_with?, or String#end_with? --- .../lib/action_view/helpers/asset_tag_helper.rb | 9 +++++---- .../lib/action_view/helpers/asset_url_helper.rb | 23 ++++++++++++---------- .../lib/action_view/helpers/translation_helper.rb | 3 ++- actionview/lib/action_view/helpers/url_helper.rb | 3 ++- 4 files changed, 22 insertions(+), 16 deletions(-) (limited to 'actionview/lib/action_view/helpers') diff --git a/actionview/lib/action_view/helpers/asset_tag_helper.rb b/actionview/lib/action_view/helpers/asset_tag_helper.rb index bcbb3db6a9..d7acf08b45 100644 --- a/actionview/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionview/lib/action_view/helpers/asset_tag_helper.rb @@ -1,5 +1,6 @@ require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/hash/keys' +require 'active_support/core_ext/regexp' require 'action_view/helpers/asset_url_helper' require 'action_view/helpers/tag_helper' @@ -213,8 +214,8 @@ module ActionView src = options[:src] = path_to_image(source) - unless src =~ /^(?:cid|data):/ || src.blank? - options[:alt] = options.fetch(:alt){ image_alt(src) } + unless src.start_with?('cid:') || src.start_with?('data:') || src.blank? + options[:alt] = options.fetch(:alt) { image_alt(src) } end options[:width], options[:height] = extract_dimensions(options.delete(:size)) if options[:size] @@ -322,9 +323,9 @@ module ActionView def extract_dimensions(size) size = size.to_s - if size =~ %r{\A\d+x\d+\z} + if /\A\d+x\d+\z/.match?(size) size.split('x') - elsif size =~ %r{\A\d+\z} + elsif /\A\d+\z/.match?(size) [size, size] end end diff --git a/actionview/lib/action_view/helpers/asset_url_helper.rb b/actionview/lib/action_view/helpers/asset_url_helper.rb index 717b326740..8af01617fa 100644 --- a/actionview/lib/action_view/helpers/asset_url_helper.rb +++ b/actionview/lib/action_view/helpers/asset_url_helper.rb @@ -1,4 +1,5 @@ require 'zlib' +require 'active_support/core_ext/regexp' module ActionView # = Action View Asset URL Helpers @@ -131,8 +132,8 @@ module ActionView raise ArgumentError, "nil is not a valid asset source" if source.nil? source = source.to_s - return "" unless source.present? - return source if source =~ URI_REGEXP + return '' if source.blank? + return source if URI_REGEXP.match?(source) tail, source = source[/([\?#].+)$/], source.sub(/([\?#].+)$/, ''.freeze) @@ -213,19 +214,21 @@ module ActionView host = options[:host] host ||= config.asset_host if defined? config.asset_host - if host.respond_to?(:call) - arity = host.respond_to?(:arity) ? host.arity : host.method(:call).arity - args = [source] - args << request if request && (arity > 1 || arity < 0) - host = host.call(*args) - elsif host =~ /%d/ - host = host % (Zlib.crc32(source) % 4) + if host + if host.respond_to?(:call) + arity = host.respond_to?(:arity) ? host.arity : host.method(:call).arity + args = [source] + args << request if request && (arity > 1 || arity < 0) + host = host.call(*args) + elsif host.include?('%d') + host = host % (Zlib.crc32(source) % 4) + end end host ||= request.base_url if request && options[:protocol] == :request return unless host - if host =~ URI_REGEXP + if URI_REGEXP.match?(host) host else protocol = options[:protocol] || config.default_asset_host_protocol || (request ? :request : :relative) diff --git a/actionview/lib/action_view/helpers/translation_helper.rb b/actionview/lib/action_view/helpers/translation_helper.rb index 152e1b1211..622bb193ae 100644 --- a/actionview/lib/action_view/helpers/translation_helper.rb +++ b/actionview/lib/action_view/helpers/translation_helper.rb @@ -1,5 +1,6 @@ require 'action_view/helpers/tag_helper' require 'active_support/core_ext/string/access' +require 'active_support/core_ext/regexp' require 'i18n/exceptions' module ActionView @@ -133,7 +134,7 @@ module ActionView end def html_safe_translation_key?(key) - key.to_s =~ /(\b|_|\.)html$/ + /(\b|_|\.)html$/.match?(key.to_s) end end end diff --git a/actionview/lib/action_view/helpers/url_helper.rb b/actionview/lib/action_view/helpers/url_helper.rb index fb6426b997..5d7940a7b1 100644 --- a/actionview/lib/action_view/helpers/url_helper.rb +++ b/actionview/lib/action_view/helpers/url_helper.rb @@ -2,6 +2,7 @@ require 'action_view/helpers/javascript_helper' require 'active_support/core_ext/array/access' require 'active_support/core_ext/hash/keys' require 'active_support/core_ext/string/output_safety' +require 'active_support/core_ext/regexp' module ActionView # = Action View URL Helpers @@ -550,7 +551,7 @@ module ActionView url_string.chomp!("/") if url_string.start_with?("/") && url_string != "/" - if url_string =~ /^\w+:\/\// + if %r{^\w+://}.match?(url_string) url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}" else url_string == request_uri -- cgit v1.2.3