Sha256: 40b0ce8dab381517b1caa42269539304ded041b52d4110c88b2093843f0d59cd

Contents?: true

Size: 1.83 KB

Versions: 12

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/array/extract_options"

module ActionDispatch
  module Routing
    class RoutesProxy # :nodoc:
      include ActionDispatch::Routing::UrlFor

      attr_accessor :scope, :routes
      alias :_routes :routes

      def initialize(routes, scope, helpers, script_namer = nil)
        @routes, @scope = routes, scope
        @helpers = helpers
        @script_namer = script_namer
      end

      def url_options
        scope.send(:_with_routes, routes) do
          scope.url_options
        end
      end

    private
      def respond_to_missing?(method, _)
        super || @helpers.respond_to?(method)
      end

      def method_missing(method, *args)
        if @helpers.respond_to?(method)
          options = args.extract_options!
          options = url_options.merge((options || {}).symbolize_keys)

          if @script_namer
            options[:script_name] = merge_script_names(
              options[:script_name],
              @script_namer.call(options)
            )
          end

          args << options
          @helpers.public_send(method, *args)
        else
          super
        end
      end

      # Keeps the part of the script name provided by the global
      # context via ENV["SCRIPT_NAME"], which `mount` doesn't know
      # about since it depends on the specific request, but use our
      # script name resolver for the mount point dependent part.
      def merge_script_names(previous_script_name, new_script_name)
        return new_script_name unless previous_script_name

        resolved_parts = new_script_name.count("/")
        previous_parts = previous_script_name.count("/")
        context_parts = previous_parts - resolved_parts + 1

        (previous_script_name.split("/").slice(0, context_parts).join("/")) + new_script_name
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/actionpack-7.1.3.4/lib/action_dispatch/routing/routes_proxy.rb
actionpack-7.1.5.1 lib/action_dispatch/routing/routes_proxy.rb
actionpack-7.1.5 lib/action_dispatch/routing/routes_proxy.rb
actionpack-7.1.4.2 lib/action_dispatch/routing/routes_proxy.rb
actionpack-7.1.4.1 lib/action_dispatch/routing/routes_proxy.rb
actionpack-7.1.4 lib/action_dispatch/routing/routes_proxy.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionpack-7.1.3.4/lib/action_dispatch/routing/routes_proxy.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/actionpack-7.1.3.4/lib/action_dispatch/routing/routes_proxy.rb
actionpack-7.1.3.4 lib/action_dispatch/routing/routes_proxy.rb
actionpack-7.1.3.2 lib/action_dispatch/routing/routes_proxy.rb
actionpack-7.1.3.1 lib/action_dispatch/routing/routes_proxy.rb
actionpack-7.1.3 lib/action_dispatch/routing/routes_proxy.rb