2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-24 12:00:26 -04:00
|
|
|
module Gitlab
|
|
|
|
module Routing
|
2016-12-13 08:51:23 -05:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2017-07-07 11:43:37 -04:00
|
|
|
mattr_accessor :_includers
|
|
|
|
self._includers = []
|
|
|
|
|
2016-12-13 08:51:23 -05:00
|
|
|
included do
|
2017-07-08 17:24:18 -04:00
|
|
|
Gitlab::Routing.includes_helpers(self)
|
|
|
|
|
2016-12-13 08:51:23 -05:00
|
|
|
include Gitlab::Routing.url_helpers
|
|
|
|
end
|
|
|
|
|
2017-07-08 17:24:18 -04:00
|
|
|
def self.includes_helpers(klass)
|
|
|
|
self._includers << klass
|
|
|
|
end
|
|
|
|
|
2017-07-07 11:43:37 -04:00
|
|
|
def self.add_helpers(mod)
|
|
|
|
url_helpers.include mod
|
|
|
|
url_helpers.extend mod
|
|
|
|
|
2017-07-08 17:24:18 -04:00
|
|
|
GitlabRoutingHelper.include mod
|
|
|
|
GitlabRoutingHelper.extend mod
|
|
|
|
|
2017-07-07 11:43:37 -04:00
|
|
|
app_url_helpers = Gitlab::Application.routes.named_routes.url_helpers_module
|
|
|
|
app_url_helpers.include mod
|
|
|
|
app_url_helpers.extend mod
|
|
|
|
|
|
|
|
_includers.each do |klass|
|
|
|
|
klass.include mod
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-24 12:00:26 -04:00
|
|
|
# Returns the URL helpers Module.
|
|
|
|
#
|
|
|
|
# This method caches the output as Rails' "url_helpers" method creates an
|
|
|
|
# anonymous module every time it's called.
|
|
|
|
#
|
|
|
|
# Returns a Module.
|
|
|
|
def self.url_helpers
|
|
|
|
@url_helpers ||= Gitlab::Application.routes.url_helpers
|
|
|
|
end
|
2017-10-26 10:06:51 -04:00
|
|
|
|
|
|
|
def self.redirect_legacy_paths(router, *paths)
|
2017-10-27 05:29:51 -04:00
|
|
|
build_redirect_path = lambda do |request, _params, path|
|
2019-05-20 10:11:44 -04:00
|
|
|
# Only replace the last occurrence of `path`.
|
2017-11-07 07:04:46 -05:00
|
|
|
#
|
|
|
|
# `request.fullpath` includes the querystring
|
2017-11-16 12:12:45 -05:00
|
|
|
new_path = request.path.sub(%r{/#{path}(/*)(?!.*#{path})}, "/-/#{path}\\1")
|
2018-10-22 03:00:50 -04:00
|
|
|
new_path = "#{new_path}?#{request.query_string}" if request.query_string.present?
|
2017-10-27 05:29:51 -04:00
|
|
|
|
2017-11-16 12:12:45 -05:00
|
|
|
new_path
|
2017-10-27 05:29:51 -04:00
|
|
|
end
|
|
|
|
|
2017-10-26 10:06:51 -04:00
|
|
|
paths.each do |path|
|
|
|
|
router.match "/#{path}(/*rest)",
|
|
|
|
via: [:get, :post, :patch, :delete],
|
2017-10-27 05:29:51 -04:00
|
|
|
to: router.redirect { |params, request| build_redirect_path.call(request, params, path) },
|
2017-10-26 10:06:51 -04:00
|
|
|
as: "legacy_#{path}_redirect"
|
|
|
|
end
|
|
|
|
end
|
2016-03-24 12:00:26 -04:00
|
|
|
end
|
|
|
|
end
|