SimpleEnum::Persistence
SimpleEnum::Persistence is extension of SimpleEnum, which brings data persistence support to SimpleEnum. Sometimes, we need to use one of our table as enum, in order to add/remove enum type dynamically
Why not ActiveRecord association?
SimpleEnum is a great library, which brings a lot of benefits to create enum-like fields;
SimpleEnum::Persistence cache all enums in memory for better performance, and it will reload when database changes (with
after_savecallback).
ActiveRecord Quick start
Let's say we have a model Tag, we want to use it as enum data, just add this to a model:
class Post
as_enum :tag, Tag, persistence: true
end
Then SimpleEnum will use Tag.all as enum data, tag.name as enum key and tag.id as enum value, if we want to use a field rather than :name as enum key:
class Post
as_enum :tag, Tag, persistence: { key: :name, value: :id }
end
License
The gem is available as open source under the terms of the MIT License.