开发者

Integrate Mongoid and CanCan

Have somebody tried to rewrite CanCan ActiverRecordAddtions for Mongoid http://github.com/ryanb/cancan/blob/master/lib/cancan/active_record_additions开发者_如何学C.rb

Regards, Alexey Zakharov


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:

  • http://gist.github.com/561639

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜