2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:51:43 -04:00
|
|
|
require "active_support/core_ext/array/extract_options"
|
2013-01-31 09:59:18 -05:00
|
|
|
|
2010-08-25 07:41:56 -04:00
|
|
|
module ActionDispatch
|
|
|
|
module Routing
|
|
|
|
class RoutesProxy #:nodoc:
|
|
|
|
include ActionDispatch::Routing::UrlFor
|
|
|
|
|
|
|
|
attr_accessor :scope, :routes
|
|
|
|
alias :_routes :routes
|
|
|
|
|
2017-07-02 17:36:33 -04:00
|
|
|
def initialize(routes, scope, helpers, script_namer = nil)
|
2010-08-25 07:41:56 -04:00
|
|
|
@routes, @scope = routes, scope
|
2015-02-19 18:11:08 -05:00
|
|
|
@helpers = helpers
|
2017-07-02 17:36:33 -04:00
|
|
|
@script_namer = script_namer
|
2010-08-25 07:41:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def url_options
|
|
|
|
scope.send(:_with_routes, routes) do
|
|
|
|
scope.url_options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-22 05:29:05 -04:00
|
|
|
private
|
|
|
|
def respond_to_missing?(method, _)
|
2015-02-19 17:20:56 -05:00
|
|
|
super || @helpers.respond_to?(method)
|
2012-01-03 14:26:45 -05:00
|
|
|
end
|
|
|
|
|
2010-08-25 07:41:56 -04:00
|
|
|
def method_missing(method, *args)
|
2015-02-19 17:20:56 -05:00
|
|
|
if @helpers.respond_to?(method)
|
2010-08-25 07:41:56 -04:00
|
|
|
self.class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
|
|
def #{method}(*args)
|
|
|
|
options = args.extract_options!
|
2017-07-02 17:36:33 -04:00
|
|
|
options = url_options.merge((options || {}).symbolize_keys)
|
2017-07-22 19:25:26 -04:00
|
|
|
|
|
|
|
if @script_namer
|
|
|
|
options[:script_name] = merge_script_names(
|
|
|
|
options[:script_name],
|
|
|
|
@script_namer.call(options)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-07-02 17:36:33 -04:00
|
|
|
args << options
|
2015-02-19 17:20:56 -05:00
|
|
|
@helpers.#{method}(*args)
|
2010-08-25 07:41:56 -04:00
|
|
|
end
|
|
|
|
RUBY
|
2017-04-22 05:29:05 -04:00
|
|
|
public_send(method, *args)
|
2010-08-25 07:41:56 -04:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2017-07-22 19:25:26 -04:00
|
|
|
|
|
|
|
# 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
|
2010-08-25 07:41:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|