systematic revision of =~ usage in AR

Where appropriatei, prefer the more concise Regexp#match?,
String#include?, String#start_with?, or String#end_with?
This commit is contained in:
Xavier Noria 2016-07-23 20:01:56 +02:00
parent 1ca571cc47
commit 99cf755800
21 changed files with 34 additions and 29 deletions

View File

@ -51,7 +51,7 @@ end
adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z0-9]+/]
t.libs << 'test'
t.test_files = (Dir.glob( "test/cases/**/*_test.rb" ).reject {
|x| x =~ /\/adapters\//
|x| x.include?('/adapters/')
} + Dir.glob("test/cases/adapters/#{adapter_short}/**/*_test.rb"))
t.warning = true
@ -64,7 +64,7 @@ end
adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z0-9]+/]
puts [adapter, adapter_short].inspect
(Dir["test/cases/**/*_test.rb"].reject {
|x| x =~ /\/adapters\//
|x| x.include?('/adapters/')
} + Dir["test/cases/adapters/#{adapter_short}/**/*_test.rb"]).all? do |file|
sh(Gem.ruby, '-w' ,"-Itest", file)
end or raise "Failures"

View File

@ -20,7 +20,7 @@ module ActiveRecord
def load(yaml)
return object_class.new if object_class != Object && yaml.nil?
return yaml unless yaml.is_a?(String) && yaml =~ /^---/
return yaml unless yaml.is_a?(String) && /^---/.match?(yaml)
obj = YAML.load(yaml)
assert_valid_value(obj)

View File

@ -85,7 +85,7 @@ module ActiveRecord
end
def mariadb? # :nodoc:
full_version =~ /mariadb/i
/mariadb/i.match?(full_version)
end
# Returns true, since this connection adapter supports migrations.

View File

@ -58,7 +58,7 @@ module ActiveRecord
def quote_default_expression(value, column) # :nodoc:
if value.is_a?(Proc)
value.call
elsif column.type == :uuid && value =~ /\(\)/
elsif column.type == :uuid && value.include?('()')
value # Does not quote function default values for UUID columns
elsif column.respond_to?(:array?)
value = type_cast_from_column(column, value)

View File

@ -548,7 +548,7 @@ module ActiveRecord
columns_string.split(',').each do |column_string|
# This regex will match the column name and collation type and will save
# the value in $1 and $2 respectively.
collation_hash[$1] = $2 if (COLLATE_REGEX =~ column_string)
collation_hash[$1] = $2 if COLLATE_REGEX =~ column_string
end
basic_structure.map! do |column|

View File

@ -29,7 +29,7 @@ module ActiveRecord
attr_reader :matchers
def match(model, name)
klass = matchers.find { |k| name =~ k.pattern }
klass = matchers.find { |k| k.pattern.match?(name) }
klass.new(model, name) if klass
end

View File

@ -1057,7 +1057,7 @@ module ActiveRecord
end
def match_to_migration_filename?(filename) # :nodoc:
File.basename(filename) =~ Migration::MigrationFilenameRegexp
Migration::MigrationFilenameRegexp.match?(File.basename(filename))
end
def parse_migration_filename(filename) # :nodoc:

View File

@ -309,7 +309,12 @@ module ActiveRecord
# Returns an array of column objects where the primary id, all columns ending in "_id" or "_count",
# and columns used for single table inheritance have been removed.
def content_columns
@content_columns ||= columns.reject { |c| c.name == primary_key || c.name =~ /(_id|_count)$/ || c.name == inheritance_column }
@content_columns ||= columns.reject do |c|
c.name == primary_key ||
c.name == inheritance_column ||
c.name.end_with?('_id') ||
c.name.end_with?('_count')
end
end
# Resets all the cached information about columns, which will cause them

View File

@ -58,7 +58,7 @@ module ActiveRecord
@delegation_mutex.synchronize do
return if method_defined?(method)
if method.to_s =~ /\A[a-zA-Z_]\w*[!?]?\z/
if /\A[a-zA-Z_]\w*[!?]?\z/.match?(method)
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(*args, &block)
scoping { @klass.#{method}(*args, &block) }

View File

@ -1135,10 +1135,10 @@ module ActiveRecord
end
def does_not_support_reverse?(order)
#uses sql function with multiple arguments
order =~ /\([^()]*,[^()]*\)/ ||
# uses "nulls first" like construction
order =~ /nulls (first|last)\Z/i
# Uses SQL function with multiple arguments.
/\([^()]*,[^()]*\)/.match?(order) ||
# Uses "nulls first" like construction.
/nulls (first|last)\Z/i.match?(order)
end
def build_order(arel)

View File

@ -153,7 +153,7 @@ module ActiveRecord
# # => "name='foo''bar' and group_id='4'"
def sanitize_sql_array(ary)
statement, *values = ary
if values.first.is_a?(Hash) && statement =~ /:\w+/
if values.first.is_a?(Hash) && /:\w+/.match?(statement)
replace_named_bind_variables(statement, values.first)
elsif statement.include?('?')
replace_bind_variables(statement, values)

View File

@ -90,7 +90,7 @@ module ActiveRecord
end
def error_class
if configuration['adapter'] =~ /jdbc/
if configuration['adapter'].include?('jdbc')
require 'active_record/railties/jdbcmysql_error'
ArJdbcMySQL::Error
elsif defined?(Mysql2)

View File

@ -60,7 +60,7 @@ module ActiveRecord
# A migration file name can only contain underscores (_), lowercase characters,
# and numbers 0-9. Any other file name will raise an IllegalMigrationNameError.
def validate_file_name!
unless file_name =~ /^[_a-z0-9]+$/
unless /^[_a-z0-9]+$/.match?(file_name)
raise IllegalMigrationNameError.new(file_name)
end
end

View File

@ -86,7 +86,7 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase
end
def test_calculate_honors_implicit_inner_joins_and_distinct_and_conditions
real_count = Author.all.to_a.select {|a| a.posts.any? {|p| p.title =~ /^Welcome/} }.length
real_count = Author.all.to_a.select {|a| a.posts.any? {|p| p.title.start_with?('Welcome')} }.length
authors_with_welcoming_post_titles = Author.all.merge!(joins: :posts, where: "posts.title like 'Welcome%'").distinct.calculate(:count, 'authors.id')
assert_equal real_count, authors_with_welcoming_post_titles, "inner join and conditions should have only returned authors posting titles starting with 'Welcome'"
end

View File

@ -20,7 +20,7 @@ class LeftOuterJoinAssociationTest < ActiveRecord::TestCase
Person.left_outer_joins(:agents => {:agents => :agents})
.left_outer_joins(:agents => {:agents => {:primary_contact => :agents}}).to_a
end
assert queries.any? { |sql| /agents_people_4/i =~ sql }
assert queries.any? { |sql| /agents_people_4/i.match?(sql) }
end
end
@ -36,12 +36,12 @@ class LeftOuterJoinAssociationTest < ActiveRecord::TestCase
def test_construct_finder_sql_ignores_empty_left_outer_joins_hash
queries = capture_sql { Author.left_outer_joins({}) }
assert queries.none? { |sql| /LEFT OUTER JOIN/i =~ sql }
assert queries.none? { |sql| /LEFT OUTER JOIN/i.match?(sql) }
end
def test_construct_finder_sql_ignores_empty_left_outer_joins_array
queries = capture_sql { Author.left_outer_joins([]) }
assert queries.none? { |sql| /LEFT OUTER JOIN/i =~ sql }
assert queries.none? { |sql| /LEFT OUTER JOIN/i.match?(sql) }
end
def test_left_outer_joins_forbids_to_use_string_as_argument
@ -50,8 +50,8 @@ class LeftOuterJoinAssociationTest < ActiveRecord::TestCase
def test_join_conditions_added_to_join_clause
queries = capture_sql { Author.left_outer_joins(:essays).to_a }
assert queries.any? { |sql| /writer_type.*?=.*?(Author|\?|\$1|\:a1)/i =~ sql }
assert queries.none? { |sql| /WHERE/i =~ sql }
assert queries.any? { |sql| /writer_type.*?=.*?(Author|\?|\$1|\:a1)/i.match?(sql) }
assert queries.none? { |sql| /WHERE/i.match?(sql) }
end
def test_find_with_sti_join

View File

@ -197,7 +197,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
if current_adapter?(:Mysql2Adapter)
def test_read_attributes_before_type_cast_on_boolean
bool = Boolean.create!({ "value" => false })
if RUBY_PLATFORM =~ /java/
if RUBY_PLATFORM.include?('java')
# JRuby will return the value before typecast as string
assert_equal "0", bool.reload.attributes_before_type_cast["value"]
else

View File

@ -31,7 +31,7 @@ class CallbackDeveloper < ActiveRecord::Base
end
ActiveRecord::Callbacks::CALLBACKS.each do |callback_method|
next if callback_method.to_s =~ /^around_/
next if callback_method.to_s.start_with?('around_')
define_callback_method(callback_method)
ActiveSupport::Deprecation.silence { send(callback_method, callback_string(callback_method)) }
send(callback_method, callback_proc(callback_method))

View File

@ -604,7 +604,7 @@ class DirtyTest < ActiveRecord::TestCase
jon = Person.create! first_name: 'Jon'
end
assert ActiveRecord::SQLCounter.log_all.none? { |sql| sql =~ /followers_count/ }
assert ActiveRecord::SQLCounter.log_all.none? { |sql| sql.include?('followers_count') }
jon.reload
assert_equal 'Jon', jon.first_name

View File

@ -201,7 +201,7 @@ class DefaultScopingTest < ActiveRecord::TestCase
def test_order_to_unscope_reordering
scope = DeveloperOrderedBySalary.order('salary DESC, name ASC').reverse_order.unscope(:order)
assert !(scope.to_sql =~ /order/i)
assert !/order/i.match?(scope.to_sql)
end
def test_unscope_reverse_order

View File

@ -115,7 +115,7 @@ module ActiveRecord
return if 'CACHE' == values[:name]
self.class.log_all << sql
self.class.log << sql unless ignore =~ sql
self.class.log << sql unless ignore.match?(sql)
end
end

View File

@ -88,7 +88,7 @@ _SQL
FOR EACH ROW EXECUTE PROCEDURE partitioned_insert_trigger();
_SQL
rescue ActiveRecord::StatementInvalid => e
if e.message =~ /language "plpgsql" does not exist/
if e.message.include?('language "plpgsql" does not exist')
execute "CREATE LANGUAGE 'plpgsql';"
retry
else