From b0eab561b24b14cd3e15a88f4aa46047884b6456 Mon Sep 17 00:00:00 2001 From: Matthew Arkin Date: Wed, 14 Aug 2024 18:49:32 +0000 Subject: [PATCH] Remove polymorphic check when generating associating methods 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. --- .../compilers/active_record_associations.rb | 24 ++----------------- .../active_record_associations_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/lib/tapioca/dsl/compilers/active_record_associations.rb b/lib/tapioca/dsl/compilers/active_record_associations.rb index c214cf36e..c76effd85 100644 --- a/lib/tapioca/dsl/compilers/active_record_associations.rb +++ b/lib/tapioca/dsl/compilers/active_record_associations.rb @@ -287,7 +287,7 @@ def populate_collection_assoc_getter_setter(klass, association_name, reflection) def type_for(reflection) validate_reflection!(reflection) - return "T.untyped" if !constant.table_exists? || polymorphic_association?(reflection) + return "T.untyped" if !constant.table_exists? T.must(qualified_name_of(reflection.klass)) end @@ -383,33 +383,13 @@ def relation_type_for(reflection) validate_reflection!(reflection) relations_enabled = compiler_enabled?("ActiveRecordRelations") - polymorphic_association = !constant.table_exists? || polymorphic_association?(reflection) if relations_enabled - if polymorphic_association - "ActiveRecord::Associations::CollectionProxy" - else - "#{qualified_name_of(reflection.klass)}::#{AssociationsCollectionProxyClassName}" - end - elsif polymorphic_association - "ActiveRecord::Associations::CollectionProxy[T.untyped]" + "#{qualified_name_of(reflection.klass)}::#{AssociationsCollectionProxyClassName}" else "::ActiveRecord::Associations::CollectionProxy[#{qualified_name_of(reflection.klass)}]" end end - - sig do - params( - reflection: ReflectionType, - ).returns(T::Boolean) - end - def polymorphic_association?(reflection) - if reflection.through_reflection? - polymorphic_association?(reflection.source_reflection) - else - !!reflection.polymorphic? - end - end end end end diff --git a/spec/tapioca/dsl/compilers/active_record_associations_spec.rb b/spec/tapioca/dsl/compilers/active_record_associations_spec.rb index b960275d6..2e21af863 100644 --- a/spec/tapioca/dsl/compilers/active_record_associations_spec.rb +++ b/spec/tapioca/dsl/compilers/active_record_associations_spec.rb @@ -265,7 +265,7 @@ def picture_ids=(ids); end # This method is created by ActiveRecord on the `Employee` class because it declared `has_many :pictures`. # 🔗 [Rails guide for `has_many` association](https://guides.rubyonrails.org/association_basics.html#the-has-many-association) - sig { returns(ActiveRecord::Associations::CollectionProxy) } + sig { returns(::Picture::PrivateCollectionProxy) } def pictures; end sig { params(value: T::Enumerable[T.untyped]).void } @@ -1036,7 +1036,7 @@ def picture_ids=(ids); end # This method is created by ActiveRecord on the `Employee` class because it declared `has_many :pictures`. # 🔗 [Rails guide for `has_many` association](https://guides.rubyonrails.org/association_basics.html#the-has-many-association) - sig { returns(ActiveRecord::Associations::CollectionProxy[T.untyped]) } + sig { returns(::ActiveRecord::Associations::CollectionProxy[::Picture]) } def pictures; end sig { params(value: T::Enumerable[T.untyped]).void }