Skip to content

Commit

Permalink
Replace alias_attribute with define_method
Browse files Browse the repository at this point in the history
Rails 7.1 lazily defines methods.  These are associations, not attributes, so we can't
use alias_attribute.

Fixes the following error with Rails 7.1:

Association named 'provisioning_manager' was not found on CustomizationScript
  • Loading branch information
jrafanie committed Feb 11, 2025
1 parent c1c5687 commit 0aaed7c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 13 deletions.
4 changes: 3 additions & 1 deletion app/models/mixins/deprecation_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def deprecate_attribute(old_attribute, new_attribute, type:)
end

def deprecate_attribute_methods(old_attribute, new_attribute)
alias_attribute old_attribute, new_attribute
define_method(old_attribute) { self.send(new_attribute) }
define_method("#{old_attribute}=") { |object| self.send("#{new_attribute}=", object) }
define_method("#{old_attribute}?") { self.send("#{new_attribute}?") }
["", "=", "?"].each { |suffix| Vmdb::Deprecation.deprecate_methods(self, "#{old_attribute}#{suffix}" => "#{new_attribute}#{suffix}") }
end
end
Expand Down
12 changes: 0 additions & 12 deletions spec/models/mixins/deprecation_mixin_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
RSpec.describe DeprecationMixin do
# Host.deprecate_attribute :address, :hostname
context ".arel_table" do
# this is defining an alias
# it is not typical for aliases to work through arel_table
# may need to get rid of this in the future
it "works for deprecate_attribute columns" do
expect(Host.attribute_supported_by_sql?(:address)).to eq(true)
expect(Host.arel_table[:address]).to_not be_nil
expect(Host.arel_table[:address].name).to eq("hostname") # typically this is a symbol. not perfect but it works
end
end

# Host.deprecate_attribute :address, :hostname
context ".visible_attribute_names" do
it "hides deprecate_attribute columns" do
Expand Down

0 comments on commit 0aaed7c

Please sign in to comment.