-
-
Notifications
You must be signed in to change notification settings - Fork 264
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
[Fix #504] Rails/FindBy being wrongly triggered on non Active Record methods #506
[Fix #504] Rails/FindBy being wrongly triggered on non Active Record methods #506
Conversation
b43a5e5
to
d2a6ae3
Compare
lib/rubocop/cop/rails/find_by.rb
Outdated
@@ -31,7 +31,12 @@ class FindBy < Base | |||
MSG = 'Use `find_by` instead of `where.%<method>s`.' | |||
RESTRICT_ON_SEND = %i[first take].freeze | |||
|
|||
def_node_matcher :preceding_where?, <<~PATTERN |
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.
Initially the matcher was called where_first
but I thought it may be confused with the ignore_where_first?
method.
Would love to hear better name suggestions ❤️
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.
I overlooked this case 😅 I think it's better to replace it with logic equivalent to SCOPE_METHODS
instead of def_node_matcher
.
https://github.com/rubocop/rubocop-rails/blob/v2.11.0/lib/rubocop/cop/rails/find_each.rb#L33
Besides where
, there are all
, eager_load
, includes
, and so on.
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.
Ah, interesting, we can use the SCOPE_METHODS
approach, but If I'm not mistaken for our case it would be simply SCOPE_METHODS = %i[where]
and that's it. Because where().take
and where().first
are the only two cases we want to check in the scope of this cop. Like we can't suggest find_by
in cases like User.all.take(5)
so we need just to skip those
Let me know if I'm missing something. Thanks!
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.
I've rebased on master and refactored it to avoid defining the def_node_matcher
and explicitly check for receiver&.method?(:where)
I also removed the receiver.block_type?
check because it should be covered by the unless receiver&.method?(:where)
check
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.
Ah, you're right! Thank you!
I'll like to add, since #502, this kind of code lead Rubocop to crash. @nvasilevski work should fix it. 🚀 def simple_history
versions.includes(:account).reorder(created_at: :desc).limit(20).slice_when do |prev, curr|
prev.whodunnit != curr.whodunnit || prev.created_at.to_date != curr.created_at.to_date
end.map { |versions| versions.max_by(&:created_at) }.take(5)
end
Thanks a lot @koic @nvasilevski. ❤️ |
Follow up rubocop#506 (comment). This PR fixes an error for `Rails/FindBy` when calling `take` after block.
d2a6ae3
to
c58e226
Compare
Fixes - #504
If I'm not mistaken the #502 PR removed an important part of the
Rails/FindBy
cop which was checking for a presence ofwhere()
method call in the method chain.Without this check the cop tries to autocorrect an offence for regular enumerable
first
andtake
methodsThis PR basically brings the check back with a bit different name.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.and description in grammatically correct, complete sentences.
bundle exec rake default
. It executes all tests and RuboCop for itself, and generates the documentation.