2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-31 07:00:53 -04:00
|
|
|
# Store object full path in separate table for easy lookup and uniq validation
|
2017-02-04 13:26:11 -05:00
|
|
|
# Object must have name and path db fields and respond to parent and parent_changed? methods.
|
2016-10-31 07:00:53 -04:00
|
|
|
module Routable
|
|
|
|
extend ActiveSupport::Concern
|
2020-12-15 10:09:59 -05:00
|
|
|
include CaseSensitivity
|
|
|
|
|
|
|
|
# Finds a Routable object by its full path, without knowing the class.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# Routable.find_by_full_path('groupname') # -> Group
|
|
|
|
# Routable.find_by_full_path('groupname/projectname') # -> Project
|
|
|
|
#
|
|
|
|
# Returns a single object, or nil.
|
|
|
|
def self.find_by_full_path(path, follow_redirects: false, route_scope: Route, redirect_route_scope: RedirectRoute)
|
|
|
|
return unless path.present?
|
|
|
|
|
|
|
|
# Case sensitive match first (it's cheaper and the usual case)
|
|
|
|
# If we didn't have an exact match, we perform a case insensitive search
|
|
|
|
#
|
|
|
|
# We need to qualify the columns with the table name, to support both direct lookups on
|
|
|
|
# Route/RedirectRoute, and scoped lookups through the Routable classes.
|
|
|
|
route =
|
|
|
|
route_scope.find_by(routes: { path: path }) ||
|
|
|
|
route_scope.iwhere(Route.arel_table[:path] => path).take
|
|
|
|
|
|
|
|
if follow_redirects
|
|
|
|
route ||= redirect_route_scope.iwhere(RedirectRoute.arel_table[:path] => path).take
|
|
|
|
end
|
|
|
|
|
|
|
|
return unless route
|
|
|
|
|
|
|
|
route.is_a?(Routable) ? route : route.source
|
|
|
|
end
|
2016-10-31 07:00:53 -04:00
|
|
|
|
|
|
|
included do
|
2018-05-10 20:19:59 -04:00
|
|
|
# Remove `inverse_of: source` when upgraded to rails 5.2
|
|
|
|
# See https://github.com/rails/rails/pull/28808
|
|
|
|
has_one :route, as: :source, autosave: true, dependent: :destroy, inverse_of: :source # rubocop:disable Cop/ActiveRecordDependent
|
2017-06-08 11:16:27 -04:00
|
|
|
has_many :redirect_routes, as: :source, autosave: true, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2016-10-31 07:00:53 -04:00
|
|
|
|
2016-12-07 12:16:02 -05:00
|
|
|
validates :route, presence: true
|
2016-10-31 07:00:53 -04:00
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
scope :with_route, -> { includes(:route) }
|
|
|
|
|
2018-01-31 11:52:09 -05:00
|
|
|
after_validation :set_path_errors
|
|
|
|
|
2020-07-02 08:09:02 -04:00
|
|
|
before_validation :prepare_route
|
|
|
|
before_save :prepare_route # in case validation is skipped
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
# Finds a single object by full path match in routes table.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
2019-09-18 10:02:45 -04:00
|
|
|
# Klass.find_by_full_path('gitlab-org/gitlab-foss')
|
2016-10-31 07:00:53 -04:00
|
|
|
#
|
|
|
|
# Returns a single object, or nil.
|
2017-05-01 16:46:30 -04:00
|
|
|
def find_by_full_path(path, follow_redirects: false)
|
2020-12-15 10:09:59 -05:00
|
|
|
# TODO: Optimize these queries by avoiding joins
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/292252
|
|
|
|
Routable.find_by_full_path(
|
|
|
|
path,
|
|
|
|
follow_redirects: follow_redirects,
|
|
|
|
route_scope: includes(:route).references(:routes),
|
|
|
|
redirect_route_scope: joins(:redirect_routes)
|
|
|
|
)
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Builds a relation to find multiple objects by their full paths.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
2019-09-18 10:02:45 -04:00
|
|
|
# Klass.where_full_path_in(%w{gitlab-org/gitlab-foss gitlab-org/gitlab})
|
2016-10-31 07:00:53 -04:00
|
|
|
#
|
|
|
|
# Returns an ActiveRecord::Relation.
|
2019-09-26 02:06:27 -04:00
|
|
|
def where_full_path_in(paths, use_includes: true)
|
2019-06-13 09:12:28 -04:00
|
|
|
return none if paths.empty?
|
2016-10-31 07:00:53 -04:00
|
|
|
|
2019-06-13 09:12:28 -04:00
|
|
|
wheres = paths.map do |path|
|
|
|
|
"(LOWER(routes.path) = LOWER(#{connection.quote(path)}))"
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
|
2019-09-26 02:06:27 -04:00
|
|
|
route =
|
|
|
|
if use_includes
|
|
|
|
includes(:route).references(:routes)
|
|
|
|
else
|
|
|
|
joins(:route)
|
|
|
|
end
|
|
|
|
|
|
|
|
route.where(wheres.join(' OR '))
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
def full_name
|
2021-05-13 05:10:44 -04:00
|
|
|
# We have to test for persistence as the cache key uses #updated_at
|
|
|
|
return (route&.name || build_full_name) unless persisted? && Feature.enabled?(:cached_route_lookups, self, type: :ops, default_enabled: :yaml)
|
|
|
|
|
|
|
|
# Return the name as-is if the parent is missing
|
|
|
|
return name if route.nil? && parent.nil? && name.present?
|
|
|
|
|
|
|
|
# If the route is already preloaded, return directly, preventing an extra load
|
|
|
|
return route.name if route_loaded? && route.present?
|
|
|
|
|
|
|
|
# Similarly, we can allow the build if the parent is loaded
|
|
|
|
return build_full_name if parent_loaded?
|
|
|
|
|
|
|
|
Gitlab::Cache.fetch_once([cache_key, :full_name]) do
|
|
|
|
route&.name || build_full_name
|
|
|
|
end
|
2017-02-04 13:26:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def full_path
|
2021-05-13 05:10:44 -04:00
|
|
|
# We have to test for persistence as the cache key uses #updated_at
|
|
|
|
return (route&.path || build_full_path) unless persisted? && Feature.enabled?(:cached_route_lookups, self, type: :ops, default_enabled: :yaml)
|
|
|
|
|
|
|
|
# Return the path as-is if the parent is missing
|
|
|
|
return path if route.nil? && parent.nil? && path.present?
|
|
|
|
|
|
|
|
# If the route is already preloaded, return directly, preventing an extra load
|
|
|
|
return route.path if route_loaded? && route.present?
|
|
|
|
|
|
|
|
# Similarly, we can allow the build if the parent is loaded
|
|
|
|
return build_full_path if parent_loaded?
|
|
|
|
|
|
|
|
Gitlab::Cache.fetch_once([cache_key, :full_path]) do
|
|
|
|
route&.path || build_full_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Overriden in the Project model
|
|
|
|
# parent_id condition prevents issues with parent reassignment
|
|
|
|
def parent_loaded?
|
|
|
|
association(:parent).loaded?
|
|
|
|
end
|
|
|
|
|
|
|
|
def route_loaded?
|
|
|
|
association(:route).loaded?
|
2017-06-29 17:53:32 -04:00
|
|
|
end
|
|
|
|
|
2017-10-03 06:34:24 -04:00
|
|
|
def full_path_components
|
|
|
|
full_path.split('/')
|
|
|
|
end
|
|
|
|
|
2017-06-16 06:11:33 -04:00
|
|
|
def build_full_path
|
|
|
|
if parent && path
|
|
|
|
parent.full_path + '/' + path
|
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-23 11:48:26 -04:00
|
|
|
# Group would override this to check from association
|
|
|
|
def owned_by?(user)
|
|
|
|
owner == user
|
|
|
|
end
|
|
|
|
|
2017-04-24 01:32:50 -04:00
|
|
|
private
|
|
|
|
|
2018-01-31 11:52:09 -05:00
|
|
|
def set_path_errors
|
|
|
|
route_path_errors = self.errors.delete(:"route.path")
|
2021-04-23 05:10:03 -04:00
|
|
|
route_path_errors&.each do |msg|
|
|
|
|
self.errors.add(:path, msg)
|
|
|
|
end
|
2018-01-31 11:52:09 -05:00
|
|
|
end
|
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
def full_name_changed?
|
|
|
|
name_changed? || parent_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_path_changed?
|
|
|
|
path_changed? || parent_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_full_name
|
|
|
|
if parent && name
|
|
|
|
parent.human_name + ' / ' + name
|
|
|
|
else
|
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_route
|
2020-07-02 08:09:02 -04:00
|
|
|
return unless full_path_changed? || full_name_changed?
|
|
|
|
|
2016-10-31 07:00:53 -04:00
|
|
|
route || build_route(source: self)
|
2017-02-04 13:26:11 -05:00
|
|
|
route.path = build_full_path
|
|
|
|
route.name = build_full_name
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
end
|