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
|
|
|
|
has_one :route, as: :source, autosave: true, dependent: :destroy
|
|
|
|
|
|
|
|
validates_associated :route
|
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) }
|
|
|
|
|
|
|
|
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.
|
|
|
|
def find_by_full_path(path)
|
|
|
|
# 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.
|
|
|
|
binary = Gitlab::Database.mysql? ? 'BINARY' : ''
|
|
|
|
|
|
|
|
order_sql = "(CASE WHEN #{binary} routes.path = #{connection.quote(path)} THEN 0 ELSE 1 END)"
|
|
|
|
|
2016-12-09 10:27:11 -05:00
|
|
|
where_full_path_in([path]).reorder(order_sql).take
|
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
|
2017-01-05 12:20:12 -05:00
|
|
|
|
|
|
|
# Builds a relation to find multiple objects that are nested under user membership
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# Klass.member_descendants(1)
|
|
|
|
#
|
|
|
|
# Returns an ActiveRecord::Relation.
|
|
|
|
def member_descendants(user_id)
|
|
|
|
joins(:route).
|
|
|
|
joins("INNER JOIN routes r2 ON routes.path LIKE CONCAT(r2.path, '/%')
|
|
|
|
INNER JOIN members ON members.source_id = r2.source_id
|
|
|
|
AND members.source_type = r2.source_type").
|
|
|
|
where('members.user_id = ?', user_id)
|
|
|
|
end
|
2017-03-09 14:27:14 -05:00
|
|
|
|
|
|
|
# Builds a relation to find multiple objects that are nested under user
|
|
|
|
# membership. Includes the parent, as opposed to `#member_descendants`
|
|
|
|
# which only includes the descendants.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# Klass.member_self_and_descendants(1)
|
|
|
|
#
|
|
|
|
# Returns an ActiveRecord::Relation.
|
|
|
|
def member_self_and_descendants(user_id)
|
|
|
|
joins(:route).
|
|
|
|
joins("INNER JOIN routes r2 ON routes.path LIKE CONCAT(r2.path, '/%')
|
|
|
|
OR routes.path = r2.path
|
|
|
|
INNER JOIN members ON members.source_id = r2.source_id
|
|
|
|
AND members.source_type = r2.source_type").
|
|
|
|
where('members.user_id = ?', user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns all objects in a hierarchy, where any node in the hierarchy is
|
|
|
|
# under the user membership.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# Klass.member_hierarchy(1)
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# Given the following group tree...
|
|
|
|
#
|
|
|
|
# _______group_1_______
|
|
|
|
# | |
|
|
|
|
# | |
|
|
|
|
# nested_group_1 nested_group_2
|
|
|
|
# | |
|
|
|
|
# | |
|
|
|
|
# nested_group_1_1 nested_group_2_1
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# ... the following results are returned:
|
|
|
|
#
|
|
|
|
# * the user is a member of group 1
|
|
|
|
# => 'group_1',
|
|
|
|
# 'nested_group_1', nested_group_1_1',
|
|
|
|
# 'nested_group_2', 'nested_group_2_1'
|
|
|
|
#
|
|
|
|
# * the user is a member of nested_group_2
|
|
|
|
# => 'group1',
|
|
|
|
# 'nested_group_2', 'nested_group_2_1'
|
|
|
|
#
|
|
|
|
# * the user is a member of nested_group_2_1
|
|
|
|
# => 'group1',
|
|
|
|
# 'nested_group_2', 'nested_group_2_1'
|
|
|
|
#
|
|
|
|
# Returns an ActiveRecord::Relation.
|
|
|
|
def member_hierarchy(user_id)
|
|
|
|
paths = member_self_and_descendants(user_id).pluck('routes.path')
|
|
|
|
|
|
|
|
return none if paths.empty?
|
|
|
|
|
|
|
|
leaf_paths = paths.group_by(&:length).flat_map(&:last)
|
|
|
|
|
|
|
|
wheres = leaf_paths.map do |leaf_path|
|
|
|
|
"#{connection.quote(leaf_path)} LIKE CONCAT(routes.path, '%')"
|
|
|
|
end
|
|
|
|
|
|
|
|
joins(:route).where(wheres.join(' OR '))
|
|
|
|
end
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
|
2017-02-04 13:26:11 -05:00
|
|
|
def full_name
|
|
|
|
if route && route.name.present?
|
|
|
|
@full_name ||= route.name
|
|
|
|
else
|
|
|
|
update_route if persisted?
|
|
|
|
|
|
|
|
build_full_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_path
|
|
|
|
if route && route.path.present?
|
|
|
|
@full_path ||= route.path
|
|
|
|
else
|
|
|
|
update_route if persisted?
|
|
|
|
|
|
|
|
build_full_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-31 07:00:53 -04:00
|
|
|
private
|
|
|
|
|
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 build_full_path
|
|
|
|
if parent && path
|
|
|
|
parent.full_path + '/' + path
|
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_route
|
|
|
|
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
|
|
|
|
@full_path = nil
|
|
|
|
@full_name = nil
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
end
|