Class: RoutingReport::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/routing_report/report.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_class: Object, routes: []) ⇒ Report

Returns a new instance of Report.



5
6
7
# File 'lib/routing_report/report.rb', line 5

def initialize(base_class: Object, routes: [])
  @base_class, @routes = base_class, routes
end

Instance Method Details

#actions_without_routesObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/routing_report/report.rb', line 39

def actions_without_routes
  base_class.descendants.each_with_object([]) do |controller_class, accum|
    controller_name = controller_class.name.underscore.match(/\A(.*)_controller\z/)[1]

    controller_class.public_instance_methods(false).each do |method|
      unless routes.any? { |r| r.defaults[:controller] == controller_name && r.defaults[:action] == method.to_s }
        accum << "#{controller_name}##{method.to_s}"
      end
    end
  end.sort
end


9
10
11
12
# File 'lib/routing_report/report.rb', line 9

def print(to: STDOUT)
  print_table('Routes without actions', routes_without_actions, to)
  print_table('Actions without routes', actions_without_routes, to)
end

#routes_without_actionsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/routing_report/report.rb', line 14

def routes_without_actions
  routes.each_with_object([]) do |route, accum|
    controller_name = route.defaults[:controller]
    action_name = route.defaults[:action]

    if controller_name && action_name
      begin
        controller = "#{controller_name}_controller".classify.constantize
      rescue NameError
        accum << "#{controller_name}##{action_name}"
        next
      end

      # get all superclasses that descend from ActionController::Base
      # this allows us to avoid false positives when routes are fulfilled by
      # actions in a superclass:
      matching_controllers = controller.ancestors.select { |c| c < base_class }

      unless matching_controllers.any? { |c| c.public_instance_methods.include?(action_name.to_sym) }
        accum << "#{controller_name}##{action_name}"
      end
    end
  end.sort
end