summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-09-05 16:18:24 +0200
committerBob Van Landuyt <bob@vanlanduyt.co>2017-10-04 22:46:49 +0200
commit648c082a23f51bdf7151b6c5f716e74c4fe6a5bd (patch)
treef24c86cec8472ed6d37ff62c6f5c67f40af3964f
parentd33e15574b064e38ceadf8aafb47af985d1a7a7a (diff)
downloadgitlab-ce-648c082a23f51bdf7151b6c5f716e74c4fe6a5bd.tar.gz
Render group children using the same entity
-rw-r--r--app/serializers/group_child_entity.rb70
-rw-r--r--spec/serializers/group_child_entity_spec.rb75
2 files changed, 138 insertions, 7 deletions
diff --git a/app/serializers/group_child_entity.rb b/app/serializers/group_child_entity.rb
index 0d2052fe6f3..bc870541795 100644
--- a/app/serializers/group_child_entity.rb
+++ b/app/serializers/group_child_entity.rb
@@ -2,10 +2,12 @@ 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
+ expose :id, :name, :path, :description, :visibility, :full_name, :full_path, :web_url,
+ :created_at, :updated_at, :can_edit, :type, :avatar_url, :permission, :edit_path
+
+ def project?
+ object.is_a?(Project)
+ end
def type
object.class.name.downcase
@@ -14,7 +16,63 @@ class GroupChildEntity < Grape::Entity
def can_edit
return false unless request.respond_to?(:current_user)
- can?(request.current_user, "edit_{type}", object)
+ if project?
+ can?(request.current_user, :admin_project, object)
+ else
+ can?(request.current_user, :admin_group, object)
+ end
+ end
+
+ def edit_path
+ if project?
+ edit_project_path(object)
+ else
+ edit_group_path(object)
+ end
+ end
+
+ def permission
+ if project?
+ object.project_members.find_by(user_id: request.current_user)&.human_access
+ else
+ object.group_members.find_by(user_id: request.current_user)&.human_access
+ end
+ end
+
+ # Project only attributes
+ expose :star_count,
+ if: lambda { |_instance, _options| project? }
+
+ # Group only attributes
+ expose :children_count, :leave_path, :parent_id, :number_projects_with_delimiter,
+ :number_users_with_delimiter, :project_count, :subgroup_count,
+ unless: lambda { |_instance, _options| project? }
+
+ def children_finder
+ @children_finder ||= GroupChildrenFinder.new(request.current_user, parent_group: object)
+ end
+
+ def children_count
+ @children_count ||= children_finder.total_count
+ end
+
+ def project_count
+ @project_count ||= children_finder.project_count
+ end
+
+ def subgroup_count
+ @subgroup_count ||= children_finder.subgroup_count
+ end
+
+ def leave_path
+ leave_group_group_members_path(object)
+ end
+
+ def number_projects_with_delimiter
+ number_with_delimiter(project_count)
+ end
+
+ def number_users_with_delimiter
+ number_with_delimiter(object.users.count)
end
- expose
end
diff --git a/spec/serializers/group_child_entity_spec.rb b/spec/serializers/group_child_entity_spec.rb
index 1c4cdc2a5fb..3a7d2205f53 100644
--- a/spec/serializers/group_child_entity_spec.rb
+++ b/spec/serializers/group_child_entity_spec.rb
@@ -1,15 +1,88 @@
require 'spec_helper'
describe GroupChildEntity do
+ let(:user) { create(:user) }
let(:request) { double('request') }
let(:entity) { described_class.new(object, request: request) }
subject(:json) { entity.as_json }
+ before do
+ allow(request).to receive(:current_user).and_return(user)
+ end
+
+ shared_examples 'group child json' do
+ it 'renders json' do
+ is_expected.not_to be_nil
+ end
+
+ %w[id
+ path
+ full_name
+ full_path
+ avatar_url
+ name
+ description
+ web_url
+ visibility
+ type
+ can_edit
+ visibility
+ edit_path
+ permission].each do |attribute|
+ it "includes #{attribute}" do
+ expect(json[attribute.to_sym]).to be_present
+ end
+ end
+ end
+
describe 'for a project' do
- let(:object) { build_stubbed(:project) }
+ set(:object) do
+ create(:project, :with_avatar,
+ description: 'Awesomeness')
+ end
+
+ before do
+ object.add_master(user)
+ end
it 'has the correct type' do
expect(json[:type]).to eq('project')
end
+
+ it 'includes the star count' do
+ expect(json[:star_count]).to be_present
+ end
+
+ it_behaves_like 'group child json'
+ end
+
+ describe 'for a group', :nested_groups do
+ set(:object) do
+ create(:group, :nested, :with_avatar,
+ description: 'Awesomeness')
+ end
+
+ before do
+ object.add_owner(user)
+ end
+
+ it 'has the correct type' do
+ expect(json[:type]).to eq('group')
+ end
+
+ it 'counts projects and subgroups as children' do
+ create(:project, namespace: object)
+ create(:group, parent: object)
+
+ expect(json[:children_count]).to eq(2)
+ end
+
+ %w[children_count leave_path parent_id number_projects_with_delimiter number_users_with_delimiter project_count subgroup_count].each do |attribute|
+ it "includes #{attribute}" do
+ expect(json[attribute.to_sym]).to be_present
+ end
+ end
+
+ it_behaves_like 'group child json'
end
end