literal
literal copied to clipboard
WIP Rails support
Closes #44
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 can we implement this in a way that doesn't depend on the PG gem? Can we just use Ruby arrays?
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.