Skip to content

Commit

Permalink
Merge pull request #1254 from Earlopain/fix-error-for-rails-expanded-…
Browse files Browse the repository at this point in the history
…date-range

Fix an error for `Rails/ExpandedDateRange` when passing an argument only to the first method call
  • Loading branch information
koic committed Mar 16, 2024
2 parents 97afb4b + 9a7f954 commit a4f5c58
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_and_error_for_rails_expanded_date_range.md
Original file line number Diff line number Diff line change
@@ -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][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/expanded_date_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/expanded_date_range_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a4f5c58

Please sign in to comment.