Repository: rails/rails
File: actionpack/lib/action_dispatch/routing/mapper.rb (Ruby)

Code before:
              end
            end
          end

          def dispatcher(raise_on_name_error)
            Routing::RouteSet::Dispatcher.new raise_on_name_error
          end

          if Thread.respond_to?(:each_caller_location)
            def route_source_location
              if Mapper.route_source_locations || ActionDispatch.verbose_redirect_logs
                action_dispatch_dir = File.expand_path("..", __dir__)
                Thread.each_caller_location do |location|
                  next if location.path.start_with?(action_dispatch_dir)

                  cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
                  next if cleaned_path.nil?

                  return "#{cleaned_path}:#{location.lineno}"
                end
                nil
              end
            end
          else
            def route_source_location
              if Mapper.route_source_locations || ActionDispatch.verbose_redirect_logs
                action_dispatch_dir = File.expand_path("..", __dir__)
                caller_locations.each do |location|
                  next if location.path.start_with?(action_dispatch_dir)

                  cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
                  next if cleaned_path.nil?

                  return "#{cleaned_path}:#{location.lineno}"
                end
                nil
              end
            end
          end
      end

      # Invokes Journey::Router::Utils.normalize_path, then ensures that /(:locale)
      # becomes (/:locale). Except for root cases, where the former is the correct
      # one.
      def self.normalize_path(path)
        path = Journey::Router::Utils.normalize_path(path)

        # the path for a root URL at this point can be something like
        # "/(/:locale)(/:platform)/(:browser)", and we would want
        # "/(:locale)(/:platform)(/:browser)" reverse "/(", "/((" etc to "(/", "((/" etc
        path.gsub!(%r{/(\(+)/?}, '\1/')

Review comment (suggestion):
If we are going to return "route source locations", we might as well change how `Mapper.route_source_locations` is set and avoid `Mapper` having to know about `verbose_redirect_logs`.

```suggestion
             if Mapper.route_source_locations
```

We could set it here instead:
https://github.com/rails/rails/blob/d0f830033b2ea27bc1e66683ce5b16bd9ca55df1/actionpack/lib/action_dispatch/railtie.rb#L71

```ruby
      ActionDispatch::Routing::Mapper.route_source_locations = Rails.env.development? || ActionDispatch.verbose_redirect_logs
```

Or could we just leave it out?
Not sure what the result would be.


Code after:
            Routing::RouteSet::Dispatcher.new raise_on_name_error
          end

          def route_source_location
            if Mapper.route_source_locations
              action_dispatch_dir = File.expand_path("..", __dir__)
              Thread.each_caller_location do |location|
                next if location.path.start_with?(action_dispatch_dir)

                cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
                next if cleaned_path.nil?

                return "#{cleaned_path}:#{location.lineno}"
              end
              nil
            end
          end
      end

      # Invokes Journey::Router::Utils.normalize_path, then ensures that /(:locale)
      # becomes (/:locale). Except for root cases, where the former is the correct
      # one.
      def self.normalize_path(path)
        path = Journey::Router::Utils.normalize_path(path)

        # the path for a root URL at this point can be something like
        # "/(/:locale)(/:platform)/(:browser)", and we would want
        # "/(:locale)(/:platform)(/:browser)" reverse "/(", "/((" etc to "(/", "((/" etc
        path.gsub!(%r{/(\(+)/?}, '\1/')
        # if a path is all optional segments, change the leading "(/" back to "/(" so it
        # evaluates to "/" when interpreted with no options. Unless, however, at least
        # one secondary segment consists of a static part, ex.
        # "(/:locale)(/pages/:page)"
        path.sub!(%r{^(\(+)/}, '/\1') if %r{^(\(+[^)]+\))(\(+/:[^)]+\))*$}.match?(path)
        path
      end

      def self.normalize_name(name)
        normalize_path(name)[1..-1].tr("/", "_")
      end

      module Base
        # Matches a URL pattern to one or more routes.
        #
        # You should not use the `match` method in your router without specifying an
        # HTTP method.
        #
        # If you want to expose your action to both GET and POST, use:
        #
        #     # sets :controller, :action, and :id in params
        #     match ':controller/:action/:id', via: [:get, :post]