Commit bc05f258 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC Committed by Matthias Käppler

Move project shorthand url helper generation from initializers

Initializers run only once while booting the application therefore on
development and test environments, if we add a new route, the shorthand
url helpers for `projects` resource will not be available until we
reboot the project unless we don't move the logic to correct place.
parent 8066d3d2
...@@ -368,30 +368,5 @@ module Gitlab ...@@ -368,30 +368,5 @@ module Gitlab
end end
end end
end end
config.after_initialize do
# Devise (see initializers/8_devise.rb) already reloads routes if
# eager loading is enabled, so don't do this twice since it's
# expensive.
Rails.application.reload_routes! unless config.eager_load
project_url_helpers = Module.new do
extend ActiveSupport::Concern
Gitlab::Application.routes.named_routes.helper_names.each do |name|
next unless name.include?('namespace_project')
define_method(name.sub('namespace_project', 'project')) do |project, *args|
send(name, project&.namespace, project, *args)
end
end
end
# We add the MilestonesRoutingHelper because we know that this does not
# conflict with the methods defined in `project_url_helpers`, and we want
# these methods available in the same places.
Gitlab::Routing.add_helpers(project_url_helpers)
Gitlab::Routing.add_helpers(TimeboxesRoutingHelper)
end
end end
end end
...@@ -154,9 +154,13 @@ if Gitlab::Metrics.enabled? && !Rails.env.test? && !(Rails.env.development? && d ...@@ -154,9 +154,13 @@ if Gitlab::Metrics.enabled? && !Rails.env.test? && !(Rails.env.development? && d
# of the ActiveRecord methods. This has to take place _after_ initializing as # of the ActiveRecord methods. This has to take place _after_ initializing as
# for some unknown reason calling eager_load! earlier breaks Devise. # for some unknown reason calling eager_load! earlier breaks Devise.
Gitlab::Application.config.after_initialize do Gitlab::Application.config.after_initialize do
Rails.application.eager_load! # We should move all the logic of this file to somewhere else
# and require it after `Rails.application.initialize!` in `environment.rb` file.
models_path = Rails.root.join('app', 'models').to_s
models = Rails.root.join('app', 'models').to_s Dir.glob("**/*.rb", base: models_path).sort.each do |file|
require_dependency file
end
regex = Regexp.union( regex = Regexp.union(
ActiveRecord::Querying.public_instance_methods(false).map(&:to_s) ActiveRecord::Querying.public_instance_methods(false).map(&:to_s)
...@@ -172,7 +176,7 @@ if Gitlab::Metrics.enabled? && !Rails.env.test? && !(Rails.env.development? && d ...@@ -172,7 +176,7 @@ if Gitlab::Metrics.enabled? && !Rails.env.test? && !(Rails.env.development? && d
else else
loc = method.source_location loc = method.source_location
loc && loc[0].start_with?(models) && method.source =~ regex loc && loc[0].start_with?(models_path) && method.source =~ regex
end end
end end
......
...@@ -288,7 +288,28 @@ Rails.application.routes.draw do ...@@ -288,7 +288,28 @@ Rails.application.routes.draw do
get '/sitemap' => 'sitemap#show', format: :xml get '/sitemap' => 'sitemap#show', format: :xml
end end
# Creates shorthand helper methods for project resources.
# For example; for the `namespace_project_path` this also creates `project_path`.
#
# TODO: We don't need the `Gitlab::Routing` module at all as we can use
# the `direct` DSL method of Rails to define url helpers. Move all the
# custom url helpers to use the `direct` DSL method and remove the `Gitlab::Routing`.
# For more information: https://gitlab.com/gitlab-org/gitlab/-/issues/299583
Gitlab::Application.routes.set.filter_map { |route| route.name if route.name&.include?('namespace_project') }.each do |name|
new_name = name.sub('namespace_project', 'project')
direct(new_name) do |project, *args|
# This is due to a bug I've found in Rails.
# For more information: https://gitlab.com/gitlab-org/gitlab/-/issues/299591
args.pop if args.last == {}
send("#{name}_url", project&.namespace, project, *args)
end
end
root to: "root#index" root to: "root#index"
get '*unmatched_route', to: 'application#route_not_found' get '*unmatched_route', to: 'application#route_not_found'
end end
Gitlab::Routing.add_helpers(TimeboxesRoutingHelper)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment