Read more

Authorize allowed values with assignable_values

Henning Koch
April 30, 2012Software engineer at makandra GmbH

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.
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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.

Posted by Henning Koch to makandra dev (2012-04-30 13:46)