Class: RailsRouteChecker::LoadedApp

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-route-checker/loaded_app.rb

Instance Method Summary collapse

Constructor Details

#initializeLoadedApp

Returns a new instance of LoadedApp.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails-route-checker/loaded_app.rb', line 5

def initialize
  app_base_path = Dir.pwd
  suppress_output do
    require_relative "#{app_base_path}/config/boot"
  end

  begin
    suppress_output do
      require_relative "#{Dir.pwd}/config/environment"
    end
  rescue Exception => e
    puts "Requiring your config/environment.rb file failed."
    puts "This means that something raised while trying to start Rails."
    puts ""
    puts e.backtrace
    raise(e)
  end

  suppress_output do
    @app = Rails.application
    @app.eager_load!
    Rails::Engine.subclasses.each(&:eager_load!)
  end
end

Instance Method Details

#all_route_namesObject



46
47
48
# File 'lib/rails-route-checker/loaded_app.rb', line 46

def all_route_names
  @all_route_names ||= app.routes.routes.map(&:name).compact
end

#controller_informationObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rails-route-checker/loaded_app.rb', line 50

def controller_information
  return @controller_information if @controller_information

  base_controllers_descendants = [ActionController::Base, ActionController::API].flat_map(&:descendants)

  @controller_information = base_controllers_descendants.map do |controller|
    next if controller.controller_path.nil? || controller.controller_path.start_with?('rails/')

    controller_helper_methods =
      if controller.respond_to?(:helpers)
        controller.helpers.methods.map(&:to_s)
      else
        []
      end

    [
      controller.controller_path,
      {
        helpers: controller_helper_methods,
        actions: controller.action_methods.to_a,
        instance_methods: instance_methods(controller),
        lookup_context: lookup_context(controller)
      }
    ]
  end.compact.to_h
end

#routesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails-route-checker/loaded_app.rb', line 30

def routes
  return @routes if defined?(@routes)

  @routes = app.routes.routes.reject do |r|
    reject_route?(r)
  end.uniq

  return @routes unless app.config.respond_to?(:assets)

  use_spec = defined?(ActionDispatch::Journey::Route) || defined?(Journey::Route)
  @routes.reject do |route|
    path = use_spec ? route.path.spec.to_s : route.path
    path =~ /^#{app.config.assets.prefix}/
  end
end