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

Fixes Rails/RelativeDateConstant offense #12601

Merged

Conversation

cyrillefr
Copy link
Contributor

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

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

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

@chahmedejaz chahmedejaz left a 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
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

Copy link
Collaborator

@rioug rioug left a 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
Copy link
Collaborator

@chahmedejaz chahmedejaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, Thanks, @cyrillefr 🎉

@rioug rioug merged commit 3afb3f5 into openfoodfoundation:master Jun 24, 2024
50 checks passed
@rioug rioug added the technical changes only These pull requests do not contain user facing changes and are grouped in release notes label Jun 26, 2024
@cyrillefr cyrillefr deleted the FixRailsRelativeDateConstant branch July 5, 2024 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
technical changes only These pull requests do not contain user facing changes and are grouped in release notes
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants