Class: RuboCop::Cop::ThreadSafety::ActiveSupportCallbacks

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/thread_safety/active_support_callbacks.rb

Overview

Avoid mutating ActiveSupport callback chains at runtime.

Calls such as User.skip_callback and User.set_callback mutate callback chains at process scope.

Examples:

# bad
Site.skip_callback(:commit, :after, :after_owner_change)

# bad
Site.set_callback(
  :commit, :after, :after_owner_change,
  if: :saved_change_to_owner?
)

# good
class User < ApplicationRecord
  skip_callback :commit, :after, :after_owner_change
end

Constant Summary collapse

MSG =
'Avoid process-wide ActiveSupport callback mutation with `%<expression>s`.'
RESTRICT_ON_SEND =
i[set_callback skip_callback].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



31
32
33
34
35
# File 'lib/rubocop/cop/thread_safety/active_support_callbacks.rb', line 31

def on_send(node)
  return unless constant_receiver?(node.receiver)

  add_offense(node.loc.selector, message: format(MSG, expression: callback_call(node)))
end