Class: RuboCop::Cop::Rails::ExpandedDateRange

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

Overview

Checks for expanded date range. It only compatible ‘..` range is targeted. Incompatible `…` range is ignored.

Examples:

# bad
date.beginning_of_day..date.end_of_day
date.beginning_of_week..date.end_of_week
date.beginning_of_month..date.end_of_month
date.beginning_of_quarter..date.end_of_quarter
date.beginning_of_year..date.end_of_year

# good
date.all_day
date.all_week
date.all_month
date.all_quarter
date.all_year

Constant Summary collapse

MSG =
'Use `%<preferred_method>s` instead.'
PREFERRED_METHODS =
{
  beginning_of_day: 'all_day',
  beginning_of_week: 'all_week',
  beginning_of_month: 'all_month',
  beginning_of_quarter: 'all_quarter',
  beginning_of_year: 'all_year'
}.freeze
MAPPED_DATE_RANGE_METHODS =
{
  beginning_of_day: :end_of_day,
  beginning_of_week: :end_of_week,
  beginning_of_month: :end_of_month,
  beginning_of_quarter: :end_of_quarter,
  beginning_of_year: :end_of_year
}.freeze

Constants included from TargetRailsVersion

TargetRailsVersion::TARGET_GEM_NAME, TargetRailsVersion::USES_REQUIRES_GEM_API

Instance Method Summary collapse

Methods included from TargetRailsVersion

minimum_target_rails_version, support_target_rails_version?

Instance Method Details

#on_irange(node) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/rails/expanded_date_range.rb', line 48

def on_irange(node)
  begin_node = node.begin
  end_node = node.end
  return if allow?(begin_node, end_node)

  preferred_method = preferred_method(begin_node)
  if begin_node.method?(:beginning_of_week) && begin_node.arguments.one? && end_node.arguments.one?
    return unless same_argument?(begin_node, end_node)

    preferred_method << "(#{begin_node.first_argument.source})"
  elsif any_arguments?(begin_node, end_node)
    return
  end

  register_offense(node, preferred_method)
end