Skip to content
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

Rails 7.1 friendly attribute changes #23337

Merged
merged 3 commits into from
Feb 18, 2025

Conversation

jrafanie
Copy link
Member

@jrafanie jrafanie commented Feb 17, 2025

Extracted from #23225 to make it easier to review both this and the rails 7.1 PR separately.

@jrafanie
Copy link
Member Author

@miq-bot cross-repo-tests /all

miq-bot pushed a commit to ManageIQ/manageiq-cross_repo-tests that referenced this pull request Feb 17, 2025
@jrafanie jrafanie mentioned this pull request Feb 17, 2025
56 tasks
# 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)
Copy link
Member Author

@jrafanie jrafanie Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This arel_table context was testing the internals. We're no longer using virtual columns or alias_attribute as someone could use deprecate_attribute on associations or attributes. The former won't work in rails 7.1 without warnings and will not work with virtual columns. It's much simpler to handle attributes or associations through runtime delegations although there is some performance implications we're accepting for this flexibility. We can bring that back in a different way if we need it.

@@ -52,7 +52,6 @@ class TenantQuota < ApplicationRecord
scope :templates_allocated, -> { where(:name => :templates_allocated) }

virtual_column :name, :type => :string
virtual_column :total, :type => :integer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handled by alias_attribute on line 61 👇

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}?") }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handles non-attribute calls to deprecate_attribute

@@ -51,7 +51,6 @@ class MiqRequest < ApplicationRecord
virtual_column :v_workflow_class, :type => :string, :uses => :workflow
virtual_column :request_type_display, :type => :string
virtual_column :resource_type, :type => :string
virtual_column :state, :type => :string
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handled by alias_attribute on line 23 ☝️

Fixes deprecation of this variety:

Foo model aliases `x`, but `x` is not an attribute. Starting in Rails 7.2, alias_attribute with non-attribute targets will raise. Use `alias_method :y, :x` or define the method manually.

This also handles aliases on association methods

We use runtime method delegation instead of class load time alias_method.
In Rails 7.1, the attribute/association methods aren't defined at class load time so we can't alias
them.

Note, we can remove virtual_column/attribute on actual attributes with alias_attribute due to:
ManageIQ#23324
ManageIQ/manageiq-automation_engine#565
ManageIQ/manageiq-api#1278
ManageIQ#23321
Note, rails 7.1 lazily load attribute methods so we can't use alias_method here.
Rails 7.1 lazily defines methods.  These are sometimes called with 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
@jrafanie jrafanie force-pushed the rails71-friendly-attribute-changes branch from 2a8b3e1 to f34896c Compare February 17, 2025 20:23
@jrafanie
Copy link
Member Author

@kbrock I extracted the attribute changes from #23225 and did a cross repo against rails 7.0 to ensure all of our changes work correctly with rails 7.0. This should make it easier to review this PR and the rails 7.1 one separately.

@jrafanie jrafanie closed this Feb 17, 2025
@jrafanie jrafanie reopened this Feb 17, 2025
@jrafanie
Copy link
Member Author

bumping after ManageIQ/manageiq-ui-classic#9351 merge

@jrafanie jrafanie closed this Feb 18, 2025
@jrafanie jrafanie reopened this Feb 18, 2025
@jrafanie
Copy link
Member Author

@kbrock this is green now along along with the cross repo

Comment on lines -19 to -20
alias_attribute :miq_provision_request, :miq_request # Legacy provisioning support
alias_attribute :provision_type, :request_type # Legacy provisioning support
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to keep these comments?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add them to each method if it makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was one method that lost it: #23338

@kbrock kbrock merged commit 1613b01 into ManageIQ:master Feb 18, 2025
12 of 20 checks passed
@kbrock kbrock deleted the rails71-friendly-attribute-changes branch February 18, 2025 18:23
jrafanie added a commit to jrafanie/manageiq that referenced this pull request Feb 19, 2025
jrafanie added a commit to jrafanie/manageiq that referenced this pull request Feb 19, 2025
Fryguy added a commit that referenced this pull request Feb 19, 2025
…attribute-changes

Revive a comment accidentally dropped in #23337
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants