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

Remove polymorphic check when generating associating methods #1990

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?
Copy link
Contributor

Choose a reason for hiding this comment

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

Lint fix:

Suggested change
return "T.untyped" if !constant.table_exists?
return "T.untyped" unless constant.table_exists?


T.must(qualified_name_of(reflection.klass))
Copy link
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Author

Choose a reason for hiding this comment

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

Awesome, this is super useful, I can work on this

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
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
Loading