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

Use Spree::Base as models base class #3476

Merged

Conversation

filippoliverani
Copy link
Contributor

Description
Ref #3450

This PR introduces Spree::ApplicationRecord class and makes every ActiveRecord model previously inheriting from ActiveRecord::Base inherit from it.
To leverage the new hierarchy, an existing Spree::Core::Permalinks monkey patch on ActiveRecord::Base has been moved to Spree::ApplicationRecord.

This way Solidus is more compliant with Rails 5+ recommendations and the risk of unwanted side effects on ActiveRecord::Base is mitigated.

Checklist:

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

This is a good change, thanks 👏

But, I recommend to name not name this class ApplicationRecord even though it is namespaced it potentially conflicts with the top level ApplicationRecord new Rails apps have.

What about Spree::BaseRecord? Also, we already have Spree::Base, right? What do we do about it?

core/app/models/spree/application_record.rb Outdated Show resolved Hide resolved
core/app/helpers/spree/base_helper.rb Outdated Show resolved Hide resolved
@filippoliverani
Copy link
Contributor Author

filippoliverani commented Jan 15, 2020

@tvdeyen Thank you for the quick review 🙇

This is a good change, thanks 👏

But, I recommend to name not name this class ApplicationRecord even though it is namespaced it potentially conflicts with the top level ApplicationRecord new Rails apps have.

What about Spree::BaseRecord? Also, we already have Spree::Base, right?
What do we do about it?

I think that is important to keep ApplicationRecord name because its has become a Rails convention, app and engine generators since Rails 5 create this class and almost everybody expects this name.
If we are not sure that namespacing is not enough we could use the require_dependency trick suggested here

@filippoliverani filippoliverani force-pushed the use-application-record-as-base branch from 36537fd to d68d514 Compare January 17, 2020 08:06
kennyadsl
kennyadsl previously approved these changes Jan 20, 2020
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

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

@kennyadsl
Copy link
Member

@filippoliverani we discussed this PR during the last core team meeting. We think that probably it's enough to just use Spree::Base as our ApplicationRecord, which is doing the same thing and it's an abstract class as well.

It has a different naming convention than what Rails users expect though but maybe changing the name of that class to Spree::ApplicationRecord is not worth it.

We propose to keep the rest of the changes (using it for models that were inheriting from ActiveRecord::Base directly + move the monkey patch into Spree::Base) and get rid of the ApplicationRecord file at all.

What do you think?

@kennyadsl kennyadsl dismissed their stale review January 29, 2020 17:01

Re-considered my approval as is after a core team discussion

@filippoliverani
Copy link
Contributor Author

filippoliverani commented Jan 31, 2020

@kennyadsl thank you for the update, the main point was to stop using ActiveRecord::Base directly, I will rearrange the code to reflect the preferred naming convention 👍

Solidus ActiveRecord models should follow Rails 5+ convention of
inheriting from an itermediate class to avoid monkey patching
ActiveRecord::Base.
Monkey patching directly ActiveRecord::Base is dangerous and
discouraged.
@filippoliverani filippoliverani force-pushed the use-application-record-as-base branch from d68d514 to 8b5f2c2 Compare January 31, 2020 10:04
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

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

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

Hmm. I am not sure if Spree::Base is the correct base class for all our database models. They now all have Ransack and Preferences included.

Is this what we want? Does this lead to potential issues?

🤔

@kennyadsl
Copy link
Member

@tvdeyen Spree::Base is already the parent class for all our models. There were just a few that are inheriting from ActiveRecord::Base but doesn't seem to exist a specific reason for that.

@filippoliverani
Copy link
Contributor Author

filippoliverani commented Jan 31, 2020

@tvdeyen There's a lot of stuff coming along with Spree::Base, not sure if that could be a problem. At this point is a trade off between having a new lean Spree::ApplicationRecord class or staying with an existing fat Spree::Base class.
Otherwise we could include Ransack and Preferences support as concerns only where needed.

@tvdeyen
Copy link
Member

tvdeyen commented Jan 31, 2020

@kennyadsl @filippoliverani

Sorry, I wasn't so sure. Then it makes sense to use it for now. Next step could be to use a lean base class for models that do not need ransack and preferences.

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

👏

@kennyadsl kennyadsl merged commit a363a74 into solidusio:master Jan 31, 2020
@filippoliverani filippoliverani changed the title Use ApplicationRecord as models base class Use Spree::Base as models base class Jul 3, 2020
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.

3 participants