Setting the parent controller per scope

622 views
Skip to first unread message

Jens Ljungblad

unread,
Sep 9, 2014, 8:09:14 AM9/9/14
to plataforma...@googlegroups.com
We have a scenario in a project where we want to to use devise both in the main app, and inside an engine. Using the information found here: https://github.com/plataformatec/devise/wiki/How-To:-Use-devise-inside-a-mountable-engine this works reasonably well:

Rails.application.routes.draw do
  devise_for :users
  mount Foo::Engine, at: "foo"
end

Foo::Engine.routes.draw do
  devise_for :foo_users, router_name: "foo"
end

However, all Devise controllers inherit from whatever is specified in the shared initializer, i.e. ::ApplicationController. Would it be possible to specify the parent controller per scope somehow? So that Foo::SessionsController inherits from Foo::ApplicationController?


Lucas Mazza

unread,
Sep 9, 2014, 9:30:14 AM9/9/14
to plataforma...@googlegroups.com
It's not possible to specify the parent controller outside the global configuration, so in your case you might have to use custom controllers for your engine (like Foo::SessionsControllers), and share code between that class and the Foo::ApplicationController with a module or something like that.

--

---
You received this message because you are subscribed to the Google Groups "Devise" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jens Ljungblad

unread,
Sep 9, 2014, 11:00:56 AM9/9/14
to plataforma...@googlegroups.com
Unfortunately that still means code from the base ApplicationController is inherited by all the engine DeviseControllers. Code that does things that are not meant to be done in the engine, for instance.

class ApplicationController < ActionController::Base
  # code only applicable to the application
end

class Foo::ApplicationController < ActionController::Base
  # code only applicable to the engine
end

Is it possible to solve this somehow? Or is it something Devise can be modified to support?

You received this message because you are subscribed to a topic in the Google Groups "Devise" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/plataformatec-devise/1ZPdAPpjGAY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to plataformatec-de...@googlegroups.com.

Jens Ljungblad

unread,
Sep 9, 2014, 12:14:08 PM9/9/14
to plataforma...@googlegroups.com
For instance, could the DeviseControllers be modularized and made includable? So that you can choose between inheritance or inclusion.

class SessionsController < Devise::SessionsController
end

class Foo::SessionsController < Foo::ApplicationController
  include Devise::Controllers::Sessions
end

Or something like that.

On the Devise end:

class Devise::SessionsController
  include Devise::Controllers::Sessions
end

module Devise::Controllers::Sessions
  include Devise::Controllers::Base
  def create; end
  def destroy; end
end

etc

Jens Ljungblad

unread,
Sep 12, 2014, 10:09:33 AM9/12/14
to plataforma...@googlegroups.com
Lucas, I did a quick "proof of concept" here: https://github.com/jensljungblad/devise/tree/modularize-controllers

Basically it moves the controller code into modules, so they can be included rather than inherited. It works for my scenario, and I can do:

app/controllers/sessions_controller.rb
class SessionsController < Devise::SessionsController # Business as usual
end

foo/controllers/foo/sessions_controller.rb
class Foo::SessionsController < Foo::ApplicationController # Not inheriting from a devise controller
  include Devise::Controllers::Base
  include Devise::Controllers::Sessions
end

What do you think?

Rick Jones

unread,
Aug 18, 2016, 10:55:26 AM8/18/16
to Devise
I'm having a similar problem separating Devise for different API versions: would like to inherit from different application controllers based on version.

Has Jens' solution been given any further consideration?
Reply all
Reply to author
Forward
0 new messages