literal icon indicating copy to clipboard operation
literal copied to clipboard

WIP Rails support

Open joeldrapper opened this issue 1 year ago • 3 comments

Closes #44

joeldrapper avatar Jun 24 '24 11:06 joeldrapper

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
...

stevegeek avatar Jun 30 '24 07:06 stevegeek

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

joeldrapper avatar Jul 01 '24 00:07 joeldrapper

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 avatar Jul 01 '24 01:07 joeldrapper