Skip to content

Commit 73c090f

Browse files
authored
Merge pull request rails#50057 from Shopify/route-source-location-thread-each-caller
Routing::Mapper: Use Thread.each_caller_location if available
2 parents f98bb7e + b8d0f39 commit 73c090f

File tree

1 file changed

+31
-6
lines changed
  • actionpack/lib/action_dispatch/routing

1 file changed

+31
-6
lines changed

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,37 @@ def dispatcher(raise_on_name_error)
360360
Routing::RouteSet::Dispatcher.new raise_on_name_error
361361
end
362362

363-
def route_source_location
364-
if Mapper.route_source_locations
365-
action_dispatch_dir = File.expand_path("..", __dir__)
366-
caller_location = caller_locations.find { |location| !location.path.include?(action_dispatch_dir) }
367-
cleaned_path = Mapper.backtrace_cleaner.clean([caller_location.path]).first
368-
"#{cleaned_path}:#{caller_location.lineno}" if cleaned_path
363+
if Thread.respond_to?(:each_caller_location)
364+
def route_source_location
365+
if Mapper.route_source_locations
366+
action_dispatch_dir = File.expand_path("..", __dir__)
367+
Thread.each_caller_location do |location|
368+
next if location.path.start_with?(action_dispatch_dir)
369+
370+
if cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
371+
return "#{cleaned_path}:#{location.lineno}"
372+
else
373+
return nil
374+
end
375+
end
376+
nil
377+
end
378+
end
379+
else
380+
def route_source_location
381+
if Mapper.route_source_locations
382+
action_dispatch_dir = File.expand_path("..", __dir__)
383+
caller_locations.each do |location|
384+
next if location.path.start_with?(action_dispatch_dir)
385+
386+
if cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
387+
return "#{cleaned_path}:#{location.lineno}"
388+
else
389+
return nil
390+
end
391+
end
392+
nil
393+
end
369394
end
370395
end
371396
end

0 commit comments

Comments
 (0)