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
Define alias attribute methods in define_attribute_methods
#49173
Define alias attribute methods in define_attribute_methods
#49173
Conversation
a2c8e29
to
3eae3db
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.
Some minor concern over that new attribute, but once this is cleared 👍
self.attribute_aliases = attribute_aliases.merge(new_name => old_name) | ||
self.local_attribute_aliases = local_attribute_aliases.merge(new_name => old_name) | ||
self.aliases_by_attribute_name[old_name] = aliases_by_attribute_name[old_name] + [new_name] |
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.
attribute_aliases
is a class_attribute, so it's inherited and it makes sense to re-assign it.
However local_attribute_aliases
and aliases_by_attribute_name
are regular instance variables, so you can actually mutate them without issue.
self.attribute_aliases = attribute_aliases.merge(new_name => old_name) | |
self.local_attribute_aliases = local_attribute_aliases.merge(new_name => old_name) | |
self.aliases_by_attribute_name[old_name] = aliases_by_attribute_name[old_name] + [new_name] | |
self.attribute_aliases = attribute_aliases.merge(new_name => old_name) | |
self.local_attribute_aliases[new_name] = old_name | |
self.aliases_by_attribute_name[old_name] << new_name |
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.
Also I wonder if we really need to keep both. I see local_attribute_aliases
is used here:
rails/activerecord/lib/active_record/attribute_methods.rb
Lines 74 to 76 in 9044d35
local_attribute_aliases.each do |new_name, old_name| | |
generate_alias_attribute_methods(code_generator, new_name, old_name) | |
end |
aliases_by_attribute_name
for that (or the opposite, what I'm trying to say is that it seem to me that we don't actually need both).
3eae3db
to
248acb9
Compare
`undefine_attribute_methods` now removes alias attribute methods along with attribute methods. This commit changes `define_attribute_methods` to redefine methods back if any alias attributes were declared which provides applications and libraries an option to bring the alias methods back after using `undefine_attribute_methods`.
248acb9
to
0f5563b
Compare
Fixes #48931
undefine_attribute_methods
now removes alias attribute methods along with attribute methods. This commit changesdefine_attribute_methods
to redefine methods back if any alias attributes were declared which provides applications and libraries an option to bring the alias methods back after usingundefine_attribute_methods
.Implementation details
In addition to existing
alias_attribute
internal lists we will populate one more -aliases_by_attribute_name
which will keep a list of alias attributes per attribute name. We will use this list in thedefine_attribute_methods
to restore aliases any time the method is being redefined. Basically it makesdefine_attribute_methods
responsible for defining both alias attribute methods and actual attribute methods for most cases apart from when a non-attribute thing is being aliased.