Remove polymorphic check when generating associating methods#1990
Open
matthewarkin wants to merge 1 commit intoShopify:mainfrom
Open
Remove polymorphic check when generating associating methods#1990matthewarkin wants to merge 1 commit intoShopify:mainfrom
matthewarkin wants to merge 1 commit intoShopify:mainfrom
Conversation
When doing a has_many with a polymorphic association, these were previously a generic/untyped collectionproxy. I'm not actually sure that needs to be the case since it appears that you should be getting a CollectionProxy of the model of the association.
Author
|
I have signed the CLA! |
amomchilov
reviewed
Aug 30, 2024
Contributor
amomchilov
left a comment
There was a problem hiding this comment.
Hey @matthewarkin, thanks for your first contribution!
We'd be happy to merge this in, but there's an error in our CI that I don't have time to troubleshoot and fix myself. Could you take a look?
| return "T.untyped" if !constant.table_exists? || polymorphic_association?(reflection) | ||
| return "T.untyped" if !constant.table_exists? | ||
|
|
||
| T.must(qualified_name_of(reflection.klass)) |
Contributor
There was a problem hiding this comment.
Hmmm, I'm getting a failure when running against our core monolith:
Error: `Tapioca::Dsl::Compilers::ActiveRecordAssociations` failed to generate RBI for `OurModel`
--
| /tmp/bundle/ruby/3.4.0+0/bundler/gems/rails-f0fd6ab1259b/activerecord/lib/active_record/reflection.rb:500:in 'ActiveRecord::Reflection::AssociationReflection#compute_class': Polymorphic associations do not support computing the class. (ArgumentError)
|
| raise ArgumentError, "Polymorphic associations do not support computing the class."
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| from /tmp/bundle/ruby/3.4.0+0/bundler/gems/rails-f0fd6ab1259b/activerecord/lib/active_record/reflection.rb:439:in 'ActiveRecord::Reflection::MacroReflection#_klass'
| from /tmp/bundle/ruby/3.4.0+0/bundler/gems/rails-f0fd6ab1259b/activerecord/lib/active_record/reflection.rb:431:in 'ActiveRecord::Reflection::MacroReflection#klass'
| from /tmp/bundle/ruby/3.4.0+0/bundler/gems/tapioca-b0eab561b24b/lib/tapioca/dsl/compilers/active_record_associations.rb:292:in 'Tapioca::Dsl::Compilers::ActiveRecordAssociations#type_for'
Where OurModel looks something like this, with a polymorphic belongs_to:
class OurModel < ApplicationRecord
belongs_to :reference, polymorphic: true
end
Author
There was a problem hiding this comment.
Awesome, this is super useful, I can work on this
amomchilov
reviewed
Aug 30, 2024
| validate_reflection!(reflection) | ||
|
|
||
| return "T.untyped" if !constant.table_exists? || polymorphic_association?(reflection) | ||
| return "T.untyped" if !constant.table_exists? |
Contributor
There was a problem hiding this comment.
Lint fix:
Suggested change
| return "T.untyped" if !constant.table_exists? | |
| return "T.untyped" unless constant.table_exists? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
We noticed when adding some typing to our code that association methods for polymorphic associations were returning a generic
ActiveRecord::Associations::CollectionProxyinstead of theModel::PrivateCollectionProxythat is used elsewhere in tapioca.Implementation
I believe checking for the polymorphicness of the association is actually unnecessary since it shouldn't ever be the case that the association for a
has_manywould return a collection of different types.Tests
I updated the existing tests to reflect the updated behavior. I don't think I'm missing a test case but thats mostly because I can't think of a case where you woudn't be able to pin the method to a specific model/type.