Skip to content

Commit

Permalink
systematic revision of =~ usage in AV
Browse files Browse the repository at this point in the history
Where appropriate, prefer the more concise Regexp#match?,
String#include?, String#start_with?, or String#end_with?
  • Loading branch information
fxn committed Jul 24, 2016
1 parent 11b463d commit 7ebef56
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 28 deletions.
9 changes: 5 additions & 4 deletions actionview/lib/action_view/helpers/asset_tag_helper.rb
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -213,8 +214,8 @@ def image_tag(source, options={})

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]
Expand Down Expand Up @@ -322,9 +323,9 @@ def multiple_sources_tag(type, sources)

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
Expand Down
23 changes: 13 additions & 10 deletions actionview/lib/action_view/helpers/asset_url_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'zlib'
require 'active_support/core_ext/regexp'

module ActionView
# = Action View Asset URL Helpers
Expand Down Expand Up @@ -131,8 +132,8 @@ def asset_path(source, options = {})
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)

Expand Down Expand Up @@ -213,19 +214,21 @@ def compute_asset_host(source = "", options = {})
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)
Expand Down
3 changes: 2 additions & 1 deletion actionview/lib/action_view/helpers/translation_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -133,7 +134,7 @@ def scope_key_by_partial(key)
end

def html_safe_translation_key?(key)
key.to_s =~ /(\b|_|\.)html$/
/(\b|_|\.)html$/.match?(key.to_s)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion actionview/lib/action_view/helpers/url_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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
Expand Down Expand Up @@ -550,7 +551,7 @@ def current_page?(options)

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
Expand Down
7 changes: 4 additions & 3 deletions actionview/lib/action_view/layouts.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "action_view/rendering"
require "active_support/core_ext/module/remove_method"
require 'action_view/rendering'
require 'active_support/core_ext/module/remove_method'
require 'active_support/core_ext/regexp'

module ActionView
# Layouts reverse the common pattern of including shared headers and footers in many templates to isolate changes in
Expand Down Expand Up @@ -279,7 +280,7 @@ def layout(layout, conditions = {})
def _write_layout_method # :nodoc:
remove_possible_method(:_layout)

prefixes = _implied_layout_name =~ /\blayouts/ ? [] : ["layouts"]
prefixes = /\blayouts/.match?(_implied_layout_name) ? [] : ["layouts"]
default_behavior = "lookup_context.find_all('#{_implied_layout_name}', #{prefixes.inspect}, false, [], { formats: formats }).first || super"
name_clause = if name
default_behavior
Expand Down
5 changes: 3 additions & 2 deletions actionview/lib/action_view/renderer/partial_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'action_view/renderer/partial_renderer/collection_caching'
require 'concurrent/map'
require 'active_support/core_ext/regexp'
require 'action_view/renderer/partial_renderer/collection_caching'

module ActionView
class PartialIteration
Expand Down Expand Up @@ -386,7 +387,7 @@ def setup(context, options, block)
end

if as = options[:as]
raise_invalid_option_as(as) unless as.to_s =~ /\A[a-z_]\w*\z/
raise_invalid_option_as(as) unless /\A[a-z_]\w*\z/.match?(as.to_s)
as = as.to_sym
end

Expand Down
2 changes: 1 addition & 1 deletion actionview/lib/action_view/renderer/template_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def resolve_layout(layout, keys, formats)
case layout
when String
begin
if layout =~ /^\//
if layout.start_with?('/')
with_fallbacks { find_template(layout, nil, false, [], details) }
else
find_template(layout, nil, false, [], details)
Expand Down
3 changes: 2 additions & 1 deletion actionview/lib/action_view/template/error.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "active_support/core_ext/enumerable"
require 'active_support/core_ext/regexp'

module ActionView
# = Action View Errors
Expand Down Expand Up @@ -35,7 +36,7 @@ def initialize(paths, path, prefixes, partial, details, *)
prefixes = Array(prefixes)
template_type = if partial
"partial"
elsif path =~ /layouts/i
elsif /layouts/i.match?(path)
'layout'
else
'template'
Expand Down
5 changes: 3 additions & 2 deletions actionview/lib/action_view/template/handlers/erb.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'erubis'
require 'active_support/core_ext/regexp'

module ActionView
class Template
Expand Down Expand Up @@ -39,7 +40,7 @@ def add_expr(src, code, indicator)

def add_expr_literal(src, code)
flush_newline_if_pending(src)
if code =~ BLOCK_EXPR
if BLOCK_EXPR.match?(code)
src << '@output_buffer.append= ' << code
else
src << '@output_buffer.append=(' << code << ');'
Expand All @@ -48,7 +49,7 @@ def add_expr_literal(src, code)

def add_expr_escaped(src, code)
flush_newline_if_pending(src)
if code =~ BLOCK_EXPR
if BLOCK_EXPR.match?(code)
src << "@output_buffer.safe_expr_append= " << code
else
src << "@output_buffer.safe_expr_append=(" << code << ");"
Expand Down
4 changes: 2 additions & 2 deletions actionview/lib/action_view/testing/resolvers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'active_support/core_ext/regexp'
require 'action_view/template/resolver'

module ActionView #:nodoc:
Expand Down Expand Up @@ -29,7 +30,7 @@ def query(path, exts, formats, _)
templates = []
@hash.each do |_path, array|
source, updated_at = array
next unless _path =~ query
next unless query.match?(_path)
handler, format, variant = extract_handler_and_format_and_variant(_path, formats)
templates << Template.new(source, _path, handler,
:virtual_path => path.virtual,
Expand All @@ -50,4 +51,3 @@ def query(path, exts, formats, _)
end
end
end

3 changes: 2 additions & 1 deletion actionview/test/actionpack/controller/layout_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'abstract_unit'
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/regexp'

# The view_paths array must be set on Base and not LayoutTest so that LayoutTest's inherited
# method has access to the view_paths array when looking for a layout to automatically assign.
Expand Down Expand Up @@ -252,7 +253,7 @@ def test_layout_status_is_rendered
end
end

unless RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
unless /mswin|mingw/.match?(RbConfig::CONFIG['host_os'])
class LayoutSymlinkedTest < LayoutTest
layout "symlinked/symlinked_layout"
end
Expand Down

0 comments on commit 7ebef56

Please sign in to comment.