ssoroka (owner)

Fork Of

Revisions

gist: 32256 Download_button fork
public
Public Clone URL: git://gist.github.com/32256.git
Embed All Files: show embed
Text #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class RouteFilter
  
  class << self
    
    def routes
      ActionController::Routing::Routes.routes
    end
    
    def controllers
      ::Object.subclasses_of( ::ActionController::Base )
    end
    
    def prune!
      controllers.each do |controller|
        puts "** Pruning routes for #{controller.to_s}"
        new( controller ).prune!
      end
    end
    
  end
  
  attr_accessor :controller
  
  def initialize( controller )
    @controller = controller
  end
  
  def path
    @controller.controller_path
  end
  
  def routes
    @routes ||= self.class.routes.find_all{|r| r.requirements[:controller] == path }
  end
  
  def routeable_actions
    @actions_from_routes ||= @routes.map{|r| r.requirements[:action] }.to_set
  end
  
  def defined_actions
    @defined_actions ||= @controller.action_methods
  end
  
  def pruneable_actions
    @pruneable_actions ||= routeable_actions ^ defined_actions
  end
  
  def pruneable_routes
    routes.find_all{|r| pruneable_actions.include?( r.requirements[:action] ) }
  end
  
  def prune!
    pruneable_routes.each do |pruneable|
      RouteFilter.routes.delete(pruneable)
    end
  end
  
end