BigW Consortium Gitlab

routing.rb 1.7 KB
Newer Older
1 2
module Gitlab
  module Routing
3 4
    extend ActiveSupport::Concern

5 6 7
    mattr_accessor :_includers
    self._includers = []

8
    included do
9 10
      Gitlab::Routing.includes_helpers(self)

11 12 13
      include Gitlab::Routing.url_helpers
    end

14 15 16 17
    def self.includes_helpers(klass)
      self._includers << klass
    end

18 19 20 21
    def self.add_helpers(mod)
      url_helpers.include mod
      url_helpers.extend mod

22 23 24
      GitlabRoutingHelper.include mod
      GitlabRoutingHelper.extend mod

25 26 27 28 29 30 31 32 33
      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

34 35 36 37 38 39 40 41 42
    # 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
43 44

    def self.redirect_legacy_paths(router, *paths)
45 46
      build_redirect_path = lambda do |request, _params, path|
        # Only replace the last occurence of `path`.
47 48
        #
        # `request.fullpath` includes the querystring
49 50
        new_path = request.path.sub(%r{/#{path}(/*)(?!.*#{path})}, "/-/#{path}\\1")
        new_path << "?#{request.query_string}" if request.query_string.present?
51

52
        new_path
53 54
      end

55 56 57
      paths.each do |path|
        router.match "/#{path}(/*rest)",
                     via: [:get, :post, :patch, :delete],
58
                     to: router.redirect { |params, request| build_redirect_path.call(request, params, path) },
59 60 61
                     as: "legacy_#{path}_redirect"
      end
    end
62 63
  end
end