Skip to content

Commit

Permalink
Remove polymorphic check when generating associating methods
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matthewarkin committed Aug 14, 2024
1 parent df6e272 commit b0eab56
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 deletions.
24 changes: 2 additions & 22 deletions lib/tapioca/dsl/compilers/active_record_associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/tapioca/dsl/compilers/active_record_associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 }
Expand Down

0 comments on commit b0eab56

Please sign in to comment.