Class: ActionDispatch::Routing::RoutesProxy

Inherits:
Object
  • Object
show all
Includes:
UrlFor
Defined in:
lib/action_dispatch/routing/routes_proxy.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UrlFor

#full_url_for, #route_for, #url_for

Methods included from PolymorphicRoutes

#polymorphic_path, #polymorphic_url

Constructor Details

#initialize(routes, scope, helpers, script_namer = nil) ⇒ RoutesProxy

Returns a new instance of RoutesProxy.



11
12
13
14
15
# File 'lib/action_dispatch/routing/routes_proxy.rb', line 11

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/action_dispatch/routing/routes_proxy.rb', line 27

def method_missing(method, *args)
  if @helpers.respond_to?(method)
    self.class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{method}(*args)
        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.#{method}(*args)
      end
    RUBY
    send(method, *args)
  else
    super
  end
end

Instance Attribute Details

#routesObject Also known as: _routes

Returns the value of attribute routes.



8
9
10
# File 'lib/action_dispatch/routing/routes_proxy.rb', line 8

def routes
  @routes
end

#scopeObject

Returns the value of attribute scope.



8
9
10
# File 'lib/action_dispatch/routing/routes_proxy.rb', line 8

def scope
  @scope
end

Instance Method Details

#merge_script_names(previous_script_name, new_script_name) ⇒ Object

Keeps the part of the script name provided by the global context via ENV, 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.



55
56
57
58
59
60
61
62
63
# File 'lib/action_dispatch/routing/routes_proxy.rb', line 55

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

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/action_dispatch/routing/routes_proxy.rb', line 23

def respond_to_missing?(method, include_private = false)
  super || @helpers.respond_to?(method)
end

#url_optionsObject



17
18
19
20
21
# File 'lib/action_dispatch/routing/routes_proxy.rb', line 17

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