diff options
author | Genadi Samokovarov <gsamokovarov@gmail.com> | 2015-12-12 14:25:00 +0100 |
---|---|---|
committer | Genadi Samokovarov <gsamokovarov@gmail.com> | 2015-12-16 10:30:09 +0100 |
commit | 2067fff9e38df4e37bdbfc021cd6bb0c2d393a2a (patch) | |
tree | 18c5067747cc422aed35dce0417f14ddf36346a7 /activerecord | |
parent | e73fe1dd8c2740ae29e7a7f48d71a62b46e6b49d (diff) | |
download | rails-2067fff9e38df4e37bdbfc021cd6bb0c2d393a2a.tar.gz rails-2067fff9e38df4e37bdbfc021cd6bb0c2d393a2a.tar.bz2 rails-2067fff9e38df4e37bdbfc021cd6bb0c2d393a2a.zip |
Introduce ApplicationRecord, an Active Record layer supertype
It's pretty common for folks to monkey patch `ActiveRecord::Base` to
work around an issue or introduce extra functionality. Instead of
shoving even more stuff in `ActiveRecord::Base`, `ApplicationRecord` can
hold all those custom work the apps may need.
Now, we don't wanna encourage all of the application models to inherit
from `ActiveRecord::Base`, but we can encourage all the models that do,
to inherit from `ApplicationRecord`.
Newly generated applications have `app/models/application_record.rb`
present by default. The model generators are smart enough to recognize
that newly generated models have to inherit from `ApplicationRecord`,
but only if it's present.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 15 | ||||
-rw-r--r-- | activerecord/lib/rails/generators/active_record/model/model_generator.rb | 9 |
2 files changed, 23 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 87420a0746..f1921dded8 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,18 @@ +* Introduce ApplicationRecord, an Active Record layer super type. + + An `ApplicationRecord` let's engines have models, isolated from the main + application. Plugin authors can use it to distribute extensions as modules + to be included into `ApplicationRecord`, instead of monkey patches. It can + also serve as a place for applications to customize the default + `ActiveRecord::Base` model behaviour. + + Newly generated applications have `app/models/application_record.rb` + present by default. Generators are smart enough to recognize that + newly generated models have to inherit from `ApplicationRecord` only if + it's present. + + *Genadi Samokovarov* + * Version the API presented to migration classes, so we can change parameter defaults without breaking existing migrations, or forcing them to be rewritten through a deprecation cycle. diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb index 395951ac9d..e4c9539362 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -43,9 +43,16 @@ module ActiveRecord # Used by the migration template to determine the parent name of the model def parent_class_name - options[:parent] || "ActiveRecord::Base" + options[:parent] || determine_default_parent_class end + def determine_default_parent_class + if File.exist?('app/models/application_record.rb') + "ApplicationRecord" + else + "ActiveRecord::Base" + end + end end end end |