-
-
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
[Fix #3743] Add Rails/SkipsModelValidations
cop
#3825
[Fix #3743] Add Rails/SkipsModelValidations
cop
#3825
Conversation
70cb27d
to
6d47b2c
Compare
Rails/SkipsValidation
cop
# # good | ||
# user.update_attributes(website: 'example.com') | ||
class SkipsValidation < Cop | ||
MSG = ['%s skips validations, and will save the object to the database', |
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.
Just use a single multiline string literal here. And wrap %s in backquotes.
Also use a single sentence instead - e.g. "Don't use %s because it skips model validations." or something like this.
update_counters | ||
).freeze | ||
|
||
BLACKLIST = METHODS.map(&:to_sym).freeze |
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'd just write an array literal of symbols instead. :-)
Due to the nature of the checks this is bound to produce false positives, which I guess is ok. It'd be nice if this was added as a rule to the rails style guide. One more thing - you're not checking whether there's an explicit receiver to those method calls or not. This might reduce the false positives a bit (or not). :-) |
I'd name the cop "SkipsModelValidations". |
6d47b2c
to
ad5491d
Compare
Rails/SkipsValidation
copRails/SkipsModelValidation
cop
Rails/SkipsModelValidation
copRails/SkipsModelValidations
cop
ad5491d
to
e586c4a
Compare
There is already mention about the |
Either adding the methods to an array in |
if I do return unless receiver.nil? && BLACKLIST.include?(method_name) I get lesser number of rubocop errors on my project. do I need to check explicit receiver? |
e586c4a
to
c9f3abf
Compare
@Drenmi added picking methods from whitelist. |
c9f3abf
to
bbfbdf4
Compare
Btw, why don't we simply make the blacklist configurable? This would eliminate the need for a whitelist and would make the operation of the cop more transparent. |
@rahulcs ping :-) |
bbfbdf4
to
6aa7531
Compare
@bbatsov Hi, i was not able to make to figure out how to make blacklist configurable. |
They'll just override it. Not sure what your exact issue is, as we have several cops that employ configurable blacklists - e.g. |
@bbatsov thank you will check that. |
6aa7531
to
2d3c06e
Compare
@bbatsov figured it out :) thanks a lot. |
Why would you blacklist the |
warn against Rails ActiveRecord methods which skip validations.
Related issue: #3743
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.
rake generate_cops_documentation
(required only when you've added a new cop or changed the configuration/documentation of an existing cop).