Easy Roles Gem

Easy Roles Gem

Easy Roles is a simple rails gem for basic role authorization with ruby on rails. You can install the gem and after that you have the possibility to use it with the "Serialize" or the "Bitmask" Method. The exact difference between this two methods are explained on github. You can find the source code and exact install instructions here: [LINK]


How to use it in some other way:

In one of my last recent projects I did not use this gem for basic role authorization. I just used it to simulate a model with an has_many and belongs_to association.


Example: You have a little shop system and your shops are different and you need to categorise them into three to four different categories/types of shops. Now you can create a model "category" and a model "shop" and create a has_many and belongs_to association between these two models. Or you can resolve this problem with the use of table inheritance for your shop model with different shop types. But then you have to create a new model class for each shop category/type, that is not convenient.


This is where the easy roles gem comes in. Just add the different type into the "roles" array in your shop model and you have your categories. Now you can easily add and remove "roles" - in this case "types" - from your shops and you are finished. You have one simple model and the possibility to categorize them in an easy and very convenient way. :-)


So this blogpost is just a little hint for you, do not think complicated every time :-). Of course this solution just works for a little number of categories/types of shops. If you are building an application with hundreds of categories/types and this categories/types must be editable, then maybe it is better to use a normal 1:n association. But sometimes the "easy_role" solution is a very good choice!

Matthias Frick
Ruby on Rails
1 Kommentar

Über den Autor

Matthias Frick
Matthias Frick, MSc.

Er ist ein langjähriger Ruby-on-Rails Entwickler und leitet das Unternehmen Frick-Web.

1 Kommentar zu "Easy Roles Gem"

  1. Gordron Wallis
    Gordron Wallis 17.07.2012
    Nice to see an expert at work! Thanks for this hint. Probably it is not good for big system architectures, but I´d give it a chance once I think.

Kommentar verfassen