Class: Ducalis::UselessOnly

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Defined in:
lib/ducalis/cops/useless_only.rb

Constant Summary collapse

OFFENSE =
"  | Seems like there is no any reason to keep before filter only for one action. Maybe it will be better to inline it?\n".gsub(/^ +\|\s/, '').strip
DETAILS =
"  | Compare:\n\n  | ```ruby\n  | before_filter :do_something, only: %i[index]\n  | def index; end\n\n  | # to\n\n  | def index\n  |   do_something\n  | end\n\n  | ```\n\n".gsub(/^ +\|\s/, '').strip
FILTERS =
i[before_filter after_filter around_filter
before_action after_action around_action].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ducalis/cops/useless_only.rb', line 31

def on_send(node)
  _, method_name, *args = *node
  hash_node = args.find { |subnode| subnode.type == :hash }
  return unless FILTERS.include?(method_name) && hash_node

  type, method_names = decomposite_hash(hash_node)
  return unless type == s(:sym, :only)
  return unless method_names.children.count == 1

  add_offense(node, :selector, OFFENSE)
end