Class: RuboCop::Cop::Rails::AttributeDefaultBlockValue
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::AttributeDefaultBlockValue
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/rails/attribute_default_block_value.rb
Overview
Looks for ‘attribute` class methods that specify a `:default` option which value is an array, string literal or method call without a block. It will accept all other values, such as string, symbol, integer and float literals as well as constants.
Constant Summary collapse
- MSG =
'Pass method in a block to `:default` option.'
- RESTRICT_ON_SEND =
%i[attribute].freeze
- TYPE_OFFENDERS =
%i[send array hash].freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rubocop/cop/rails/attribute_default_block_value.rb', line 75 def on_send(node) default_attribute(node) do |attribute| value = attribute.children.last return unless TYPE_OFFENDERS.any?(value.type) add_offense(value) do |corrector| expression = default_attribute(node).children.last corrector.replace(value, "-> { #{expression.source} }") end end end |