If you've ever found yourself in a situation where you feel like you should use enum, but it has 50 positions so you don't know where to put it, the answer is here!
Just store your enums in a .yml file!
This makes little sense when you have a small enum with just a couple of options, but the more options you have, the easier it is to see the benefits of using it.
- Create a folder
/config/enums
to store your enum files in .yml format. - Make sure your enum column has a type
string
.
Et voila!
Given a file /config/enums/eye_colors.yml
you can add eye_color enum to a Cat model with the following line:
enum :eye_color, from_file: :eye_colors
You can continue using regular syntax for regular cases:
enum status: { active: "active", archived: "archived" }
All the options will be passed to native enum implementation as expected.
You can structure your .yml file either as a hash
blue: blue
copper: copper
yellow: yellow
Or as a list
gold
orange
green
red
The list will be converted automatically, so both structures yield the same result.
Add this line to your application's Gemfile:
gem "enum_from_file"
And then execute:
$ bundle
Bug reports and pull requests are welcome on project's page at GitHub
The gem is available as open source under the terms of the MIT License.