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
Creating ApplicationModel #12162
Creating ApplicationModel #12162
Conversation
The application model will keep all of the Active Record configurations, instead of sending the configurations directly to ActiveRecord::Base. This means it will be possible to configure multiple applications if each application has its own ApplicationModel. Also, changing some tests to correspond to these changes.
The Active Record tests should now include inside of it an ApplicationModel.
This replaces the current use of ActiveRecord::Base in most tests.
These methods will be used by the ApplicationModel to send the correct configuration.
|
Awesome pull request, thank you! I've noticed several things:
Don't hesitate to ping me if you want some help. |
|
Thanks! Given the very large scope of this PR could this be a right time to discuss extension vs composition? I've seen some feedback of people discussing that directly subclassing from class Person
# Person is the root object under the problem domain.
# The fact it uses AR::Base as a persistence implementation, shouldn't drive the domain class hierarchy.
include ActiveRecord::Base # Or ApplicationModel
end
class Client < Person
# Client is a domain-driven subclass of Person, so it makes sense for it to be an actual subclass
endThe above would also make it more flexible to use This change going to be a disruptive change to matter what, so it's a good time as any to discuss what's the best approach. Thanks again! |
| @@ -92,7 +92,7 @@ def test_module_file_is_not_created | |||
|
|
|||
| def test_adds_namespace_to_model | |||
| run_generator | |||
| assert_file "app/models/test_app/account.rb", /module TestApp/, / class Account < ActiveRecord::Base/ | |||
| assert_file "app/models/test_app/account.rb", /module TestApp/, / class Account < ApplicationModel/ | |||
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.
Should base model classes be generated per namespace? E.g.
module TestApp
class AppicationModel < ::ApplicationModel
end
end
module TestApp
class Account < ApplicationModel
end
endAnyone remembers how controller generators work for namespaced controllers?
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.
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.
Yep, you're right about this. I'll make the change.
|
We already had this discussion in rails 4 and the conclusion was: there are Also include also means inheritance in Ruby, so this change is not worth of
|
|
What impact will there be on Rails Engines? Will there be a separate ApplicationModel per engine? |
|
I am |
|
@fxn maybe this should be named |
|
@guilleiguaran yeah, I believe |
|
+1 on |
|
+1 on |
|
-1 about included module. See 9e4c41c for reason |
|
So it seems that most people think it should be called |
This method first checks to see if an application configuration is defined, then gets the correct ApplicationModel. Reverts to ActiveRecord::Base if it can't find anything.
The comments have become outdated since the creation of the ApplicationModel. Now, the model classes should inherit from ApplicationModel instead of ActiveRecord::Base.
This new name is less general and more appropriate since these changes are limited to Active Record.
|
we agreed with @wangjohn to move his GSoC PRs to this branch https://github.com/rails/rails/tree/rework_initialization |
This PR creates the
ApplicationModelclass. This class will serve as an intermediate between a model andActiveRecord::Base. Currently, to create a model, one follows the following procedure:This inherits directly from
ActiveRecord::Baseand means that all configurations onActiveRecord::Baseare global to all of its subclasses (unless the subclass redefines the configuration on its own).I'm introducing
ApplicationModelso that models have an intermediary layer for inheritance like so:What the
ApplicationModelclass does is allow different sets of configurations to be defined on multiple applications. Each Rails application will have its ownApplicationModel. TheApplicationModelfigures out which application it belongs when it is pulled in through the railtie andconfigs_fromis specified.\cc @spastorino, @josevalim Can you guys take a look? I'm sure there's something I forgot so please let me know.