namespaces_helper.rb 2.52 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1
module NamespacesHelper
2 3 4 5
  def namespace_id_from(params)
    params.dig(:project, :namespace_id) || params[:namespace_id]
  end

6 7 8 9
  def namespaces_options(selected = :current_user, display_path: false, groups: nil, extra_group: nil, groups_only: false)
    groups ||= current_user.manageable_groups
                 .eager_load(:route)
                 .order('routes.path')
10
    users = [current_user.namespace]
11
    selected_id = selected
12

13 14 15 16
    unless extra_group.nil? || extra_group.is_a?(Group)
      extra_group = Group.find(extra_group) if Namespace.find(extra_group).kind == 'group'
    end

17
    if extra_group && extra_group.is_a?(Group)
18
      extra_group = dedup_extra_group(extra_group)
19 20

      if Ability.allowed?(current_user, :read_group, extra_group)
21
        # Assign the value to an invalid primary ID so that the select box works
22
        extra_group.id = -1 unless extra_group.persisted?
23
        selected_id = extra_group.id if selected == :extra_group
24
        groups |= [extra_group]
25 26
      else
        selected_id = current_user.namespace.id
27
      end
28
    end
29

30 31
    options = []
    options << options_for_group(groups, display_path: display_path, type: 'group')
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
32

33 34 35
    unless groups_only
      options << options_for_group(users, display_path: display_path, type: 'user')

36
      if selected == :current_user && current_user.namespace
37
        selected_id = current_user.namespace.id
38
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
39 40
    end

41
    grouped_options_for_select(options, selected_id)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
42
  end
43

44
  def namespace_icon(namespace, size = 40)
Douwe Maan's avatar
Douwe Maan committed
45
    if namespace.is_a?(Group)
46
      group_icon_url(namespace)
47
    else
48
      avatar_icon_for_user(namespace.owner, size)
49 50
    end
  end
51 52 53

  private

54 55 56 57
  # Many importers create a temporary Group, so use the real
  # group if one exists by that name to prevent duplicates.
  def dedup_extra_group(extra_group)
    unless extra_group.persisted?
58
      existing_group = Group.find_by(path: extra_group.path)
59 60 61 62 63 64
      extra_group = existing_group if existing_group&.persisted?
    end

    extra_group
  end

65 66
  def options_for_group(namespaces, display_path:, type:)
    group_label = type.pluralize
67 68
    elements = namespaces.sort_by(&:human_name).map! do |n|
      [display_path ? n.full_path : n.human_name, n.id,
69
       data: {
70
         options_parent: group_label,
71 72 73
         visibility_level: n.visibility_level_value,
         visibility: n.visibility,
         name: n.name,
74 75
         show_path: (type == 'group') ? group_path(n) : user_path(n),
         edit_path: (type == 'group') ? edit_group_path(n) : nil
76
       }]
77 78
    end

79
    [group_label.camelize, elements]
80
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
81
end