role model is a gem that lets you add roles to the user as a method and
saves the role into a field on the user table as a bitmask
that is , lets say you have and array like this
ROLES = %w[admin moderator author banned]
this gord in your user model and you have a field in the user table
called roles_mask
as you see you can map the roles in the array as a series of bit
[admin moderator author banned]
0 0 0 0
if a user is admin the in his roles_mask field is this
1000
in binary which translate to 8 in decimal format, if the user is
admin and moderator the value is
1100
and that is a 12
As you can see there is no reason to have a roles table in the proyect
unless you have
like 20 or more roles.
role model saves the roles with a virtual attribute, roles that is in
you user model and is looks like this
def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
end
as you can see it maps the array in ROLES to a binary array and saves
it in the roles_mask field of the users table
to read a role it does this
def roles
ROLES.reject do |r|
((roles_mask || 0) & 2**ROLES.index(r)).zero?
end
end
this returns an array of strings when ever it finds a 1 in the binary
representation of the string array that is saved on the
roles_mask field
On Tue, Sep 14, 2010 at 11:47 AM, Daniel Gaytán <
[email protected]> wrote:
> http://github.com/martinrehfeld/role_model
>
> Enjoy!
>
> Daniel Gaytán
>
>
> 2010/9/14 nobosh <[email protected]>
>
> Thanks for the reply. In the example above I'm not sure where the
>> Admin variable is located? Is that in the User's table as a boolean?
>>
>> The reason for a roles table is that there will be multiple roles
>> (Admin, Member, Viewer, Guest) etc...
>>
>> I tried finding the role model gem but it didn't come up in a Google
>> Search and I'm curious to learn more, do you have a URL?
>>
>> Thanks
>>
>> On Sep 14, 5:02 am, radhames brito <[email protected]> wrote:
>> > with cancan, in your ability model
>> >
>> > can :manage , Proyect do |proyect|
>> > proyect.user = current_user
>> > end
>> >
>> > with this only the proyect's owner is an admin of the proyect
>> >
>> > and i suggest you create model with the role model gem, there really is
>> no
>> > need for a role table.
>> >
>> >
>> >
>> > On Tue, Sep 14, 2010 at 12:28 AM, nobosh <[email protected]> wrote:
>> > > Hello... Currently I'm using Devise & CanCan which allows me to create
>> > > Users with Roles using a (Roles_Users) table.
>> >
>> > > That's nice, but what I want is to Have Projects in my app and for
>> > > each project for a user to possibly have a role like (Admin, Viewer,
>> > > etc)
>> >
>> > > What kind of Model would work for this?
>> >
>> > > Models
>> > > Users
>> > > has_many: projects
>> >
>> > > Projects
>> > > ?
>> >
>> > > Roles
>> > > ?
>> >
>> > > Users_Roles_Projects (user_id, project_id, role_id)
>> >
>> > > What do you think? I'm a newbie and could use the understanding and
>> > > thinking from you fine experienced folks. Thanks!
>> >
>> > > --
>> > > You received this message because you are subscribed to the Google
>> Groups
>> > > "Ruby on Rails: Talk" group.
>> > > To post to this group, send email to
>> [email protected].
>> > > To unsubscribe from this group, send email to
>> > > [email protected]<rubyonrails-talk%[email protected]><rubyonrails-talk%2Bunsubscrib
>> [email protected]>
>> > > .
>> > > For more options, visit this group at
>> > >http://groups.google.com/group/rubyonrails-talk?hl=en.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby on Rails: Talk" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<rubyonrails-talk%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/rubyonrails-talk?hl=en.
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rubyonrails-talk%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.