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

WIP Rails support #90

Closed
wants to merge 3 commits into from
Closed

WIP Rails support #90

wants to merge 3 commits into from

Conversation

joeldrapper
Copy link
Owner

@joeldrapper joeldrapper commented Jun 24, 2024

Closes #44

@stevegeek
Copy link
Collaborator

Just playing with Enums in AR, if a project depends on pg then something like this is useful:

class LiteralEnumInArrayType < Literal::Rails::EnumType
  def deserialize(values)
    PG::TextDecoder::Array.new.decode(values).map { super(_1) }
  end

  def serialize(values)
    PG::TextEncoder::Array.new.encode(values.map { super(_1) })
  end
end

so you can do

class MyModel < ApplicationRecord
  attribute :my_things, LiteralEnumInArrayType.new(MyEnum), array: true
...

@joeldrapper
Copy link
Owner Author

@stevegeek can we implement this in a way that doesn't depend on the PG gem? Can we just use Ruby arrays?

@joeldrapper
Copy link
Owner Author

Also, can you think of a way to make this prettier?

attribute :status, Literal::Rails::EnumType.new(Status)

Ideally, we could do something like this:

attribute :status, Status

That would require Rails to have some sort of to_active_model_type interface we could implement on enum classes.

@joeldrapper joeldrapper closed this Oct 1, 2024
@joeldrapper joeldrapper deleted the rails branch October 1, 2024 11:05
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.

Rails support
2 participants