Integrate Mongoid and CanCan
Asked Answered
I

2

5

Have somebody tried to rewrite CanCan ActiverRecordAddtions for Mongoid http://github.com/ryanb/cancan/blob/master/lib/cancan/active_record_additions.rb

Regards, Alexey Zakharov

Isidoro answered 23/9, 2010 at 8:51 Comment(0)
M
9

I've managed to get CanCan and Mongoid (version 2) to work together pretty well on a rails 3 app. Still get some errors here and there related to conditions in the permission definition (the Ability model).

I just put the contents of this gist into a file in config/initializers:

The condition hashes are almost the same as with ActiveRecord:

# can only manage own account
can :manage, User, :_id => current_user.id

I'm still working on how to use more advanced Mongoid::Criteria conditions, but you can always use a block to do more complex conditions:

# can only manage own account
can :eat, Cake do
  current_user.jobs.any?{ |job| job.title == 'Peasant'}
end
Mouthpart answered 24/9, 2010 at 22:49 Comment(6)
Thank you. Hope that you extensions will be added to future version of gemIsidoro
You're welcome. Mongoid is awesome and so is CanCan. I'll try to put together a working patch for CanCan so that this behavior is included in the library without a need to add anything. By the way, I made some significant updates to the gist that allow for complex conditions hashes. I recommend using the latest gist version, because the version I had earlier caused some hard-to-detect but significant bugs.Mouthpart
My extensions made it into Cancan. The latest version on github now supports Mongoid: github.com/ryanb/cancan/pull/172 and version 1.5 from rubygems will have Mongoid support as well.Mouthpart
Looks like CanCan 1.5.0 is released and it is indeed added in it - fantastic :)Hescock
Great, that's perfect :) However I got one question, cancan will generate the ability mode, but what about the role model ? Should that be done manually, or does cancan or devise takes care of it ? I am a bit lost here... thx.Mephistopheles
CanCan does not create the roles, and neither does Devise. You can create your own, or use a gem like github.com/kristianmandrup/roles_generic or github.com/martinrehfeld/role_modelMouthpart
G
-1

I know it's an old one, but for those who search mongoid and cancancan integration, you could try official mongoid adapter

For cacancan gem version >2.0 there's separate gem cancacan-mongoid

Bear in mind that this gem status is "in development", but still it's working pretty well and build passing

Greenfinch answered 13/6, 2018 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.