diff options
-rw-r--r-- | app/controllers/dashboard/groups_controller.rb | 27 | ||||
-rw-r--r-- | app/serializers/group_entity.rb | 16 | ||||
-rw-r--r-- | app/serializers/group_serializer.rb | 19 | ||||
-rw-r--r-- | app/views/dashboard/groups/_groups.html.haml | 5 | ||||
-rw-r--r-- | app/views/dashboard/groups/index.html.haml | 2 |
5 files changed, 59 insertions, 10 deletions
diff --git a/app/controllers/dashboard/groups_controller.rb b/app/controllers/dashboard/groups_controller.rb index d03265e9f20..b339344dd40 100644 --- a/app/controllers/dashboard/groups_controller.rb +++ b/app/controllers/dashboard/groups_controller.rb @@ -1,16 +1,29 @@ class Dashboard::GroupsController < Dashboard::ApplicationController def index - @group_members = current_user.group_members.includes(source: :route).joins(:group) - @group_members = @group_members.merge(Group.search(params[:filter_groups])) if params[:filter_groups].present? - @group_members = @group_members.merge(Group.sort(@sort = params[:sort])) - @group_members = @group_members.page(params[:page]) + @groups = if params[:parent_id] + parent = Group.find(params[:parent_id]) + + if parent.users_with_parents.find_by(id: current_user) + Group.where(id: parent.children) + else + Group.none + end + else + Group.joins(:group_members).merge(current_user.group_members) + end + + @groups = @groups.search(params[:filter_groups]) if params[:filter_groups].present? + @groups = @groups.includes(:route) + @groups = @groups.sort(@sort = params[:sort]) + @groups = @groups.page(params[:page]) respond_to do |format| format.html format.json do - render json: { - html: view_to_html_string("dashboard/groups/_groups", locals: { group_members: @group_members }) - } + render json: GroupSerializer + .new(current_user: @current_user) + .with_pagination(request, response) + .represent(@groups) end end end diff --git a/app/serializers/group_entity.rb b/app/serializers/group_entity.rb new file mode 100644 index 00000000000..33f1fbff31d --- /dev/null +++ b/app/serializers/group_entity.rb @@ -0,0 +1,16 @@ +class GroupEntity < Grape::Entity + include RequestAwareEntity + + expose :id, :name, :path, :description, :visibility + expose :avatar_url + expose :web_url + expose :full_name, :full_path + expose :parent_id + expose :created_at, :updated_at + + expose :permissions do + expose :group_access do |group, options| + group.group_members.find_by(user_id: request.current_user)&.access_level + end + end +end diff --git a/app/serializers/group_serializer.rb b/app/serializers/group_serializer.rb new file mode 100644 index 00000000000..26e8566828b --- /dev/null +++ b/app/serializers/group_serializer.rb @@ -0,0 +1,19 @@ +class GroupSerializer < BaseSerializer + entity GroupEntity + + def with_pagination(request, response) + tap { @paginator = Gitlab::Serializer::Pagination.new(request, response) } + end + + def paginated? + @paginator.present? + end + + def represent(resource, opts = {}) + if paginated? + super(@paginator.paginate(resource), opts) + else + super(resource, opts) + end + end +end diff --git a/app/views/dashboard/groups/_groups.html.haml b/app/views/dashboard/groups/_groups.html.haml index 6c3bf1a2b3b..d33ee450b29 100644 --- a/app/views/dashboard/groups/_groups.html.haml +++ b/app/views/dashboard/groups/_groups.html.haml @@ -1,6 +1,7 @@ .js-groups-list-holder %ul.content-list - - @group_members.each do |group_member| + - @groups.each do |group| + - group_member = group.group_members.find_by(user_id: current_user) = render 'shared/groups/group', group: group_member.group, group_member: group_member - = paginate @group_members, theme: 'gitlab' + = paginate @groups, theme: 'gitlab' diff --git a/app/views/dashboard/groups/index.html.haml b/app/views/dashboard/groups/index.html.haml index 73ab2c95ff9..0d35d11fb63 100644 --- a/app/views/dashboard/groups/index.html.haml +++ b/app/views/dashboard/groups/index.html.haml @@ -2,7 +2,7 @@ - header_title "Groups", dashboard_groups_path = render 'dashboard/groups_head' -- if @group_members.empty? +- if @groups.empty? = render 'empty_state' - else = render 'groups' |