diff --git a/changelog/fix_and_error_for_rails_expanded_date_range.md b/changelog/fix_and_error_for_rails_expanded_date_range.md new file mode 100644 index 0000000000..173f2c7bc2 --- /dev/null +++ b/changelog/fix_and_error_for_rails_expanded_date_range.md @@ -0,0 +1 @@ +* [#1254](https://github.com/rubocop/rubocop-rails/pull/1254): Fix an error for `Rails/ExpandedDateRange` when passing an argument only to the first method call for weeks. ([@earlopain][]) diff --git a/lib/rubocop/cop/rails/expanded_date_range.rb b/lib/rubocop/cop/rails/expanded_date_range.rb index 073d41abe9..1655583097 100644 --- a/lib/rubocop/cop/rails/expanded_date_range.rb +++ b/lib/rubocop/cop/rails/expanded_date_range.rb @@ -51,7 +51,7 @@ def on_irange(node) return if allow?(begin_node, end_node) preferred_method = preferred_method(begin_node) - if begin_node.method?(:beginning_of_week) && begin_node.arguments.one? + 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})" diff --git a/spec/rubocop/cop/rails/expanded_date_range_spec.rb b/spec/rubocop/cop/rails/expanded_date_range_spec.rb index 039a713c2c..db9896b160 100644 --- a/spec/rubocop/cop/rails/expanded_date_range_spec.rb +++ b/spec/rubocop/cop/rails/expanded_date_range_spec.rb @@ -57,6 +57,12 @@ RUBY end + it 'does not register an offense when using `date.beginning_of_week(:sunday)..date.end_of_week`' do + expect_no_offenses(<<~RUBY) + date.beginning_of_week(:sunday)..date.end_of_week + RUBY + end + it 'registers and corrects an offense when using `date.beginning_of_year..date.end_of_year`' do expect_offense(<<~RUBY) date.beginning_of_year..date.end_of_year