summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-10-11 14:39:23 +0200
committerBob Van Landuyt <bob@vanlanduyt.co>2017-10-12 11:36:54 +0200
commit8cde1e3285c870c85bee3a9a9ff4b8e5f53cff86 (patch)
tree207a39cabbbf48f5cf9e9cb17a01e55c09a3eae1
parentbd8943f5adfc377491bedb2a794d8c39b2b4c45e (diff)
downloadgitlab-ce-8cde1e3285c870c85bee3a9a9ff4b8e5f53cff86.tar.gz
Use polymorphism for common attributes in `GroupChildEntity`
-rw-r--r--app/serializers/group_child_entity.rb28
-rw-r--r--spec/serializers/group_child_entity_spec.rb11
2 files changed, 22 insertions, 17 deletions
diff --git a/app/serializers/group_child_entity.rb b/app/serializers/group_child_entity.rb
index 5c1fa72b1ac..37240bfb0b1 100644
--- a/app/serializers/group_child_entity.rb
+++ b/app/serializers/group_child_entity.rb
@@ -6,33 +6,25 @@ class GroupChildEntity < Grape::Entity
:created_at, :updated_at, :avatar_url
expose :type do |instance|
- instance.class.name.downcase
+ type
end
expose :can_edit do |instance|
return false unless request.respond_to?(:current_user)
- if project?
- can?(request.current_user, :admin_project, instance)
- else
- can?(request.current_user, :admin_group, instance)
- end
+ can?(request.current_user, "admin_#{type}", instance)
end
expose :edit_path do |instance|
- if project?
- edit_project_path(instance)
- else
- edit_group_path(instance)
- end
+ # We know `type` will be one either `project` or `group`.
+ # The `edit_polymorphic_path` helper would try to call the path helper
+ # with a plural: `edit_groups_path(instance)` or `edit_projects_path(instance)`
+ # while our methods are `edit_group_path` or `edit_group_path`
+ public_send("edit_#{type}_path", instance) # rubocop:disable GitlabSecurity/PublicSend
end
expose :relative_path do |instance|
- if project?
- project_path(instance)
- else
- group_path(instance)
- end
+ polymorphic_path(instance)
end
expose :permission do |instance|
@@ -78,4 +70,8 @@ class GroupChildEntity < Grape::Entity
def project?
object.is_a?(Project)
end
+
+ def type
+ object.class.name.downcase
+ end
end
diff --git a/spec/serializers/group_child_entity_spec.rb b/spec/serializers/group_child_entity_spec.rb
index 64000385781..452754d7a79 100644
--- a/spec/serializers/group_child_entity_spec.rb
+++ b/spec/serializers/group_child_entity_spec.rb
@@ -1,6 +1,8 @@
require 'spec_helper'
describe GroupChildEntity do
+ include Gitlab::Routing.url_helpers
+
let(:user) { create(:user) }
let(:request) { double('request') }
let(:entity) { described_class.new(object, request: request) }
@@ -24,7 +26,6 @@ describe GroupChildEntity do
type
can_edit
visibility
- edit_path
permission
relative_path].each do |attribute|
it "includes #{attribute}" do
@@ -51,6 +52,10 @@ describe GroupChildEntity do
expect(json[:star_count]).to be_present
end
+ it 'has the correct edit path' do
+ expect(json[:edit_path]).to eq(edit_project_path(object))
+ end
+
it_behaves_like 'group child json'
end
@@ -87,6 +92,10 @@ describe GroupChildEntity do
expect(json[:can_leave]).to be_truthy
end
+ it 'has the correct edit path' do
+ expect(json[:edit_path]).to eq(edit_group_path(object))
+ end
+
it_behaves_like 'group child json'
end
end