Subclassing controllers and when to call super

I have a lot of controllers that will have similar behavior, eg. user needs to be logged in, some scope needs to be configured, current_account / current_user needs to be set and permissions cached.

I am thinking about using a standard controller and subclassing that.

class MyStandardController < ApplicationController
 before_filter :xyz

class SomeController < MyStandardController


What I'm wondering is I need / when should I be called super

at all?



source to share

1 answer

You don't need to ever call super

inside a controller that inherits from another controller; in fact, it would probably be strange. Super executes the method of the same name from the superclass, and you probably won't have any methods on MyStandardController

that you override in your children.

The main reason you are doing this is, as you said yourself, to get filters and methods to simplify names between controllers. We do something similar in our applications where one area of ​​a site with very similar behavior inherits from a controller (like ShoppingController) that has a section of private convenience methods that can only be used on all of its children.

However, in terms of realism, it would probably be better to have modules that implement the desired functionality and include them in the correct controllers. After all, you probably want something from one controller on top of another, and this is easier with modules than with complex inheritance hierarchies.



All Articles