-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Allow ignoring scopes for inverse_of cop #614
Conversation
3815ef8
to
8f4fd31
Compare
How do we detect that the latter condition is met? |
That's not something this Cop currently detects, with or without this change (and I'm not sure there's a good way we could detect it without runtime info). For example: class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post, -> { where("1=0") }
end Rails is not able to automatically infer the inverse_of for And regardless of the change in this PR the Rails/InverseOf cop will not report an offense on I think this is actually a good thing, since setting class Post < ActiveRecord::Base
has_many :comments, inverse_of: post
end
class Comment < ActiveRecord::Base
belongs_to :post, -> { where("1=0") }
end
post = Post.create!
comment = post.comments.create!
# This fails because we use the already loaded inverse record,
# and thus never run the scope.
assert_nil comment.post |
8f4fd31
to
7eecd2c
Compare
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.
You've right, I've missed the point of Rails/InverseOf
cop.
Now, when Rails provides this option, inverse_of
becomes redundant.
Looks good to me.
Thanks for the contribution!
🎉 @pirj any chance you have access to approve the workflow so I can verify everything passes? |
Unfortunately not, I'm only volunteering here with preliminary assessment of tickets and preliminary code review. |
Can you update the config/default.yml? Rails/InverseOf:
Description: 'Checks for associations where the inverse cannot be determined automatically.'
Enabled: true
VersionAdded: '0.52'
+ IgnoreScopes: false
Include:
- app/models/**/*.rb |
lib/rubocop/cop/rails/inverse_of.rb
Outdated
# class Post < ApplicationRecord | ||
# belongs_to :blog | ||
# 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.
Perhaps the contrast will be more noticeable whether only the differences are extracted as shown below.
# @example IgnoreScopes: false (default)
# # bad
# class Blog < ApplicationRecord
# has_many :posts, -> { order(published_at: :desc) }
# end
#
# @example IgnoreScopes: true
# # good
# class Blog < ApplicationRecord
# has_many :posts, -> { order(published_at: :desc) }
# end
And can you move this as the bottom example?
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.
Done! Thank you for the review, and hi 👋🏻!
7eecd2c
to
bd968b1
Compare
With rails/rails#43358, Rails can now infer inverse_of for associations with a scope when `config.active_record.automatic_scope_inversing = true`. This commit adds an `IgnoreScopes` option to the inverse_of cop allowing us to disable the cop for scopes when the new Rails option is set to `true`. I considered having the default for `IgnoreScopes` change based on the Rails version since this is the default for new Rails >= 7.0 applications, but existing applications that upgrade to 7.0 wouldn't necessarily have the option set and in that case changing the rubocop-rails default behavior would be a breaking change.
bd968b1
to
95b3e8c
Compare
Thank you, Daniel! |
With rails/rails#43358, Rails can now infer
inverse_of for associations with a scope when
config.active_record.automatic_scope_inversing = true
.This commit adds an
IgnoreScopes
option to the inverse_of cop allowingus to disable the cop for scopes when the new Rails option is set to
true
.I considered having the default for
IgnoreScopes
change based on theRails version since this is the default for new Rails >= 7.0
applications, but existing applications that upgrade to 7.0 wouldn't
necessarily have the option set and in that case changing the
rubocop-rails default behavior would be a breaking change.
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.and description in grammatically correct, complete sentences.