-
-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Rails/ExpandedDateRange
aware beginning_of_week
with an argument
#730
Conversation
…e/time" rule Follow up rubocop/rubocop-rails#730. This PR adds new "Prefer `all_(day|week|month|quarter|year)` over range of date/time" rule. ```ruby # 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 ```
6f4a44d
to
3994741
Compare
I guess it is this line. I see a number of similar errors when running it against |
3994741
to
f0bb668
Compare
I've updated this PR. Thank you @pirj! |
Nice! Running on the same corpus:
|
replacement = replacement(range_begin) | ||
|
||
if range_begin.method?(:beginning_of_week) && range_begin.arguments.one? | ||
return unless same_argument?(range_begin, range_end) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call it same_receiver?
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an intended naming because it confirms the argument of beginning_of_week
.
end | ||
|
||
def replacement(range_begin) | ||
+"#{range_begin.receiver.source}.#{PREFER_METHODS[range_begin.method_name]}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it need a +
? It won't be frozen, since it contains interpolation, will it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it's a feature from Ruby 3.0.
% cat example.rb
# frozen_string_literal: true
value = 'value'
p "#{value}".frozen?
Ruby 2.7
% ruby -ve xample.rb
ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a) [x86_64-darwin19]
true
Ruby 3.0
% ruby -v example.rb
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-darwin19]
false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL, thank you!
Checked manually for some potential offences: Invoice.where(invoice_date: Date.today.beginning_of_year..Date.today) This may be unsafe to correct, as we don't really know if there are dates in the future. All good. |
|
Oops! I completely overlooked it... On the other hand, this implementation is better to detecting the |
f0bb668
to
8889e1c
Compare
…ment This PR makes `Rails/ExpandedDateRange` aware `beginning_of_week` with an argument. ```ruby # bad date.beginning_of_week(:sunday)..date.end_of_week(:sunday) # good date.all_week(:sunday) ```
8889e1c
to
15858ae
Compare
Rails/DateTimeRange
copRails/ExpandedDateRange
aware beginning_of_week
with an argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you!
I'm running low on disk space, can't check on real-world-rails
now.
And I guess the code is the same as for the previous run, so it makes no sense to run it on real-world-rspec
again.
…e/time" rule Follow up rubocop/rubocop-rails#730. This PR adds new "Prefer `all_(day|week|month|quarter|year)` over range of date/time" rule. ```ruby # 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 ```
This PR makes
Rails/ExpandedDateRange
awarebeginning_of_week
with an argument.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.