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
|
|
|
|
|
|
|
|
included do
|
2017-06-08 11:16:27 -04:00
|
|
|
has_one :route, as: :source, autosave: true, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
|
|
|
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
|
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
before_validation do
|
|
|
|
if full_path_changed? || full_name_changed?
|
|
|
|
prepare_route
|
|
|
|
end
|
|
|
|
end
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
# Finds a single object by full path match in routes table.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# Klass.find_by_full_path('gitlab-org/gitlab-ce')
|
|
|
|
#
|
|
|
|
# Returns a single object, or nil.
|
2017-05-01 16:46:30 -04:00
|
|
|
def find_by_full_path(path, follow_redirects: false)
|
2016-10-31 07:00:53 -04:00
|
|
|
# On MySQL we want to ensure the ORDER BY uses a case-sensitive match so
|
|
|
|
# any literal matches come first, for this we have to use "BINARY".
|
|
|
|
# Without this there's still no guarantee in what order MySQL will return
|
|
|
|
# rows.
|
2017-05-01 16:46:30 -04:00
|
|
|
#
|
|
|
|
# Why do we do this?
|
|
|
|
#
|
|
|
|
# Even though we have Rails validation on Route for unique paths
|
|
|
|
# (case-insensitive), there are old projects in our DB (and possibly
|
|
|
|
# clients' DBs) that have the same path with different cases.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/18603. Also note that
|
|
|
|
# our unique index is case-sensitive in Postgres.
|
2016-10-31 07:00:53 -04:00
|
|
|
binary = Gitlab::Database.mysql? ? 'BINARY' : ''
|
|
|
|
order_sql = "(CASE WHEN #{binary} routes.path = #{connection.quote(path)} THEN 0 ELSE 1 END)"
|
2017-05-01 16:46:30 -04:00
|
|
|
found = where_full_path_in([path]).reorder(order_sql).take
|
|
|
|
return found if found
|
2016-10-31 07:00:53 -04:00
|
|
|
|
2017-05-01 16:46:30 -04:00
|
|
|
if follow_redirects
|
|
|
|
if Gitlab::Database.postgresql?
|
|
|
|
joins(:redirect_routes).find_by("LOWER(redirect_routes.path) = LOWER(?)", path)
|
|
|
|
else
|
2017-05-06 04:45:25 -04:00
|
|
|
joins(:redirect_routes).find_by(redirect_routes: { path: path })
|
2017-05-01 16:46:30 -04:00
|
|
|
end
|
|
|
|
end
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Builds a relation to find multiple objects by their full paths.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
2016-12-09 10:27:11 -05:00
|
|
|
# Klass.where_full_path_in(%w{gitlab-org/gitlab-ce gitlab-org/gitlab-ee})
|
2016-10-31 07:00:53 -04:00
|
|
|
#
|
|
|
|
# Returns an ActiveRecord::Relation.
|
2016-12-09 10:27:11 -05:00
|
|
|
def where_full_path_in(paths)
|
2016-10-31 07:00:53 -04:00
|
|
|
wheres = []
|
|
|
|
cast_lower = Gitlab::Database.postgresql?
|
|
|
|
|
|
|
|
paths.each do |path|
|
|
|
|
path = connection.quote(path)
|
|
|
|
|
2017-03-17 17:26:46 -04:00
|
|
|
where =
|
|
|
|
if cast_lower
|
|
|
|
"(LOWER(routes.path) = LOWER(#{path}))"
|
|
|
|
else
|
|
|
|
"(routes.path = #{path})"
|
|
|
|
end
|
2016-10-31 07:00:53 -04:00
|
|
|
|
|
|
|
wheres << where
|
|
|
|
end
|
|
|
|
|
|
|
|
if wheres.empty?
|
|
|
|
none
|
|
|
|
else
|
|
|
|
joins(:route).where(wheres.join(' OR '))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
def full_name
|
|
|
|
if route && route.name.present?
|
2017-11-22 02:50:36 -05:00
|
|
|
@full_name ||= route.name # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-02-04 13:26:11 -05:00
|
|
|
else
|
|
|
|
update_route if persisted?
|
|
|
|
|
|
|
|
build_full_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-24 01:32:50 -04:00
|
|
|
# Every time `project.namespace.becomes(Namespace)` is called for polymorphic_path,
|
|
|
|
# a new instance is instantiated, and we end up duplicating the same query to retrieve
|
|
|
|
# the route. Caching this per request ensures that even if we have multiple instances,
|
|
|
|
# we will not have to duplicate work, avoiding N+1 queries in some cases.
|
2017-02-04 13:26:11 -05:00
|
|
|
def full_path
|
2017-04-24 01:32:50 -04:00
|
|
|
return uncached_full_path unless RequestStore.active?
|
|
|
|
|
2017-06-29 17:53:32 -04:00
|
|
|
RequestStore[full_path_key] ||= uncached_full_path
|
|
|
|
end
|
|
|
|
|
2017-10-03 06:34:24 -04:00
|
|
|
def full_path_components
|
|
|
|
full_path.split('/')
|
|
|
|
end
|
|
|
|
|
2017-06-29 17:53:32 -04:00
|
|
|
def expires_full_path_cache
|
|
|
|
RequestStore.delete(full_path_key) if RequestStore.active?
|
2017-11-22 02:50:36 -05:00
|
|
|
@full_path = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-04-24 01:32:50 -04:00
|
|
|
end
|
|
|
|
|
2017-06-16 06:11:33 -04:00
|
|
|
def build_full_path
|
|
|
|
if parent && path
|
|
|
|
parent.full_path + '/' + path
|
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
|
|
|
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")
|
|
|
|
self.errors[:path].concat(route_path_errors) if route_path_errors
|
|
|
|
end
|
|
|
|
|
2017-04-24 01:32:50 -04:00
|
|
|
def uncached_full_path
|
2017-02-04 13:26:11 -05:00
|
|
|
if route && route.path.present?
|
2017-11-22 02:50:36 -05:00
|
|
|
@full_path ||= route.path # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-02-04 13:26:11 -05:00
|
|
|
else
|
|
|
|
update_route if persisted?
|
|
|
|
|
|
|
|
build_full_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_name_changed?
|
|
|
|
name_changed? || parent_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_path_changed?
|
|
|
|
path_changed? || parent_changed?
|
|
|
|
end
|
|
|
|
|
2017-06-29 17:53:32 -04:00
|
|
|
def full_path_key
|
|
|
|
@full_path_key ||= "routable/full_path/#{self.class.name}/#{self.id}"
|
|
|
|
end
|
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
def build_full_name
|
|
|
|
if parent && name
|
|
|
|
parent.human_name + ' / ' + name
|
|
|
|
else
|
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_route
|
2017-09-19 03:44:58 -04:00
|
|
|
return if Gitlab::Database.read_only?
|
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
prepare_route
|
|
|
|
route.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_route
|
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
|
2017-11-22 02:50:36 -05:00
|
|
|
@full_path = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
@full_name = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
end
|