views:

85

answers:

1

Hi guys,

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

+1  A: 

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
bowsersenior
Thank you. Hope that you extensions will be added to future version of gem
Alexey Zakharov
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.
bowsersenior