diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-09-05 12:30:16 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-10-04 23:46:49 +0300 |
commit | d33e15574b064e38ceadf8aafb47af985d1a7a7a (patch) | |
tree | 0852a044ceef545d7cdc5b17ee0ab70cee1f3e63 | |
parent | 376a8c66c1ca8ee2a95255d21c9d55ce006ab655 (diff) |
Add serializer for group children
-rw-r--r-- | app/controllers/groups_controller.rb | 15 | ||||
-rw-r--r-- | app/serializers/group_child_entity.rb | 20 | ||||
-rw-r--r-- | app/serializers/group_child_serializer.rb | 5 | ||||
-rw-r--r-- | spec/serializers/group_child_entity_spec.rb | 15 |
4 files changed, 55 insertions, 0 deletions
diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index b8def9f5812..17d40cedd5d 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -60,7 +60,22 @@ class GroupsController < Groups::ApplicationController end end + def children + parent = Group.find_by(parent_id: params[:parent_id]) || @group + if parent.nil? || !can?(current_user, :read_group, parent) + render_404 + end + + @children = GroupChildrenFinder.new(current_user, parent_group: parent, params: params).execute + respond_to do |format| + format.json do + render json: GroupChildrenSerializer + .new(current_user: current_user) + .with_pagination(request, response) + .represent(@children) + end + end end def activity diff --git a/app/serializers/group_child_entity.rb b/app/serializers/group_child_entity.rb new file mode 100644 index 00000000000..0d2052fe6f3 --- /dev/null +++ b/app/serializers/group_child_entity.rb @@ -0,0 +1,20 @@ +class GroupChildEntity < Grape::Entity + include ActionView::Helpers::NumberHelper + include RequestAwareEntity + + expose :id, :name, :description, :visibility, :full_name, :full_path, :web_url, + :created_at, :updated_at, :star_count, :can_edit, :type, :parent_id, + :children_count, :leave_path, :edit_path, :number_projects_with_delimiter, + :number_users_with_delimiter, :permissions, :star_count + + def type + object.class.name.downcase + end + + def can_edit + return false unless request.respond_to?(:current_user) + + can?(request.current_user, "edit_{type}", object) + end + expose +end diff --git a/app/serializers/group_child_serializer.rb b/app/serializers/group_child_serializer.rb new file mode 100644 index 00000000000..fbf4a6783b9 --- /dev/null +++ b/app/serializers/group_child_serializer.rb @@ -0,0 +1,5 @@ +class GroupChildSerializer < BaseSerializer + include WithPagination + + entity GroupChildEntity +end diff --git a/spec/serializers/group_child_entity_spec.rb b/spec/serializers/group_child_entity_spec.rb new file mode 100644 index 00000000000..1c4cdc2a5fb --- /dev/null +++ b/spec/serializers/group_child_entity_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe GroupChildEntity do + let(:request) { double('request') } + let(:entity) { described_class.new(object, request: request) } + subject(:json) { entity.as_json } + + describe 'for a project' do + let(:object) { build_stubbed(:project) } + + it 'has the correct type' do + expect(json[:type]).to eq('project') + end + end +end |