Skip to content

Commit 96f469d

Browse files
authored
Merge pull request #1257 from Earlopain/change-add-more-load-hooks
Add more load hooks to `Rails/ActiveSupportOnLoad`
2 parents 77bbcf6 + fa73627 commit 96f469d

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1257](https://github.com/rubocop/rubocop-rails/pull/1257): Add Rails 7.1 load hooks and `active_record_sqlite3adapter` to `Rails/ActiveSupportOnLoad`. ([@earlopain][])

config/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Rails/ActiveSupportOnLoad:
165165
- 'https://guides.rubyonrails.org/engines.html#available-load-hooks'
166166
SafeAutoCorrect: false
167167
VersionAdded: '2.16'
168+
VersionChanged: <<next>>
168169

169170
Rails/AddColumnIndex:
170171
Description: >-

lib/rubocop/cop/rails/active_support_on_load.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,35 @@ class ActiveSupportOnLoad < Base
5555
'ActiveSupport::TestCase' => 'active_support_test_case'
5656
}.freeze
5757

58+
RAILS_5_2_LOAD_HOOKS = {
59+
'ActiveRecord::ConnectionAdapters::SQLite3Adapter' => 'active_record_sqlite3adapter'
60+
}.freeze
61+
62+
RAILS_7_1_LOAD_HOOKS = {
63+
'ActiveRecord::TestFixtures' => 'active_record_fixtures',
64+
'ActiveModel::Model' => 'active_model',
65+
'ActionText::EncryptedRichText' => 'action_text_encrypted_rich_text',
66+
'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter' => 'active_record_postgresqladapter',
67+
'ActiveRecord::ConnectionAdapters::Mysql2Adapter' => 'active_record_mysql2adapter',
68+
'ActiveRecord::ConnectionAdapters::TrilogyAdapter' => 'active_record_trilogyadapter'
69+
}.freeze
70+
5871
def on_send(node)
5972
receiver, method, arguments = *node # rubocop:disable InternalAffairs/NodeDestructuring
60-
return unless arguments && (hook = LOAD_HOOKS[receiver&.const_name])
73+
return unless arguments && (hook = hook_for_const(receiver&.const_name))
6174

6275
preferred = "ActiveSupport.on_load(:#{hook}) { #{method} #{arguments.source} }"
6376
add_offense(node, message: format(MSG, prefer: preferred, current: node.source)) do |corrector|
6477
corrector.replace(node, preferred)
6578
end
6679
end
80+
81+
def hook_for_const(const_name)
82+
hook = LOAD_HOOKS[const_name]
83+
hook ||= RAILS_5_2_LOAD_HOOKS[const_name] if target_rails_version >= 5.2
84+
hook ||= RAILS_7_1_LOAD_HOOKS[const_name] if target_rails_version >= 7.1
85+
hook
86+
end
6787
end
6888
end
6989
end

spec/rubocop/cop/rails/active_support_on_load_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,47 @@
9999
MyClass1.prepend(MyClass)
100100
RUBY
101101
end
102+
103+
context 'Rails 5.2', :rails52 do
104+
it 'registers an offense for a Rails 5.2 load hook' do
105+
expect_offense(<<~RUBY)
106+
ActiveRecord::ConnectionAdapters::SQLite3Adapter.include(MyClass)
107+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record_sqlite3adapter) { include MyClass }` [...]
108+
RUBY
109+
110+
expect_correction(<<~RUBY)
111+
ActiveSupport.on_load(:active_record_sqlite3adapter) { include MyClass }
112+
RUBY
113+
end
114+
115+
it 'registers no offense for a Rails 7.1 load hook' do
116+
expect_no_offenses(<<~RUBY)
117+
ActiveRecord::TestFixtures.include(MyClass)
118+
RUBY
119+
end
120+
end
121+
122+
context 'Rails 7.1', :rails71 do
123+
it 'registers an offense for a Rails 5.2 load hook' do
124+
expect_offense(<<~RUBY)
125+
ActiveRecord::ConnectionAdapters::SQLite3Adapter.include(MyClass)
126+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record_sqlite3adapter) { include MyClass }` [...]
127+
RUBY
128+
129+
expect_correction(<<~RUBY)
130+
ActiveSupport.on_load(:active_record_sqlite3adapter) { include MyClass }
131+
RUBY
132+
end
133+
134+
it 'registers an offense for a Rails 7.1 load hook' do
135+
expect_offense(<<~RUBY)
136+
ActiveRecord::TestFixtures.include(MyClass)
137+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record_fixtures) { include MyClass }` [...]
138+
RUBY
139+
140+
expect_correction(<<~RUBY)
141+
ActiveSupport.on_load(:active_record_fixtures) { include MyClass }
142+
RUBY
143+
end
144+
end
102145
end

0 commit comments

Comments
 (0)