Make the rails enum integration aware of the original type - #311
Draft
willcosgrove wants to merge 1 commit into
Draft
willcosgrove wants to merge 1 commit into
willcosgrove wants to merge 1 commit into
Conversation
Contributor
|
That's awesome! would be super awesome if class MyEnum < Literal::Enum(Integer)
Foo = new(1)
Bar = new(2)
end
class Example < ApplicationRecord
literal_enum :x, MyEnum, suffix: true, validate: { allow_nil: true }
endMy current problem is that, when I use something like: def literal_enum(name, literal_enum, **)
mapping = literal_enum.to_h do |o|
[literal_enum.names.fetch(o).to_s.underscore, o.value]
end
enum(name, mapping, **)
endI get Rails enum good parts like: |
joeldrapper
reviewed
Apr 17, 2025
|
|
||
| class Literal::Rails::EnumType < ActiveModel::Type::Value | ||
| def initialize(enum) | ||
| def initialize(enum, subtype) |
Collaborator
There was a problem hiding this comment.
I think this is the supertype not the subtype.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change makes the
:literal_enumattribute type, type-aware of the attribute that it is replacing.Additionally it adds a
literal_enummacro toActiveRecord::Basethat delegates toattributeand figures out the underlying attribute type.My concern here is with calling
type_for_attributeat class evaluation time is going to trigger a connection to the DB. This change broke my reproduction script because I created the database after defining the model. But this change makes the model definition require the database to be set up already.Unfortunately I don't think we can lazy evaluate the
type_for_attributebecause after the call toattributeit's type will have changed to:literal_enum.