Filters
There are three types of filters in Mack: Before-Filter, After-Filter, and After-Render-Filter.
Filter must be scoped to the controller that uses it. When defining a filter type, you can also designate which actions in the controller are to use the filter (or define which actions in the controller are to be excluded by the filter execution.) Also note that when defining which filter to use, make sure that the method is accessible from that controller (i.e. either the method comes from the same scope as the controller, or it comes from some controller helper module).
Here's some ideas on when to use which filter type:
-
before_filter.
Since it's run before an action of a controller, it's a good place to setup common elements that are needed by the actions; it's also the perfect place to do user authentication (since it's guarantee that the authentication code will get run before the actual action). -
after_filter.
After-Filter gets run after the action is executed. This is a great place to set up common elements for a view, that depend on stuff from inside your action. Because nothing has been 'rendered' yet, you still can add new instance variables, and alter ones created in the action. -
after_render filter.
After-Render-Filter is run after the rendering has happened; at this point there is an instance variable, @final_rendered_action, that is available on which work can be done. This variable will have any layouts rendered to, any Erubis::Eruby will have been processed, etc... It should be the final String that will get rendered to the screen. This is a great place to do things like write a log, gzip, etc...
The following is an example on how to define filter in a controller.
class MyAwesomeController include Mack::Controller # all actions in this controller will have this filter run: before_filter :authenticate # only the show and index actions in this controller will have this filter run: before_filter :load_side_bar, :only => [:show, :index] # all actions, except for the create action will have this filter run. after_filter :write_to_log, :except => :create end