Module: Gin::Filterable::ClassMethods

Defined in:
lib/gin/filterable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object

:nodoc:



14
15
16
# File 'lib/gin/filterable.rb', line 14

def self.extended obj     # :nodoc:
  obj.__setup_filterable
end

Instance Method Details

#__setup_filterableObject

:nodoc:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gin/filterable.rb', line 24

def __setup_filterable    # :nodoc:
  @before_filters = {nil => []}
  superclass.before_filters.each{|k,v| @before_filters[k] = v.dup if v } if
    superclass.respond_to?(:before_filters)

  @after_filters  = {nil => []}
  superclass.after_filters.each{|k,v| @after_filters[k] = v.dup if v } if
    superclass.respond_to?(:after_filters)

  @filters = {}
  pfilters = self.superclass.filters if
    self.superclass.respond_to?(:filters)
  @filters = pfilters.dup if pfilters
end

#after_filter(name, *opts, &block) ⇒ Object

Assign one or more filters to run after calling an action. Set for all actions by default. This attribute is inherited. Supports an options hash as the last argument with :only and :except keys.

after_filter :clear_cookies, :only => :logout do
  session[:user] = nil
end


156
157
158
159
# File 'lib/gin/filterable.rb', line 156

def after_filter name, *opts, &block
  filter(name, &block) if block_given?
  append_filters(self.after_filters, name, *opts)
end

#after_filtersObject

List of after filters.



165
166
167
# File 'lib/gin/filterable.rb', line 165

def after_filters
  @after_filters
end

#after_filters_for(action) ⇒ Object

Get an Array of after filter names for the given action.



192
193
194
# File 'lib/gin/filterable.rb', line 192

def after_filters_for action
  after_filters[action] || after_filters[nil] || []
end

#append_filters(filter_hsh, name, *names) ⇒ Object

:nodoc:



94
95
96
97
98
# File 'lib/gin/filterable.rb', line 94

def append_filters filter_hsh, name, *names #:nodoc:
  modify_filter_stack(filter_hsh, name, *names) do |h,k,n|
    h[k].concat n
  end
end

#before_filter(name, *opts, &block) ⇒ Object

Assign one or more filters to run before calling an action. Set for all actions by default. This attribute is inherited. Supports an options hash as the last argument with :only and :except keys.

before_filter :logged_in, :except => :index do
  verify_session! || halt 401
end


119
120
121
122
# File 'lib/gin/filterable.rb', line 119

def before_filter name, *opts, &block
  filter(name, &block) if block_given?
  append_filters(before_filters, name, *opts)
end

#before_filtersObject

List of before filters. This attribute is inherited.



129
130
131
# File 'lib/gin/filterable.rb', line 129

def before_filters
  @before_filters
end

#before_filters_for(action) ⇒ Object

Get an Array of before filter names for the given action.



184
185
186
# File 'lib/gin/filterable.rb', line 184

def before_filters_for action
  before_filters[action] || before_filters[nil] || []
end

#filter(name, &block) ⇒ Object

Create a filter for controller actions.

filter :logged_in do
  @user && @user.logged_in?
end

Use Gin::Controller.before_filter and Gin::Controller.after_filter to apply filters.



49
50
51
# File 'lib/gin/filterable.rb', line 49

def filter name, &block
  self.filters[name.to_sym] = block
end

#filtersObject

Hash of filters defined by Gin::Controller.filter. This attribute is inherited.



58
59
60
# File 'lib/gin/filterable.rb', line 58

def filters
  @filters
end

#inherited(subclass) ⇒ Object

:nodoc:



18
19
20
21
# File 'lib/gin/filterable.rb', line 18

def inherited subclass    # :nodoc:
  subclass.__setup_filterable
  super
end

#modify_filter_stack(filter_hsh, name, *names) ⇒ Object

:nodoc:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gin/filterable.rb', line 63

def modify_filter_stack filter_hsh, name, *names #:nodoc:
  names = [name].concat(names)
  opts  = Hash === names[-1] ? names.pop : {}
  names.map!(&:to_sym)

  if opts[:only]
    Array(opts[:only]).each do |action|
      action = action.to_sym
      filter_hsh[action] ||= filter_hsh[nil].dup
      yield filter_hsh, action, names
    end

  elsif opts[:except]
    except = Array(opts[:except])
    except.each do |action|
      filter_hsh[action] ||= filter_hsh[nil].dup
    end

    filter_hsh.keys.each do |action|
      next if except.include?(action)
      yield filter_hsh, action, names
    end

  else
    filter_hsh.keys.each do |action|
      yield filter_hsh, action, names
    end
  end
end

#skip_after_filter(name, *names) ⇒ Object

Skip an after filter in the context of the controller. This attribute is inherited. Supports an options hash as the last argument with :only and :except keys.



176
177
178
# File 'lib/gin/filterable.rb', line 176

def skip_after_filter name, *names
  skip_filters(self.after_filters, name, *names)
end

#skip_before_filter(name, *names) ⇒ Object

Skip a before filter in the context of the controller. This attribute is inherited. Supports an options hash as the last argument with :only and :except keys.



140
141
142
# File 'lib/gin/filterable.rb', line 140

def skip_before_filter name, *names
  skip_filters(self.before_filters, name, *names)
end

#skip_filters(filter_hsh, name, *names) ⇒ Object

:nodoc:



101
102
103
104
105
# File 'lib/gin/filterable.rb', line 101

def skip_filters filter_hsh, name, *names #:nodoc:
  modify_filter_stack(filter_hsh, name, *names) do |h,k,n|
    h[k] -= n
  end
end