Skip to content

Commit ef636d2

Browse files
authored
Rubocop fixes (#21)
* Update Rubocop and fix some rules * Rubocop updates
1 parent 07d2d60 commit ef636d2

File tree

10 files changed

+136
-140
lines changed

10 files changed

+136
-140
lines changed

.rubocop.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
inherit_from: .rubocop_todo.yml
22

33
AllCops:
4+
NewCops: enable
45
DisplayCopNames: true
56
DisplayStyleGuide: true
67
TargetRubyVersion: 3.3
@@ -22,9 +23,6 @@ Metrics/CyclomaticComplexity:
2223
Metrics/MethodLength:
2324
Enabled: false
2425

25-
Layout/LineLength:
26-
Enabled: false
27-
2826
Metrics/PerceivedComplexity:
2927
Enabled: false
3028

@@ -38,3 +36,12 @@ Style/PercentLiteralDelimiters:
3836
PreferredDelimiters:
3937
default: '[]'
4038
'%r': '{}'
39+
40+
Layout/HashAlignment:
41+
EnforcedColonStyle: table
42+
43+
Layout/FirstArrayElementIndentation:
44+
EnforcedStyle: consistent
45+
46+
Style/TrailingCommaInArguments:
47+
EnforcedStyleForMultiline: comma

.rubocop_todo.yml

Lines changed: 51 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,86 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2022-03-14 22:30:45 UTC using RuboCop version 1.26.0.
3+
# on 2025-04-15 19:53:18 UTC using RuboCop version 1.75.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
10-
# Configuration parameters: Include.
11-
# Include: **/*.gemspec
12-
Gemspec/RequiredRubyVersion:
9+
# Offense count: 3
10+
# Configuration parameters: EnforcedStyle, AllowedGems, Include.
11+
# SupportedStyles: Gemfile, gems.rb, gemspec
12+
# Include: **/*.gemspec, **/Gemfile, **/gems.rb
13+
Gemspec/DevelopmentDependencies:
1314
Exclude:
1415
- 'odbc_adapter.gemspec'
1516

16-
# Offense count: 6
17-
# This cop supports safe auto-correction (--auto-correct).
18-
Layout/EmptyLineAfterGuardClause:
19-
Exclude:
20-
- 'lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb'
21-
- 'lib/odbc_adapter/column_metadata.rb'
22-
- 'lib/odbc_adapter/quoting.rb'
23-
- 'lib/odbc_adapter/registry.rb'
24-
- 'lib/odbc_adapter/schema_statements.rb'
25-
26-
# Offense count: 16
27-
# This cop supports safe auto-correction (--auto-correct).
28-
# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle.
29-
# SupportedHashRocketStyles: key, separator, table
30-
# SupportedColonStyles: key, separator, table
31-
# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit
32-
Layout/HashAlignment:
17+
# Offense count: 1
18+
# This cop supports safe autocorrection (--autocorrect).
19+
# Configuration parameters: Severity, Include.
20+
# Include: **/*.gemspec
21+
Gemspec/RequireMFA:
3322
Exclude:
34-
- 'lib/odbc_adapter/column_metadata.rb'
35-
- 'lib/odbc_adapter/registry.rb'
36-
- 'lib/odbc_adapter/schema_statements.rb'
23+
- 'odbc_adapter.gemspec'
3724

3825
# Offense count: 1
39-
# This cop supports safe auto-correction (--auto-correct).
40-
# Configuration parameters: AllowInHeredoc.
41-
Layout/TrailingWhitespace:
26+
# This cop supports safe autocorrection (--autocorrect).
27+
Lint/AmbiguousOperatorPrecedence:
4228
Exclude:
43-
- 'lib/odbc_adapter/adapters/null_odbc_adapter.rb'
29+
- 'lib/odbc_adapter/registry.rb'
4430

4531
# Offense count: 1
46-
Lint/MissingSuper:
47-
Exclude:
48-
- 'test/registry_test.rb'
32+
# Configuration parameters: CountComments, CountAsOne.
33+
Metrics/ModuleLength:
34+
Max: 102
4935

5036
# Offense count: 2
5137
# Configuration parameters: Max, CountKeywordArgs.
5238
Metrics/ParameterLists:
5339
MaxOptionalParameters: 4
5440

55-
# Offense count: 35
56-
# This cop supports safe auto-correction (--auto-correct).
41+
# Offense count: 6
42+
# This cop supports safe autocorrection (--autocorrect).
43+
# Configuration parameters: EnforcedStyle, BlockForwardingName.
44+
# SupportedStyles: anonymous, explicit
45+
Naming/BlockForwarding:
46+
Exclude:
47+
- 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb'
48+
- 'lib/odbc_adapter/registry.rb'
49+
- 'test/crud_test.rb'
50+
51+
# Offense count: 4
52+
# This cop supports safe autocorrection (--autocorrect).
53+
# Configuration parameters: AllowOnlyRestArgument, UseAnonymousForwarding, RedundantRestArgumentNames, RedundantKeywordRestArgumentNames, RedundantBlockArgumentNames.
54+
# RedundantRestArgumentNames: args, arguments
55+
# RedundantKeywordRestArgumentNames: kwargs, options, opts
56+
# RedundantBlockArgumentNames: blk, block, proc
57+
Style/ArgumentsForwarding:
58+
Exclude:
59+
- 'lib/odbc_adapter/registry.rb'
60+
61+
# Offense count: 32
62+
# This cop supports unsafe autocorrection (--autocorrect-all).
5763
# Configuration parameters: EnforcedStyle.
5864
# SupportedStyles: always, always_true, never
5965
Style/FrozenStringLiteralComment:
6066
Enabled: false
6167

62-
# Offense count: 5
63-
# This cop supports safe auto-correction (--auto-correct).
64-
Style/IfUnlessModifier:
65-
Exclude:
66-
- 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb'
67-
- 'lib/odbc_adapter/quoting.rb'
68-
- 'lib/odbc_adapter/schema_statements.rb'
69-
7068
# Offense count: 1
71-
# This cop supports safe auto-correction (--auto-correct).
72-
# Configuration parameters: EnforcedStyle.
73-
# SupportedStyles: literals, strict
74-
Style/MutableConstant:
69+
# This cop supports unsafe autocorrection (--autocorrect-all).
70+
# Configuration parameters: AllowSplatArgument.
71+
Style/HashConversion:
7572
Exclude:
76-
- 'lib/active_record/connection_adapters/odbc_adapter.rb'
73+
- 'lib/odbc_adapter/database_metadata.rb'
7774

7875
# Offense count: 1
79-
# Configuration parameters: AllowedMethods.
80-
# AllowedMethods: respond_to_missing?
81-
Style/OptionalBooleanParameter:
76+
# This cop supports unsafe autocorrection (--autocorrect-all).
77+
Style/MapToHash:
8278
Exclude:
83-
- 'lib/odbc_adapter/column.rb'
79+
- 'lib/active_record/connection_adapters/odbc_adapter.rb'
8480

8581
# Offense count: 1
86-
# This cop supports safe auto-correction (--auto-correct).
87-
Style/RedundantRegexpEscape:
82+
# This cop supports unsafe autocorrection (--autocorrect-all).
83+
# Configuration parameters: AutoCorrect, AllowComments.
84+
Style/RedundantInitialize:
8885
Exclude:
89-
- 'lib/odbc_adapter/quoting.rb'
90-
91-
# Offense count: 4
92-
# This cop supports safe auto-correction (--auto-correct).
93-
# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods.
94-
# AllowedMethods: present?, blank?, presence, try, try!
95-
Style/SafeNavigation:
96-
Exclude:
97-
- 'lib/odbc_adapter/column_metadata.rb'
98-
- 'lib/odbc_adapter/schema_statements.rb'
99-
100-
# Offense count: 2
101-
# This cop supports safe auto-correction (--auto-correct).
102-
# Configuration parameters: AllowModifier.
103-
Style/SoleNestedConditional:
104-
Exclude:
105-
- 'lib/odbc_adapter/adapters/mysql_odbc_adapter.rb'
106-
- 'lib/odbc_adapter/quoting.rb'
86+
- 'test/registry_test.rb'

lib/odbc_adapter/adapters/mysql_odbc_adapter.rb

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def truncate(table_name, name = nil)
3131
# Quotes a string, escaping any ' (single quote) and \ (backslash)
3232
# characters.
3333
def quote_string(string)
34-
string.gsub(/\\/, '\&\&').gsub(/'/, "''")
34+
string.gsub("\\", '\&\&').gsub("'", "''")
3535
end
3636

3737
def quoted_true
@@ -70,10 +70,11 @@ def disable_referential_integrity(&_block)
7070
# create_database 'rails_development'
7171
# create_database 'rails_development', charset: :big5
7272
def create_database(name, options = {})
73+
charset = options[:charset] || "utf8"
7374
if options[:collation]
74-
execute("CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`")
75+
execute("CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{charset}` COLLATE `#{options[:collation]}`")
7576
else
76-
execute("CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`")
77+
execute("CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{charset}`")
7778
end
7879
end
7980

@@ -95,11 +96,10 @@ def rename_table(name, new_name)
9596
end
9697

9798
def change_column(table_name, column_name, type, options = {})
98-
unless options_include_default?(options)
99-
options[:default] = column_for(table_name, column_name).default
100-
end
99+
options[:default] = column_for(table_name, column_name).default unless options_include_default?(options)
101100

102-
change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
101+
column_type = type_to_sql(type, options[:limit], options[:precision], options[:scale])
102+
change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{column_type}"
103103
add_column_options!(change_column_sql, options)
104104
execute(change_column_sql)
105105
end
@@ -114,7 +114,11 @@ def change_column_null(table_name, column_name, null, default = nil)
114114
column = column_for(table_name, column_name)
115115

116116
unless null || default.nil?
117-
execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
117+
quoted_table_name = quote_table_name(table_name)
118+
quoted_column_name = quote_column_name(column_name)
119+
execute <<~SQL
120+
UPDATE #{quoted_table_name} SET #{quoted_column_name}=#{quote(default)} WHERE #{quoted_column_name} IS NULL
121+
SQL
118122
end
119123
change_column(table_name, column_name, column.sql_type, null: null)
120124
end
@@ -128,18 +132,17 @@ def rename_column(table_name, column_name, new_column_name)
128132

129133
# Skip primary key indexes
130134
def indexes(table_name, name = nil)
131-
super(table_name, name).reject { |i| i.unique && i.name =~ /^PRIMARY$/ }
135+
super.reject { |i| i.unique && i.name =~ /^PRIMARY$/ }
132136
end
133137

134138
# MySQL 5.x doesn't allow DEFAULT NULL for first timestamp column in a
135139
# table
136-
def options_include_default?(options)
137-
if options.include?(:default) && options[:default].nil?
138-
if options.include?(:column) && options[:column].native_type =~ /timestamp/i
139-
options.delete(:default)
140-
end
140+
def options_include_default?(opts)
141+
if opts.key?(:default) && opts[:default].nil? && opts.key?(:column) && opts[:column].native_type =~ /timestamp/i
142+
opts.delete(:default)
141143
end
142-
super(options)
144+
145+
super
143146
end
144147

145148
protected

lib/odbc_adapter/adapters/null_odbc_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Adapters
44
# registry. This allows for minimal support for DBMSs for which we don't
55
# have an explicit adapter.
66
class NullODBCAdapter < ActiveRecord::ConnectionAdapters::ODBCAdapter
7-
class BindSubstitution < Arel::Visitors::ToSql
7+
class BindSubstitution < Arel::Visitors::ToSql
88
end
99

1010
# Using a BindVisitor so that the SQL string gets substituted before it is

lib/odbc_adapter/adapters/postgresql_odbc_adapter.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def type_cast(value, column)
5858
case value
5959
when String
6060
return super unless column.native_type == "bytea"
61+
6162
{ value: value, format: 1 }
6263
else
6364
super
@@ -67,7 +68,7 @@ def type_cast(value, column)
6768
# Quotes a string, escaping any ' (single quote) and \ (backslash)
6869
# characters.
6970
def quote_string(string)
70-
string.gsub(/\\/, '\&\&').gsub(/'/, "''")
71+
string.gsub("\\", '\&\&').gsub("'", "''")
7172
end
7273

7374
def disable_referential_integrity
@@ -122,7 +123,8 @@ def rename_table(name, new_name)
122123
end
123124

124125
def change_column(table_name, column_name, type, options = {})
125-
execute("ALTER TABLE #{table_name} ALTER #{column_name} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}")
126+
column_type = type_to_sql(type, options[:limit], options[:precision], options[:scale])
127+
execute("ALTER TABLE #{table_name} ALTER #{column_name} TYPE #{column_type}")
126128
change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
127129
end
128130

lib/odbc_adapter/column_metadata.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def native_database_types
2727
GENERICS.each_with_object({}) do |(abstract, candidates), mapped|
2828
candidates.detect do |candidate|
2929
next unless grouped[candidate]
30+
3031
mapped[abstract] = native_type_mapping(abstract, grouped[candidate])
3132
end
3233
end
@@ -41,6 +42,7 @@ def native_type_mapping(abstract, rows)
4142
# ODBC doesn't provide any info on a DBMS's native syntax for
4243
# autoincrement columns. So we use a lookup instead.
4344
return adapter.class::PRIMARY_KEY if abstract == :primary_key
45+
4446
selected_row = rows[0]
4547

4648
# If more than one native type corresponds to the SQL type we're
@@ -69,7 +71,7 @@ def reported_types
6971
stmt = adapter.raw_connection.types
7072
stmt.fetch_all
7173
ensure
72-
stmt.drop unless stmt.nil?
74+
stmt&.drop
7375
end
7476
end
7577
end

lib/odbc_adapter/quoting.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module ODBCAdapter
22
module Quoting
33
# Quotes a string, escaping any ' (single quote) characters.
44
def quote_string(string)
5-
string.gsub(/\'/, "''")
5+
string.gsub("'", "''")
66
end
77

88
# Returns a quoted form of the column name.
@@ -11,15 +11,14 @@ def quote_column_name(name)
1111
quote_char = database_metadata.identifier_quote_char.to_s.strip
1212

1313
return name if quote_char.empty?
14+
1415
quote_char = quote_char[0]
1516

1617
# Avoid quoting any already quoted name
1718
return name if name[0] == quote_char && name[-1] == quote_char
1819

1920
# If upcase identifiers, only quote mixed case names.
20-
if database_metadata.upcase_identifiers?
21-
return name unless name =~ /([A-Z]+[a-z])|([a-z]+[A-Z])/
22-
end
21+
return name if database_metadata.upcase_identifiers? && name !~ /([A-Z]+[a-z])|([a-z]+[A-Z])/
2322

2423
"#{quote_char.chr}#{name}#{quote_char.chr}"
2524
end
@@ -31,9 +30,7 @@ def quoted_date(value)
3130
default_tz = ActiveRecord.try(:default_timezone) || ActiveRecord::Base.default_timezone
3231
zone_conversion_method = default_tz == :utc ? :getutc : :getlocal
3332

34-
if value.respond_to?(zone_conversion_method)
35-
value = value.send(zone_conversion_method)
36-
end
33+
value = value.send(zone_conversion_method) if value.respond_to?(zone_conversion_method)
3734
value.strftime("%Y-%m-%d %H:%M:%S") # Time, DateTime
3835
else
3936
value.strftime("%Y-%m-%d") # Date

lib/odbc_adapter/registry.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Registry
44

55
def initialize
66
@dbs = {
7-
/my.*sql/i => :MySQL,
7+
/my.*sql/i => :MySQL,
88
/postgres/i => :PostgreSQL,
99
/snowflake/i => :Snowflake
1010
}
@@ -28,6 +28,7 @@ def register(pattern, superclass = Object, &block)
2828

2929
def normalize_adapter(adapter)
3030
return adapter unless adapter.is_a?(Symbol)
31+
3132
require "odbc_adapter/adapters/#{adapter.downcase}_odbc_adapter"
3233
Adapters.const_get(:"#{adapter}ODBCAdapter")
3334
end

0 commit comments

Comments
 (0)