Class: RuboCop::Cop::Rails::PluralizationGrammar

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

Overview

Checks for correct grammar when using ActiveSupport’s core extensions to the numeric classes.

Examples:

# bad
3.day.ago
1.months.ago
5.megabyte
1.gigabytes

# good
3.days.ago
1.month.ago
5.megabytes
1.gigabyte

Constant Summary collapse

SINGULAR_METHODS =
{
  second: :seconds,
  minute: :minutes,
  hour: :hours,
  day: :days,
  week: :weeks,
  fortnight: :fortnights,
  month: :months,
  year: :years,
  byte: :bytes,
  kilobyte: :kilobytes,
  megabyte: :megabytes,
  gigabyte: :gigabytes,
  terabyte: :terabytes,
  petabyte: :petabytes,
  exabyte: :exabytes,
  zettabyte: :zettabytes
}.freeze
RESTRICT_ON_SEND =
SINGULAR_METHODS.keys + SINGULAR_METHODS.values
PLURAL_METHODS =
SINGULAR_METHODS.invert.freeze
MSG =
'Prefer `%<number>s.%<correct>s`.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/rails/pluralization_grammar.rb', line 49

def on_send(node)
  return unless duration_method?(node.method_name) && literal_number?(node.receiver) && offense?(node)

  number, = *node.receiver

  add_offense(node, message: message(number, node.method_name)) do |corrector|
    method_name = node.loc.selector.source

    corrector.replace(node.loc.selector, correct_method(method_name))
  end
end