Give GeneratedAttributeMethods module a name #35595
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Currently, inspecting the ancestors of an AR class gives you something like this:
The second and fifth objects that appear in this list (after the model class itself) are instances of the
ActiveRecord::AttributeMethods::GeneratedAttributeMethods
module builder, included into every AR class to hold the class' attribute methods. These are extremely important modules for AR classes, probably the most important type of module in their ancestors, and they appear very prominently at the top of this list. But in their current form, most Rails developers probably have no idea what they are actually doing or why they are even there.This is not helped by the fact that the generated association methods module is given its own name under the model namespace, in the case above,
User::GeneratedAssociationMethods
.This PR brings some consistency and clarity to this situation by creating a constant under the model namespace to capture these generated attribute methods, so that the ancestor list above becomes:
Now, rather than an instance of a module builder class, the AR model gets a (private) named module
User::GeneratedAttributeMethods
, which is much more readable and approachable than before, and which follows the same pattern asUser::GeneratedAssociationMethods
. Also, since this applies to all AR models, the parent class' attribute methods also appear in this form asApplicationRecord::GeneratedAttributeMethods
, and we eliminate all anonymous instances in model ancestors.Other Information
In addition to the change above, I have also renamed the previously-named
ActiveRecord::AttributeMethods::GeneratedAttributeMethods
toActiveRecord::AttributeMethods::GeneratedAttributeMethodsBuilder
, to emphasize that this is not a module but a module builder (class). Currently this is a point of inconsistency between generated association methods and generated attribute methods, which surely causes additional confusion to developers trying to understand how these modules work internally.