-
-
Notifications
You must be signed in to change notification settings - Fork 724
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 cop RedundantPresenceValidationOnBelongs #12380
Fix cop RedundantPresenceValidationOnBelongs #12380
Conversation
- presence: true is redundant since Rails 5.0 BUT applies with new default config of belongs_to_required_by_default to true Lots of files with belongs_to_required_by_default = false (backward compatibility) So: deleting this setting implies to adding optional: true on belongs_to relations where there is no explicit check for presence. And also to delete redundant presence: true The implicit becomes the explicit and vice versa - updated toto list
baa2169
to
37814c4
Compare
I understand that using These are the relations that jumped out to me, there might others : LineItem
belongs_to :order, class_name: "Spree::Order", inverse_of: :line_items, optional: true
Order
belongs_to :user, class_name: "Spree::User", optional: true
belongs_to :created_by, class_name: "Spree::User", optional: true
belongs_to :order_cycle, optional: true
belongs_to :distributor, class_name: 'Enterprise', optional: true
belongs_to :customer, optional: true
ProductProperty
belongs_to :product, class_name: "Spree::Product", touch: true, optional: true @openfoodfoundation/core-devs what do you think ? |
Thanks Gaetan, yes those ones that you've listed look like they should be required, we just haven't yet completed the work that was started in #11297. Maybe Maikel can share more insight from when he worked on it, but I think we just need to work through each file one-by-one. Sorry Cyrille, as you've found this is not a simple one! It might be helpful to separate the simpler ones (like the first few files up to and including address) into a separate commit, then deal with the harder ones step by step, in further commits or PRs. |
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.
As per previous comment
This PR has been chunked in smaller ones. Each is listed here. So, I can now close it. |
What? Why?
Contributes to Fix Rubocop Rails issues #11482
Cop: Rails::RedundantPresenceValidationOnBelongsTo
removes
presence: true
NB: Since Rails 5.0, default implicit checking of presence went from false to true. To avoid breaking things, this default has been added to some files, to keep the current behaviour. See the PR here.
So for each of the offenses to be addressed, I have remove that line and made the explicit, implicit , and vice versa.
Which means:
active_record.belongs_to_required_by_default
config, also removing thepresence: true
, but also adding anoptional: true
for thebelongs_to
that were not validatedapp/models/spree/address.rb
file:A state is not required, but a country is, for the
Spre::Address
model.Pre-Rails 5.0: validates country (with self.belongs_to_required_by_default = false) bc country mandatory but not state.
Post-Rails 5.0: removes validates country BUT add
optional: true
tobelongs_to :state
otherwise state is mandatory (with belongs_to_required_by_default set to true by Rails by default). By default now each belongs_to will be checked for presence unless explicitly statingoptional: true
NB 2 Some fields might be tagged optional ( like
user
in theSpree::Order
model) and not be valid if the field is missing, but it is the same behaviour as before (there is no null in the DB column).NB 3 Even though we have
belongs_to optional: true
, that does not deter tohave one
validates :specifib_context
(Spree::Order
model). And no offense raised.NB 4 I had 3 strange errors, it seems that on validation, there is no more
"can't be blank"
, but"must exists"
They are ActiveRecord messages, so not supposed to be displayed.
Though, I do not know if this a problem or not.
Cf. the conversation here on rubocop github.
One of the message I have got:
So, I have modified these 2 specs accordingly:
spec/models/subscription_line_item_spec.rb
spec/models/tag_rule_spec.rb
What should we test?
Nothing. All offenses should be corrected and therefore raise nothing.
All tests should pass and so show no sign of regression.
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.