Shopify / Shopify/tapioca

Feature request: Support types for polymorphic associations

Open
#2,272 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help-wanted
Dominant language
Ruby
Stars
873
Forks
164
Avg merge
4d 27m
Merged PRs (30d)
9

Description

Take the follow code, which defines a myParent.child relation:

class MyParent < Versions::VersionedApplicationRecord
  VALID_CHILD_TYPES = T.let([MyChild1.polymorphic_name, MyChild2.polymorphic_name].freeze, T::Array[String])

  has_one :child, polymorphic: true
  validates :child_type, presence: true, inclusion: { in: VALID_CHILD_TYPES }
end

In theory, there should be enough information to statically type the child association as T.any(MyChild1, MyChild2). However, what it currently gets typed as is T.untyped. It would be great if this common use-case were supported.

(Note that MyClass1.polymorphic_name is not necessarily the same as MyClass1.name. I believe you can call polymorphic_class_for to map the polymorphic_name to the class instance)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Ruby example and tracing how Tapioca types polymorphic associations. Check how polymorphic_name values map to classes, including polymorphic_class_for, and define done as generating T.any(MyChild1, MyChild2) instead of T.untyped for the association.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.