0

I have application and engines (gems, Rails::Engine). CanCan used for authorization and I want to use it at engines. Engines isolated with namespace, but for example:

module MyEngine
  class ApplicationController < ::ApplicationController
  end
end

So, I can use load_and_authorize_resource at controllers (must specify model class name) and 'can' helper also. All abilities I must write to Ability at main application (models from engines must be namespaced). It is not nice way. I want specify abilities for engine at this engine, but without create new ability object. How can I do this? Any idea?

  • One of solution: make `MyEngine::Ability` class at engine as model (`include ::CanCan::Ability` also) and write abilities for engine here. Than add `before_filter` to `ApplicationController` which make `current_ability.merge(MyEngine::Ability.new(current_user))`. Maybe it is what I want... – Yuriy Kolodovskyy Aug 22 '12 at 01:38

1 Answers1

2

It wasn't super pretty, but I did something similar with a Rails application I'm writing currently. I have written my own engine inside this application called FormX, which is sort of a form CMS engine. I wanted users to have the ability to only edit certain responses to forms, and only some users to have the ability to create forms.

Given that I had a Form model in the Formx namespace, I was able to define abilities in my MainApp ability.rb file by doing using Formx::Form syntax.

Ex.

can :manage, Formx::Form

Then in my controller I had to manually place the authorize! calls in each action, as I couldn't get load_and_authorize_resource to work within the engine's namespace.

Chiubaka
  • 801
  • 2
  • 11
  • 27