Class: Mack::Controller::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/mack/controller/filter.rb

Overview

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…

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_method, klass, action_list = {}) ⇒ Filter

Returns a new instance of Filter.



37
38
39
40
41
# File 'lib/mack/controller/filter.rb', line 37

def initialize(filter_method, klass, action_list = {})
  @filter_method = filter_method
  clean_action_list(action_list)
  @klass = klass
end

Instance Attribute Details

#action_listObject (readonly)

Returns the value of attribute action_list.



35
36
37
# File 'lib/mack/controller/filter.rb', line 35

def action_list
  @action_list
end

#filter_methodObject (readonly)

Returns the value of attribute filter_method.



34
35
36
# File 'lib/mack/controller/filter.rb', line 34

def filter_method
  @filter_method
end

Instance Method Details

#==(other) ⇒ Object



57
58
59
# File 'lib/mack/controller/filter.rb', line 57

def ==(other)
  self.to_s == other.to_s
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/mack/controller/filter.rb', line 61

def eql?(other)
  self.to_s == other.to_s
end

#hashObject



65
66
67
# File 'lib/mack/controller/filter.rb', line 65

def hash
  self.to_s.hash
end

#run?(action) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
# File 'lib/mack/controller/filter.rb', line 43

def run?(action)
  return true if action_list.empty?
  if action_list[:only]
    return action_list[:only].include?(action)
  elsif action_list[:except]
    return !action_list[:except].include?(action)
  end
  return false
end

#to_sObject



53
54
55
# File 'lib/mack/controller/filter.rb', line 53

def to_s
  "#{@klass}.#{filter_method}"
end