Skip to content

Add Enum DSL Compiler#12

Draft
coreyja wants to merge 2 commits intoangellist:mainfrom
WellSheet:coreyja/main/active-record-enum-compiler
Draft

Add Enum DSL Compiler#12
coreyja wants to merge 2 commits intoangellist:mainfrom
WellSheet:coreyja/main/active-record-enum-compiler

Conversation

@coreyja
Copy link

@coreyja coreyja commented Jun 12, 2025

Closes #3

As discussed in that issue we at Wellsheet are in the proccess of migrating from sorbet-rails to tapioca dsl. And one issue for us was our heavy use of ActiveRecord enums and the T::Enum classes that SorbetRails was generating passed on those

This PR adds both a new DSL compiler ActiveRecordTypedEnum and a new Railtie EnumsRailtie.

One thing this PR does NOT yet include, but was mentioned on the issue is a way to make this behavior opt-in. It currently follows the pattern of RelationsRailtie which is included by default.
I want to think about the best way to make this opt-in, and thought I'd get this draft up and see if anyone else has ideas as well!

@coreyja coreyja force-pushed the coreyja/main/active-record-enum-compiler branch from cd285a7 to c0d880c Compare June 12, 2025 20:52

# Add typed enum methods for each enum
definitions.each_key do |name|
enum_values = defined_enums.fetch(name.to_s)
Copy link
Author

Choose a reason for hiding this comment

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

We purposefully use the defined_enums method from ActiveRecord, as opposed to using the definitions hash that is passed in

This way we can rely on the normalization that happens when the enums are defined. For instance enum status: [:created, :in_progress] is a valid way to define an enum. And the defined_enums normalizes that so we get a hash like { 'status' => { 'created' => 0, 'in_progress' => 1 } }
Allows us to always expect string keys, and know that we will have the correct values for each variant, even in Rails defined them for the user!

@coreyja coreyja force-pushed the coreyja/main/active-record-enum-compiler branch 4 times, most recently from f293ba6 to 9148a43 Compare June 16, 2025 16:50
@coreyja coreyja force-pushed the coreyja/main/active-record-enum-compiler branch 2 times, most recently from 14d7974 to 1d320c7 Compare June 16, 2025 17:48
@coreyja coreyja force-pushed the coreyja/main/active-record-enum-compiler branch from 1d320c7 to 5c26004 Compare June 16, 2025 19:04
@stathis-alexander
Copy link
Member

Hey @coreyja ,

Thanks a bunch for this contribution. I think there must be a way to set this up to configure which railties get applied, just need to figure out how to do it in an elegant way. I will need to experiment to figure out what that is (not sure if you have an idea here, but I was thinking of something similar to how Datadog instrumentation is configured).

I'm pretty swamped with other work at the moment, but will try to find some time to jam on this in the next week or two

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support creating T::Enum from Rails enums

2 participants