Class: Mack::Controller::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/sea_level/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::Base to do this for.

Examples:

class MyAwesomeController < Mack::Controller::Base
  # 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.



36
37
38
39
40
# File 'lib/sea_level/filter.rb', line 36

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.



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

def action_list
  @action_list
end

#filter_methodObject (readonly)

Returns the value of attribute filter_method.



33
34
35
# File 'lib/sea_level/filter.rb', line 33

def filter_method
  @filter_method
end

Instance Method Details

#==(other) ⇒ Object



56
57
58
# File 'lib/sea_level/filter.rb', line 56

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/sea_level/filter.rb', line 60

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

#hashObject



64
65
66
# File 'lib/sea_level/filter.rb', line 64

def hash
  self.to_s.hash
end

#run?(action) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'lib/sea_level/filter.rb', line 42

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



52
53
54
# File 'lib/sea_level/filter.rb', line 52

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