Class Mack::Controller::Filter
In: lib/mack/controller/filter.rb
Parent: Object

A wrapper class to hold calls to filter methods for Controllers. This class should never be called by itself. Instead there are helper methods in Mack::Controller to do this for.

Examples:

  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

Filter methods need to be scoped to the controller that is to run them. There are three different filters available: before, after and after_render.

before filters get run before an action is called. This is a great place to set up common elements needed for your action. Things like authentication should be done here, etc…

after filters get run after an action has been called. 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 filters get run after the rendering of the action 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…

Methods

==   eql?   hash   new   run?   to_s  

Attributes

action_list  [R] 
filter_method  [R] 

Public Class methods

[Source]

    # File lib/mack/controller/filter.rb, line 37
37:       def initialize(filter_method, klass, action_list = {})
38:         @filter_method = filter_method
39:         clean_action_list(action_list)
40:         @klass = klass
41:       end

Public Instance methods

[Source]

    # File lib/mack/controller/filter.rb, line 57
57:       def ==(other)
58:         self.to_s == other.to_s
59:       end

[Source]

    # File lib/mack/controller/filter.rb, line 61
61:       def eql?(other)
62:         self.to_s == other.to_s
63:       end

[Source]

    # File lib/mack/controller/filter.rb, line 65
65:       def hash
66:         self.to_s.hash
67:       end

[Source]

    # File lib/mack/controller/filter.rb, line 43
43:       def run?(action)
44:         return true if action_list.empty?
45:         if action_list[:only]
46:           return action_list[:only].include?(action)
47:         elsif action_list[:except]
48:           return !action_list[:except].include?(action)
49:         end
50:         return false
51:       end

[Source]

    # File lib/mack/controller/filter.rb, line 53
53:       def to_s
54:         "#{@klass}.#{filter_method}"
55:       end

[Validate]