2012-11-27 15:48:25 +00:00
|
|
|
module NamespacesHelper
|
2016-08-08 13:05:58 +00:00
|
|
|
def namespaces_options(selected = :current_user, extra_groups = [], display_path: false)
|
2014-05-28 16:04:42 +00:00
|
|
|
groups = current_user.owned_groups + current_user.masters_groups
|
2016-08-08 13:05:58 +00:00
|
|
|
|
|
|
|
groups += process_extra_groups(extra_groups) if extra_groups.any?
|
|
|
|
|
2013-11-05 15:15:43 +00:00
|
|
|
users = [current_user.namespace]
|
2012-11-27 15:48:25 +00:00
|
|
|
|
2016-04-12 13:41:34 +00:00
|
|
|
data_attr_group = { 'data-options-parent' => 'groups' }
|
|
|
|
data_attr_users = { 'data-options-parent' => 'users' }
|
|
|
|
|
|
|
|
group_opts = [
|
|
|
|
"Groups", groups.sort_by(&:human_name).map { |g| [display_path ? g.path : g.human_name, g.id, data_attr_group] }
|
|
|
|
]
|
|
|
|
|
|
|
|
users_opts = [
|
|
|
|
"Users", users.sort_by(&:human_name).map { |u| [display_path ? u.path : u.human_name, u.id, data_attr_users] }
|
|
|
|
]
|
2012-11-27 15:48:25 +00:00
|
|
|
|
|
|
|
options = []
|
|
|
|
options << group_opts
|
|
|
|
options << users_opts
|
|
|
|
|
|
|
|
if selected == :current_user && current_user.namespace
|
|
|
|
selected = current_user.namespace.id
|
|
|
|
end
|
|
|
|
|
|
|
|
grouped_options_for_select(options, selected)
|
|
|
|
end
|
2013-11-15 13:25:09 +00:00
|
|
|
|
2016-08-08 13:05:58 +00:00
|
|
|
def process_extra_groups(extra_groups)
|
|
|
|
# Remove duplicate groups - we either keep the ones that exist for the user
|
|
|
|
# (already in groups) or ignore those that do not belong to the user.
|
2016-09-09 14:47:38 +00:00
|
|
|
duplicated_groups = extra_groups.map { |name| Namespace.where(name: name).pluck(:name) }
|
2016-08-08 13:05:58 +00:00
|
|
|
extra_groups = extra_groups - duplicated_groups.flatten
|
|
|
|
|
|
|
|
extra_groups.map { |name| Group.new(name: name) }
|
|
|
|
end
|
|
|
|
|
2014-11-14 14:06:39 +00:00
|
|
|
def namespace_icon(namespace, size = 40)
|
|
|
|
if namespace.kind_of?(Group)
|
2015-03-12 20:56:53 +00:00
|
|
|
group_icon(namespace)
|
2014-11-14 14:06:39 +00:00
|
|
|
else
|
|
|
|
avatar_icon(namespace.owner.email, size)
|
|
|
|
end
|
|
|
|
end
|
2012-11-27 15:48:25 +00:00
|
|
|
end
|