Class: RuboCop::Cop::Rails::DelegateAllowBlank

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/rails/delegate_allow_blank.rb

Overview

This cop looks for delegations that pass :allow_blank as an option instead of :allow_nil. :allow_blank is not a valid option to pass to ActiveSupport#delegate.

Examples:

# bad
delegate :foo, to: :bar, allow_blank: true

# good
delegate :foo, to: :bar, allow_nil: true

Constant Summary collapse

MSG =
'`allow_blank` is not a valid option, use `allow_nil`.'

Instance Method Summary collapse

Instance Method Details

#autocorrect(pair_node) ⇒ Object



29
30
31
32
33
# File 'lib/rubocop/cop/rails/delegate_allow_blank.rb', line 29

def autocorrect(pair_node)
  lambda do |corrector|
    corrector.replace(pair_node.key.source_range, 'allow_nil')
  end
end

#on_send(node) ⇒ Object



23
24
25
26
27
# File 'lib/rubocop/cop/rails/delegate_allow_blank.rb', line 23

def on_send(node)
  allow_blank_option(node) do |offending_node|
    add_offense(offending_node)
  end
end