-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Added ReflectionClassNameString cop #6704
Added ReflectionClassNameString cop #6704
Conversation
This seems like a good cop to me. 👍 https://github.com/rubocop-hq/rubocop-rails also exists, so I think you'll want to add this cop to that repository too. |
FYI, since Rails 5.2 app if a string isn't used, the following error occurs. /Users/koic/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/activerecord-5.2.2/lib/active_record/reflection.rb:436:in `initialize': A class was passed to `:class_name` but we are expecting a string. (ArgumentError) Having auto-correct in the future will be useful when migrating from old Rails to new Rails. |
Looks good to me. Can you squash your commits into one? |
If you do not mind, could you please update the body of commit message with the description of the PR that you wrote using After merging this PR I'd like to forward porting it to the RuboCop Rails repository (Rails cops are extracted in the near future to this repository). Moving the repository will weaken the linkage with PR, so it will be helpful if details are written in the body of commit message. |
This PR add a cop to check if the value of the option `class_name` of a [ActiveRecord::Reflection](https://apidock.com/rails/ActiveRecord/Reflection/AbstractReflection/class_name) is a string. _Why this cop:_ I was working in a Rails project where often there was a constant used for `class_name` value. That caused a issue which was to preload unnecessary models use in the definition of a reflection.
I merged this PR. And I will forward port it to the RuboCop Rails repository. @mikegee Your comment is right, however I reviewed and merged this PR because it takes a little more time to adjust RuboCop Rails release 💦 Thank you as always. |
Using e.g. |
@bquorning Using |
Shouldn't this cop allow a symbol value as well as a string? |
@ryym We can discuss about that (maybe in an issue), but using a symbol can quickly have its limits when using module. Example: |
I just came here to ask the same a @ryym For your example you could use The advice that I've generally heard is that symbols are better for memory usage than strings when you can use them |
@cheerfulstoic You're right that can works. But Reflection#class_name call In the following example there will be 1 symbol and 2 strings (when using # ActiveRecord #1
has_one :account, class_name: :Account
# ActiveRecord #2
has_many :account, class_name: :Account OR 2 strings # ActiveRecord #1
has_one :account, class_name: 'Account'
# ActiveRecord #2
has_many :account, class_name: 'Account' Proof that options = { class_name: 'Account' }
p options[:class_name].object_id # => 70101446453420
@class_name = options[:class_name].to_s
p @class_name.object_id # => 70101446453420
options = { class_name: :Account }
p options[:class_name].object_id # => 6419108
@class_name2 = options[:class_name].to_s
p @class_name2.object_id # => 2347999460 Correct me if there something wrong |
@Bhacaz I see. I didn't notice them. Thanks! |
I didn't know that it called 👍 |
This PR add a cop to check if the value of the option
class_name
of a ActiveRecord::Reflection is a string.Why this cop:
I was working in a Rails project where often there was a constant used for
class_name
value. That caused a issue which was to preload unnecessary models use in the definition of a reflection.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).and description in grammatically correct, complete sentences.
bundle exec rake default
. It executes all tests and RuboCop for itself, and generates the documentation.