Class: RuboCop::Cop::Obsession::Rails::NoCallbackConditions

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/obsession/rails/no_callback_conditions.rb

Overview

This cop checks for model callbacks with conditions.

Model callback with conditions should be avoided because they can quickly degenerate into multiple conditions that pollute the macro definition section, even more so if lambdas are involved. Instead, move the condition inside the callback method.

Note: conditions are allowed for ‘validates :field` callbacks, as it is sometimes not easy to translate them into `validate :validate_field` custom validation callbacks.

Examples:


# bad
after_update_commit :crawl_rss, if: :rss_changed?

def crawl_rss
  ...
end

# good
after_update_commit :crawl_rss

def crawl_rss
  return if !rss_changed?
  ...
end

Constant Summary collapse

MSG =
'Avoid condition in callback declaration, move it inside the callback method instead.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/obsession/rails/no_callback_conditions.rb', line 50

def on_send(node)
  return if !callback_with_condition?(node)
  callback = node.children[1]
  return if callback == :validates
  return if callback.to_s.include?('around')

  add_offense(node)
end