-
-
Notifications
You must be signed in to change notification settings - Fork 257
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
Rails/ReflectionClassName gives false positive #106
Comments
The problem reported can be fixed (we can allow anything that is not a constant as the STR_CONST = 'user'
belongs_to :last_edited_by, class_name: STR_CONST belongs_to :last_edited_by, class_name: User These two different declarations of I'm not sure that this cop can reliably enforce its original intention without side-effects. |
This is also raising an error on: belongs_to :last_edited_by, class_name: User.to_s Is this intended? |
The rationale of the cop is to avoid triggering auto-loading, so this seems intended, as any reference to the class constant |
Ah, that makes sense. If this cop remains, is it worth adding this detail to the rule overview? It currently reads in 0.65:
Perhaps we could add:
|
There is no reason that a literal is required here. Any method or variable that has a string is just as good. The only reason we recommend a string literal is because that's the only case that Rubocop can detect. This to me seems like Rubocop overreaching. You can't solve this category of problem using static analysis. If one wanted to prevent a Class being passed, why not make a PR against activerecord and deprecate anything beside a String. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding! |
I agree with @jcoyne's analysis. I opened this duplicate (sorry!) issue to report the following use case which seems legitimate to me. class Comment < ApplicationRecord
belongs_to :parent, class_name: model_name.to_s
end We're avoiding duplication by using |
@jcoyne, In Rails 5.2, it's already the case, you cannot pass a Class (take a look at the original PR rails/rails#27551). The cop try to prevent the same think, circular dependencies, where
@floehopper you got a good example where the cop see an offense and there is no danger of circular dependencies. I don't know if it's an edge case or the cop needs to be change so it will not only search for a literal string. |
@Bhacaz using a class was @petewalker's example. My example was using a string from configuration |
@jcoyne Sorry I miss that detail 😕. It's right to flag the issue. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding! |
in my case I'm using a Module Builder, something like module HasOwner
module With
def with(klazz_name)
Module.new do
extend ActiveSupport::Concern
included do
has_many :xyz,
as: :owner,
class_name: klazz_name
end
end
end
end
extend With
end So that it gets used like class Thing < AR::Base
include HasOwner.with('Person')
end NOTE: the real case is more complex and this is a good solution. Now, 2 things:
|
Fixes rubocop#106. This PR marks `Rails/ReflectionClassName` as unsafe. And it would be possible to exclude `to_s` call other than constants. I will consider opening it as a separate PR.
Fixes rubocop#106. This PR marks `Rails/ReflectionClassName` as unsafe. And it would be possible to exclude `to_s` call other than constants. I will consider opening it as a separate PR.
…_unsafe [Fix #106] Mark `Rails/ReflectionClassName` as unsafe
…f string with `to_s` Follow rubocop#469 Fixes rubocop#106. This PR makes `Rails/ReflectionClassName` aware of the use of string with `to_s`.
…f string with `to_s` Follow rubocop#469 Fixes rubocop#106. This PR makes `Rails/ReflectionClassName` aware of the use of string with `to_s`.
…are_of_to_s [Fix #106] Make `Rails/ReflectionClassName` aware of the use of string with `to_s`
The principle being followed is to prefer false positives, since there are an abundance of ways to address those built in. Conversely, there is no way to get at false negatives. |
Expected behavior
It should not mark an error when a string is given
Actual behavior
Describe here what actually happened.
Steps to reproduce the problem
However
Spotlight::Engine.config.user_class
is returning a string.RuboCop version
The text was updated successfully, but these errors were encountered: