Authorize allowed values with assignable_values

Updated . Posted . Visible to the public.

All our projects have enum-like requirements like this:

  • An attribute value must be included in a given set of values.
  • The list of allowed values must be retrievable in order to render <select> boxes.
  • Each value has a humanized label.
  • Sometimes there is a default value.

Most of the time, this requirement is also needed:

  • The list of assignable values depends on the user who is currently signed in.

In our past projects there are many different solutions for these related requirements, e.g. ChoiceTrait, methods like available_states, helper methods to render option tags, and so on. Unfortunately each of these solutions is limited in its own way, so it was time to come up with a standard solution.

assignable_values is the new way

We have now updated/created two gems that implement the requirements above using the best practices we know of:

This is the way we want to implement the requirements above in most of our future projects. You should migrate existing projects to assignable_values if you run into the limits of our previous approaches (e.g. ChoiceTrait cannot be internationalized).

Please read the assignable_values README Show archive.org snapshot to familiarize yourself with the new solution.

Also see our instructions on how to migrate legacy approaches to assignable_values.

Testing

There is a custom RSpec matcher.

Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2012-04-30 11:46)