-
-
Notifications
You must be signed in to change notification settings - Fork 708
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 Rubocop Rails: Rails/HasManyOrHasOneDependent #12313
Fix Rubocop Rails: Rails/HasManyOrHasOneDependent #12313
Conversation
1e0b382
to
27ced44
Compare
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.
Thanks for your help ! I have some reservation on some of the changes that have been made.
We need to think a bit more about what we delete or not when deleting an enterprise. Maybe we should also use act_as_paranoid
on Enterprise
? Or maybe we need to replace the enterprise by a place holder, ie : https://world.hey.com/jorge/code-i-like-i-domain-driven-boldness-71456476 ?
ping @openfoodfoundation/developers
app/models/spree/stock_item.rb
Outdated
@@ -8,7 +8,7 @@ class StockItem < ApplicationRecord | |||
|
|||
belongs_to :stock_location, class_name: 'Spree::StockLocation', inverse_of: :stock_items | |||
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant' | |||
has_many :stock_movements | |||
has_many :stock_movements, dependent: nil |
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 am not sure why we would want to keep stock_movements on a delete stock item, but maybe I am missing some context.
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 agree. We should destroy stock movements.
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.
Can you change this to destroy?
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.
Fixed.
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.
Thank you for working on this. It's impossible for you to know how our business logic works. So we added some comments for the behaviour we would like for now.
app/models/spree/stock_item.rb
Outdated
@@ -8,7 +8,7 @@ class StockItem < ApplicationRecord | |||
|
|||
belongs_to :stock_location, class_name: 'Spree::StockLocation', inverse_of: :stock_items | |||
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant' | |||
has_many :stock_movements | |||
has_many :stock_movements, dependent: nil |
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 agree. We should destroy stock movements.
67c2df3
to
465bf40
Compare
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.
Much better, I just found one last thing to change.
app/models/spree/stock_item.rb
Outdated
@@ -8,7 +8,7 @@ class StockItem < ApplicationRecord | |||
|
|||
belongs_to :stock_location, class_name: 'Spree::StockLocation', inverse_of: :stock_items | |||
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant' | |||
has_many :stock_movements | |||
has_many :stock_movements, dependent: nil |
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.
Can you change this to destroy?
465bf40
to
fc4b59d
Compare
Thanks @mkllnk |
That model came from the Spree framework and it seems like they didn't account for deleting any data. There are not callbacks registered on the model, so could just do a delete. |
The reasoning is that we should not delete an address that has ever received a shipment
TaxRate acts_as_paranoid iand is thus not hard_deleted
Spree::Variant acts_as_paranoid and is thus not hard deleted
As much as the associated models act_as_paranoid, it doesnt make sense to keep them around after deleting the enterprise
f70ea27
to
293ce91
Compare
293ce91
to
0d03cdf
Compare
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.
Looks good now 👍 Thanks for your help !
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.
Thank you!
Testing:
|
Hey @mkllnk ,
Thanks for pointing that out. I've staged the PR and verified that deleting both products and variants work as before:
Looks good, merging. |
What? Why?
Fix all violations of the Rails/HasManyOrHasOneDependent cop.
Rubocop requires
has_many
orhas_one
associations to specify a:dependent
optionWhat should we test?
N/A
Release notes