Skip to content

Commit

Permalink
Merge pull request #1256 from Earlopain/fix-error-for-rails-active-su…
Browse files Browse the repository at this point in the history
…pport-on-load

Fix an error for `Rails/ActiveSupportOnLoad` when calling without arguments
  • Loading branch information
koic committed Mar 18, 2024
2 parents a4f5c58 + fdbba0c commit 77bbcf6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_error_for_rails_active_support_load_hook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1256](https://github.com/rubocop/rubocop-rails/pull/1256): Fix an error for `Rails/ActiveSupportOnLoad` when calling without arguments. ([@earlopain][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/active_support_on_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ActiveSupportOnLoad < Base

def on_send(node)
receiver, method, arguments = *node # rubocop:disable InternalAffairs/NodeDestructuring
return unless receiver && (hook = LOAD_HOOKS[receiver.const_name])
return unless arguments && (hook = LOAD_HOOKS[receiver&.const_name])

preferred = "ActiveSupport.on_load(:#{hook}) { #{method} #{arguments.source} }"
add_offense(node, message: format(MSG, prefer: preferred, current: node.source)) do |corrector|
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/active_support_on_load_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
RUBY
end

it 'does not add offense for include without arguments' do
expect_no_offenses(<<~RUBY)
ActiveRecord::Base.include
RUBY
end

it 'adds offense and corrects when trying to extend a framework class with prepend' do
expect_offense(<<~RUBY)
ActiveRecord::Base.prepend(MyClass)
Expand Down

0 comments on commit 77bbcf6

Please sign in to comment.