Class: RuboCop::Cop::Infinum::AttributeDefaultBlockValue

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/infinum/attribute_default_block_value.rb

Overview

This cop looks for ‘attribute` class methods that specify a `:default` option and pass it a method without a block.

Examples:

# bad
class User < ApplicationRecord
  attribute :confirmed_at, :datetime, default: Time.zone.now
end

# good
class User < ActiveRecord::Base
  attribute :confirmed_at, :datetime, default: -> { Time.zone.now }
end

Constant Summary collapse

MSG =
'Pass method in a block to `:default` option.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/infinum/attribute_default_block_value.rb', line 32

def on_send(node)
  default_attribute(node) do |hash_pair|
    target_node = attribute(hash_pair)
    return unless target_node.send_type?

    add_offense(target_node, message: MSG) do |corrector|
      expression = attribute(default_attribute(node))

      corrector.replace(target_node, "-> { #{expression.source} }")
    end
  end
end