Class: RouteGuard::RouteLoader
- Inherits:
-
Object
- Object
- RouteGuard::RouteLoader
- Defined in:
- lib/route_guard/route_loader.rb
Class Method Summary collapse
Class Method Details
.load ⇒ Object
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 |
# File 'lib/route_guard/route_loader.rb', line 8 def self.load load_environment! RouteTracker.reset! RouteTracker.enable_tracking! RouteTracker.enabled = true begin $stderr.puts "RouteGuard Debug: defined?(Rails) = #{defined?(Rails)}" if defined?(Rails) $stderr.puts "RouteGuard Debug: Rails.respond_to?(:application) = #{Rails.respond_to?(:application)}" if Rails.respond_to?(:application) && Rails.application $stderr.puts "RouteGuard Debug: Rails.application = #{Rails.application.class.name}" $stderr.puts "RouteGuard Debug: Routes size before reload = #{Rails.application.routes.routes.size}" Rails.application.reload_routes! $stderr.puts "RouteGuard Debug: Routes size after reload = #{Rails.application.routes.routes.size}" else $stderr.puts "RouteGuard Debug: Rails.application = nil/undefined" end end if defined?(Rails) && Rails.respond_to?(:application) && Rails.application load_from_route_set(Rails.application.routes) else [] end ensure RouteTracker.enabled = false end end |
.load_environment! ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/route_guard/route_loader.rb', line 39 def self.load_environment! return if defined?(Rails) && Rails.respond_to?(:application) && Rails.application env_file = File.("config/environment.rb", Dir.pwd) if File.exist?(env_file) original_stdout = $stdout.clone original_stderr = $stderr.clone begin $stdout.reopen(File::NULL, "w") $stderr.reopen(File::NULL, "w") require env_file ensure $stdout.reopen(original_stdout) $stderr.reopen(original_stderr) end end end |
.load_from_route_set(route_set) ⇒ Object
57 58 59 60 61 62 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/route_guard/route_loader.rb', line 57 def self.load_from_route_set(route_set) routes = [] route_set.routes.each do |r| # Skip internal rails routes if the route itself responds to internal? next if r.respond_to?(:internal?) && r.internal? # Extract verb verb = if r.verb.is_a?(Regexp) r.verb.source.gsub(/[^A-Z|]/, "") else r.verb.to_s.upcase end verb = "ANY" if verb.empty? # Extract path original_path = if r.respond_to?(:path) && r.path.respond_to?(:spec) r.path.spec.to_s else r.path.to_s end path = original_path.gsub(/\(\.\:format\)\z/, "").gsub(/\(\/:format\)\z/, "") defaults = r.defaults || {} controller = defaults[:controller] action = defaults[:action] name = r.name constraints = r.requirements || {} trace_info = RouteTracker.route_traces[r.object_id] || r.instance_variable_get(:@route_guard_trace) file = trace_info&.[](:file) line = trace_info&.[](:line) route = Models::Route.new( verb: verb, path: path, original_path: original_path, controller: controller, action: action, name: name, constraints: constraints, file: file, line: line, defaults: defaults ) next if route.internal? routes << route end routes end |