Controller Inheritance


#1

Is there any known problem in inheriting a Controller from another
controller (that is to say having an Admin::PostController <
PostController, with PostController < ApplicationController)?

It is something I haven’t seen done in tutorials or books, however in my
opinion (and as far as I can see from my tests) it should work.


#2

Riko removed_email_address@domain.invalid writes:

Is there any known problem in inheriting a Controller from another
controller (that is to say having an Admin::PostController <
PostController, with PostController < ApplicationController)?

I am using inherited controllers, and I feel it made my code a lot
DRYier. Haven’t had any problem yet.

It is something I haven’t seen done in tutorials or books, however in
my opinion (and as far as I can see from my tests) it should work.


Surendra S.
http://ssinghi.kreeti.com, http://www.kreeti.com
Read my blog at: http://cuttingtheredtape.blogspot.com/
,----
| “War is Peace! Freedom is Slavery! Ignorance is Strength!”
| – Orwell, 1984, 1948
`----


#3

As far as I know there’s no known problems…but I haven’t searched
the bug list. I only say that because I’m using an inheritance scheme
close to what you’re doing. Except I’ve named my classes a little
differently.

I have mine set up (using your controller names) like this :
class AdminPostController < PostController
…class methods…
end

class PostController < ApplicationController
…class methods…
end

and it works fine. Is the scope operator (::slight_smile: absolutely necessary in
your case?


#4

Riko wrote:

Is there any known problem in inheriting a Controller from another
controller (that is to say having an Admin::PostController <
PostController, with PostController < ApplicationController)?

It is something I haven’t seen done in tutorials or books, however in my
opinion (and as far as I can see from my tests) it should work.

Controller inheritance works fine, and is a good way to share code among
related controllers. For instance, putting your user authentication
filter in a superclass controller so all the subclasses are protected.

The only major caveat I’ve seen is that if you inherit an action from a
superclass controller that renders a view, you have to create a view
template for it in the subclass controller too, since ActionController
only looks for the view template in the class of the controller
instance, not by searching the views of the superclasses.


Josh S.
http://blog.hasmanythrough.com


#5

Surendra S. wrote:

I am using inherited controllers, and I feel it made my code a lot
DRYier. Haven’t had any problem yet.

Good. That’s what I wanted to hear.


#6

Josh S. wrote:

Controller inheritance works fine, and is a good way to share code
among
related controllers. For instance, putting your user authentication
filter in a superclass controller so all the subclasses are
protected.

I took some time to realize it. In fact if I were in a more traditional
framework (that is to say one where I had to write a lot more code), I’d
probably used inheritance from the beginning.

Rails is new to me, and I try to do things in “the right way”. So I
didn’t think to inheritance, since I hadn’t seen it. But actually it
seems a widespread tecnique.

The only major caveat I’ve seen is that if you inherit an action from
a
superclass controller that renders a view, you have to create a view
template for it in the subclass controller too, since
ActionController
only looks for the view template in the class of the controller
instance, not by searching the views of the superclasses.

I noticed it. I think I’ll have something like

        +------------+
        |   Post     |
        | Controller |
        +------------+
        /            \
+------------+     +------------+
| Admin::Post|     | Pub::Post  |
| Controller |     | Controller |
+------------+     +------------+

where the views exist only for the subclasses (along with the layouts)
and PostController has only some partials.


blog: http://www.akropolix.net/rik0/blogs |
site: http://www.akropolix.net/rik0/ |
forum: http://www.akropolix.net/forum/ |


#7

Brian Chamberlain wrote:

As far as I know there’s no known problems…

Good.

Is the scope operator (::slight_smile: absolutely necessary in
your case?

Not necessary. I just wanted to. I grouped all the admin controllers.
This way I access them with

/admin/posts
/admin/users

Rails maps Admin::PostsController in /admin/posts and that’s what I
wanted :slight_smile: