-
-
Notifications
You must be signed in to change notification settings - Fork 710
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
Fixes Rails/RelativeDateConstant offense #12601
Fixes Rails/RelativeDateConstant offense #12601
Conversation
- Cop: Rails/RelativeDateConstant - raises offense if Constant is relative data (ie: since, ago) - Reason: relative data will be evaluated only once - BUT here, Date should not be evaluated in a class method, and have a different - value for each call. But the data should be the same for an instance - Therefore: move the ago in init method - Cf. https://docs.rubocop.org/rubocop-rails/cops_rails.html#railsrelativedateconstant - Since there is no constant to be called form a class, but a date from an instance, the spec has been modified accordingly. The RemoveTransientData.new.call had to be splitted.
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.
Nicely done 🎉
Just a small suggestion in the RemoveTransientData
. :)
Spree::StateChange.where("created_at < ?", RETENTION_PERIOD).delete_all | ||
Spree::LogEntry.where("created_at < ?", RETENTION_PERIOD).delete_all | ||
Session.where("updated_at < ?", RETENTION_PERIOD).delete_all | ||
Spree::StateChange.where("created_at < ?", @expiration_date).delete_all |
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.
It would be great if we could use the attr_reader for @expiration_date
in this class as well i.e. expiration_date
.
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 :)
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.
Good one, thanks @cyrillefr 🙏 , could you just address @chahmedejaz comment ?
- replace direct instance variable use by instance getter use
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.
Perfect, Thanks, @cyrillefr 🎉
What? Why?
Contributes to #11482
The: Rails/RelativeDateConstant Cop checks whether constant value isn’t relative date. Because the relative date will be evaluated only once.
Constant is relative date : Constant.since or Constant.ago.
However here, expiration date should stay the same for the same instance hence, in init.
Spec has been modified accordingly
What should we test?
Nothing.
Automatic specs should all pass.
Release notes
Changelog Category (reviewers may add a label for the release notes):
The title of the pull request will be included in the release notes.
Dependencies
Documentation updates