0

I have used cancan and rolify in my rails app. Now i want to use these, atleast rolify in my rails mountable engine(isolated namespace). i tried hard but went in vain. I put role model inside models/myengine/ and models/ also when prior fails. It throws error at role adapter level saying has_role? coundn't be found. Thanks in advance.

Kundan Pandit
  • 412
  • 6
  • 17

2 Answers2

2

I had similar problem with rolify. I couldn't make it work inside mountable engine either. It looks like it just doesn't work well with engines. I propose that you use simple_roles gem instead - this is what I have used, and there is no problem with using it inside mountable engine.

UPDATE Rolify added support for namespaces so this is not longer valid. I have successfully used it in my engine. You can now just do rails g rolify EngineName::Role EngineName::User

0

I'm converting a working Rails 4.2. app into an engine. The tables are already created as 'users', 'roles' and 'users_roles'.

To make it work inside the engine, User just needs this customization:

rolify role_cname: 'EngineName::Role', role_join_table_name: 'users_roles'

Testing in the console:

EngineName::User.last.roles
SELECT  "users".* FROM "users"  ORDER BY "users"."id" DESC LIMIT 1
SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1  [["user_id", "xxxx"]]

EngineName::User.last.has_role? :admin
SELECT  "users".* FROM "users"  ORDER BY "users"."id" DESC LIMIT 1
SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", "xxxx"]]