I’m thinking about a Pull Request for Rails. But, I want to validate with you.
Why not put Enum into ActiveModel? We may need some enumeration into another model without ActiveRecord, a example: something like a mail object form with a enumeration for subject… and other situations.
I think that as ActiveModel holds the attributes, it can hold the enums. Enums are like attributes, but with a pre-defined collection of values( enum is a special kind of attribute). And not only ActiveRecord objects can have enumerations. I think that this is natural of a model and not only of a record of database.
With enum attribute in ActiveModel, we can take advantage of the same API of enums for create objects like Form Objects, ValueObjects(ActiveModel) and etc … With this, we can extend the use of enums`s API.
That way, we can have a conversion for the translations(humanization) of enumerations:
A example of a YML file with the enum values translations that we could access by example a method Employee.type.human:
A method called Employee.type.collection could return the collection with translated humanized values, that can be used by a select box in a form.
Why not put Enum into ActiveModel? We may need some enumeration into another model without ActiveRecord, a example: something like a mail object form with a enumeration for subject…
If the object isn’t persisted to the DB, it doesn’t seem like you’d need to use AR::Enum — the primary benefit is that it maps the options to integers in the DB column. For a non-persisted model, you can just use a regular attribute with an inclusion validation like Rafael mentioned.
By what you described I believe you can reach with validates_inclusion_of and a hash.
Note that AR::Enum doesn’t play nicely with inclusion validations — see https://github.com/rails/rails/issues/13971 — so this only applies to the provided workaround for a non-persisted object.