Skip to content
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

Conversation

rahulcs
Copy link
Contributor

@rahulcs rahulcs commented Dec 25, 2016

warn against Rails ActiveRecord methods which skip validations.

Related issue: #3743


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

@rahulcs rahulcs force-pushed the feature/rails-validation-skipping-method-usage branch 2 times, most recently from 70cb27d to 6d47b2c Compare December 25, 2016 18:35
@rahulcs rahulcs changed the title [Fix #3743] Add Rails/SkipsValidation cop [Fix #3743] Add Rails/SkipsValidation cop Dec 25, 2016
# # good
# user.update_attributes(website: 'example.com')
class SkipsValidation < Cop
MSG = ['%s skips validations, and will save the object to the database',
Copy link
Collaborator

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
Copy link
Collaborator

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. :-)

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 26, 2016

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). :-)

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 26, 2016

I'd name the cop "SkipsModelValidations".

@rahulcs rahulcs force-pushed the feature/rails-validation-skipping-method-usage branch from 6d47b2c to ad5491d Compare December 27, 2016 04:09
@rahulcs rahulcs changed the title [Fix #3743] Add Rails/SkipsValidation cop [Fix #3743] Add Rails/SkipsModelValidation cop Dec 27, 2016
@rahulcs rahulcs changed the title [Fix #3743] Add Rails/SkipsModelValidation cop [Fix #3743] Add Rails/SkipsModelValidations cop Dec 27, 2016
@rahulcs rahulcs force-pushed the feature/rails-validation-skipping-method-usage branch from ad5491d to e586c4a Compare December 27, 2016 04:39
@rahulcs
Copy link
Contributor Author

rahulcs commented Dec 27, 2016

There is already mention about the update_attribute method here in the rails-style-guide. Shall i update it?

@Drenmi
Copy link
Collaborator

Drenmi commented Dec 27, 2016

Due to the nature of the checks this is bound to produce false positives, which I guess is ok.

Either adding the methods to an array in config/default.yml to allow overriding, or having a whitelist option would probably be good. 😀

@rahulcs
Copy link
Contributor Author

rahulcs commented Dec 27, 2016

@bbatsov rake generate_cops_documentation is generating more documentation than i added.
attached diff. what do i need to do?

@rahulcs
Copy link
Contributor Author

rahulcs commented Dec 27, 2016

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). :-)

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?

@rahulcs rahulcs force-pushed the feature/rails-validation-skipping-method-usage branch from e586c4a to c9f3abf Compare December 27, 2016 09:06
@rahulcs
Copy link
Contributor Author

rahulcs commented Dec 27, 2016

@Drenmi added picking methods from whitelist.

@rahulcs rahulcs force-pushed the feature/rails-validation-skipping-method-usage branch from c9f3abf to bbfbdf4 Compare December 27, 2016 10:18
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 29, 2016

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.

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 2, 2017

@rahulcs ping :-)

@rahulcs rahulcs force-pushed the feature/rails-validation-skipping-method-usage branch from bbfbdf4 to 6aa7531 Compare January 3, 2017 06:51
@rahulcs
Copy link
Contributor Author

rahulcs commented Jan 3, 2017

@bbatsov Hi, i was not able to make to figure out how to make blacklist configurable.
if all the methods are initialized to be blacklisted, then how will the user remove the method from blacklist in .rubocop.yml?

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 3, 2017

They'll just override it. Not sure what your exact issue is, as we have several cops that employ configurable blacklists - e.g. Style/PredicateName.

@rahulcs
Copy link
Contributor Author

rahulcs commented Jan 3, 2017

@bbatsov thank you will check that.

@rahulcs rahulcs force-pushed the feature/rails-validation-skipping-method-usage branch from 6aa7531 to 2d3c06e Compare January 3, 2017 12:12
@rahulcs
Copy link
Contributor Author

rahulcs commented Jan 3, 2017

@bbatsov figured it out :) thanks a lot.

@bbatsov bbatsov merged commit bbb8089 into rubocop:master Jan 3, 2017
@rahulcs rahulcs deleted the feature/rails-validation-skipping-method-usage branch January 4, 2017 07:04
@dpisarewski
Copy link

dpisarewski commented Feb 7, 2017

Why would you blacklist the touch method. Called without arguments it does not change any data. What is a better alternative for touch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants