Add create and delete relation links to representers.

pull/1645/head
Richard 10 years ago
parent 68a526844d
commit 1efc1292a0
  1. 12
      lib/api/v3/work_packages/relation_representer.rb
  2. 10
      lib/api/v3/work_packages/work_package_representer.rb
  3. 18
      spec/lib/api/v3/work_packages/work_package_representer_spec.rb

@ -62,6 +62,14 @@ module API
{ href: "#{root_url}api/v3/work_packages/#{represented.model.to_id}" }
end
link :remove do
{
href: "#{root_url}api/v3/relationships/#{represented.model.id}",
method: :delete,
title: "Remove relation"
} if current_user_allowed_to(:manage_work_package_relations)
end
property :delay, getter: -> (*) { model.delay }, render_nil: true, if: -> (*) { model.relation_type == 'precedes' }
def _type
@ -70,6 +78,10 @@ module API
private
def current_user_allowed_to(permission)
@current_user && @current_user.allowed_to?(permission, represented.model.from.project)
end
def relation_type
relation = represented.model
relation.relation_type_for(relation.to).camelize

@ -109,6 +109,14 @@ module API
} if current_user_allowed_to(:add_work_package_watchers, represented.work_package)
end
link :addRelation do
{
href: "#{root_url}/api/v3/work_packages/#{represented.work_package.id}/relations",
method: :post,
title: 'Add relation'
} if current_user_allowed_to(:manage_work_package_relations, represented.work_package)
end
link :parent do
{
href: "#{root_url}/api/v3/work_packages/#{represented.work_package.parent.id}",
@ -160,7 +168,7 @@ module API
end
def relations
represented.relations.map{ |relation| RelationRepresenter.new(relation, work_package: represented.work_package) }
represented.relations.map{ |relation| RelationRepresenter.new(relation, work_package: represented.work_package, current_user: @current_user) }
end
def custom_properties

@ -44,7 +44,7 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
)
}
let(:project) { work_package.project }
let(:permissions) { %i(view_work_packages view_work_package_watchers add_work_package_watchers delete_work_package_watchers) }
let(:permissions) { %i(view_work_packages view_work_package_watchers add_work_package_watchers delete_work_package_watchers manage_work_package_relations) }
let(:role) { FactoryGirl.create :role, permissions: permissions }
before(:each) do
@ -150,6 +150,22 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
expect(subject).to_not have_json_path('_links/add_watcher/href')
end
end
context 'when the user has the permission to manage relations' do
it 'should have a link to add relation' do
expect(subject).to have_json_path('_links/addRelation/href')
end
end
context 'when the user does not have the permission to manage relations' do
before do
role.permissions.delete(:manage_work_package_relations) and role.save
end
it 'should not have a link to add relation' do
expect(subject).to_not have_json_path('_links/addRelation/href')
end
end
end
describe '_embedded' do

Loading…
Cancel
Save