Support methods added by ActiveRecord delegated types
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 873
- Forks
- 164
- Avg merge
- 4d 27m
- Merged PRs (30d)
- 9
Description
One of the apps I work on uses delegated types ( https://api.rubyonrails.org/classes/ActiveRecord/DelegatedType.html ).
Given
class Entry < ApplicationRecord
delegated_type :entryable, types: %w[ Message Comment ]
end
then in addition to the underlying association methods, rails defines (this is just from the docs)
Entry#entryable_class # => +Message+ or +Comment+
Entry#entryable_name # => "message" or "comment"
Entry.messages # => Entry.where(entryable_type: "Message")
Entry#message? # => true when entryable_type == "Message"
Entry#message # => returns the message record, when entryable_type == "Message", otherwise nil
Entry#message_id # => returns entryable_id, when entryable_type == "Message", otherwise nil
Entry.comments # => Entry.where(entryable_type: "Comment")
Entry#comment? # => true when entryable_type == "Comment"
Entry#comment # => returns the comment record, when entryable_type == "Comment", otherwise nil
Entry#comment_id # => returns entryable_id, when entryable_type == "Comment", otherwise nil
I'd like it if tapioca could include these methods in generated rbis & thought I would have a go at adding support for this, however unlike associations, rails doesn't retain any memory of how it defined those methods - it just defines them (see implementation https://github.com/rails/rails/blob/main/activerecord/lib/active_record/delegated_type.rb#L206) - there's no state you can iterate over like you can with associations.
Is there a suggested approach for handling cases like this? I saw that the compiled for frozen record overwrites a method from that library (https://github.com/Shopify/tapioca/blob/main/lib/tapioca/dsl/extensions/frozen_record.rb ) in order to stash some information for tapioca - would something like that be acceptable?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading Active Record's implementation in activerecord/lib/active_record/delegated_type.rb, especially the linked method-definition section, and compare it with tapioca/lib/tapioca/dsl/extensions/frozen_record.rb. Determine how delegated types can be discovered for RBI generation and whether the generated output should include the listed methods. Done means delegated-type methods appear correctly in generated RBIs without relying on unavailable association state.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rails, ruby
- Domain
- devtools, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100