7

I'm using cancan and cells gems in my ruby-on-rails project. How to access can? method from within cell? Thanks.

jigfox
  • 18,057
  • 3
  • 60
  • 73

2 Answers2

12

I've had to do exactly this. Try

class MyCell < Cell::Rails

  include CanCan::ControllerAdditions

end                                        

If you're also using Devise, I had to do this:

class MyCell < Cell::Rails

  include CanCan::ControllerAdditions
  include Devise::Controllers::Helpers
  Devise::Controllers::Helpers.define_helpers(Devise::Mapping.new(:user, {}))

end                                        

#define_helpers will add helper methods such as current_user and user_signed_in? to the cell.

qnm
  • 521
  • 3
  • 14
  • hi qnm! what is the last `define_helpers` part needed for? – dgilperez Mar 20 '12 at 00:37
  • @dgilperez Take a look at [the code](https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb#L42) - you'll see this brings the devise helpers - current_user / user_signed_in? etc. into the cell as helpers. – qnm Oct 12 '12 at 09:46
4

For those who happen to have a custom current_ability() method (in which you can change the name of current_user method and Ability class names):

class OrderCell < Cell::Rails
  include CanCan::ControllerAdditions
  delegate :current_ability, :to => :controller
end
lulalala
  • 17,572
  • 15
  • 110
  • 169