Use API V3's path helper

pull/2239/head
Hagen Schink 10 years ago committed by Jens Ulferts
parent 6af688abf4
commit 746f7cce74
  1. 33
      config/initializers/grape.rb
  2. 2
      lib/api/decorators/collection.rb
  3. 19
      lib/api/v3/activities/activity_representer.rb
  4. 17
      lib/api/v3/attachments/attachment_representer.rb
  5. 4
      lib/api/v3/categories/categories_api.rb
  6. 2
      lib/api/v3/priorities/priorities_api.rb
  7. 2
      lib/api/v3/projects/available_assignees_api.rb
  8. 2
      lib/api/v3/projects/available_responsibles_api.rb
  9. 8
      lib/api/v3/projects/project_representer.rb
  10. 7
      lib/api/v3/queries/query_representer.rb
  11. 12
      lib/api/v3/root_representer.rb
  12. 4
      lib/api/v3/statuses/status_representer.rb
  13. 2
      lib/api/v3/statuses/statuses_api.rb
  14. 9
      lib/api/v3/users/user_representer.rb
  15. 2
      lib/api/v3/versions/versions_api.rb
  16. 11
      lib/api/v3/work_packages/form/form_representer.rb
  17. 8
      lib/api/v3/work_packages/form/work_package_attribute_links_representer.rb
  18. 20
      lib/api/v3/work_packages/form/work_package_schema_representer.rb
  19. 10
      lib/api/v3/work_packages/relation_representer.rb
  20. 32
      lib/api/v3/work_packages/work_package_representer.rb

@ -0,0 +1,33 @@
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2014 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
module Grape
class Endpoint
include ::API::V3::Utilities::PathHelper
end
end

@ -48,7 +48,7 @@ module API
as_strategy = API::Utilities::CamelCasingStrategy.new
link :self do
{ href: "#{root_path}api/v3/#{@self_link}" }
{ href: @self_link }
end
property :_type, getter: -> (*) { 'Collection' }

@ -36,7 +36,7 @@ module API
class ActivityRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include API::Utilities::UrlHelper
include API::V3::Utilities::PathHelper
include OpenProject::TextFormatting
self.as_strategy = API::Utilities::CamelCasingStrategy.new
@ -50,20 +50,29 @@ module API
property :_type, exec_context: :decorator
link :self do
{ href: "#{root_path}api/v3/activities/#{represented.id}", title: "#{represented.id}" }
{
href: api_v3_paths.activity(represented.id),
title: "#{represented.id}"
}
end
link :workPackage do
{ href: "#{root_path}api/v3/work_packages/#{represented.journable.id}", title: "#{represented.journable.subject}" }
{
href: api_v3_paths.work_package(represented.journable.id),
title: "#{represented.journable.subject}"
}
end
link :user do
{ href: "#{root_path}api/v3/users/#{represented.user.id}", title: "#{represented.user.name} - #{represented.user.login}" }
{
href: api_v3_paths.user(represented.user.id),
title: "#{represented.user.name} - #{represented.user.login}"
}
end
link :update do
{
href: "#{root_path}api/v3/activities/#{represented.id}",
href: api_v3_paths.activity(represented.id),
method: :patch,
title: "#{represented.id}"
} if current_user_allowed_to_edit?

@ -36,24 +36,33 @@ module API
class AttachmentRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include OpenProject::StaticRouting::UrlHelpers
include API::V3::Utilities::PathHelper
self.as_strategy = API::Utilities::CamelCasingStrategy.new
property :_type, exec_context: :decorator
link :self do
{ href: "#{root_path}api/v3/attachments/#{represented.id}", title: "#{represented.filename}" }
{
href: api_v3_paths.attachment(represented.id),
title: "#{represented.filename}"
}
end
link :work_package do
work_package = represented.container
{ href: "#{root_path}api/v3/work_packages/#{work_package.id}", title: "#{work_package.subject}" } unless work_package.nil?
{
href: api_v3_paths.work_package(work_package.id),
title: "#{work_package.subject}"
} unless work_package.nil?
end
link :author do
author = represented.author
{ href: "#{root_path}api/v3/users/#{author.id}", title: "#{author.name} - #{author.login}" } unless author.nil?
{
href: api_v3_paths.user(author.id),
title: "#{author.name} - #{author.login}"
} unless author.nil?
end
property :id, render_nil: true

@ -37,9 +37,11 @@ module API
end
get do
self_link = api_v3_paths.categories(@project.identifier)
CategoryCollectionRepresenter.new(@categories,
@categories.count,
"projects/#{@project.identifier}/categories")
self_link)
end
end
end

@ -39,7 +39,7 @@ module API
get do
PriorityCollectionRepresenter.new(@priorities,
@priorities.count,
'priorities')
api_v3_paths.priorities)
end
end
end

@ -36,7 +36,7 @@ module API
available_assignees = @project.possible_assignees
total = available_assignees.count
self_link = "projects/#{@project.id}/available_assignees"
self_link = api_v3_paths.available_assignees(@project.id)
::API::V3::Users::UserCollectionRepresenter.new(available_assignees,
total,
self_link)

@ -36,7 +36,7 @@ module API
available_responsibles = @project.possible_responsibles
total = available_responsibles.count
self_link = "projects/#{@project.id}/available_responsibles"
self_link = api_v3_paths.available_responsibles(@project.id)
::API::V3::Users::UserCollectionRepresenter.new(available_responsibles,
total,
self_link)

@ -36,7 +36,7 @@ module API
class ProjectRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include OpenProject::StaticRouting::UrlHelpers
include API::V3::Utilities::PathHelper
self.as_strategy = API::Utilities::CamelCasingStrategy.new
@ -44,17 +44,17 @@ module API
link :self do
{
href: "#{root_path}api/v3/projects/#{represented.id}",
href: api_v3_paths.project(represented.id),
title: "#{represented.name}"
}
end
link 'categories' do
"#{root_path}api/v3/projects/#{represented.id}/categories"
{ href: api_v3_paths.categories(represented.id) }
end
link 'versions' do
"#{root_path}api/v3/projects/#{represented.id}/versions"
{ href: api_v3_paths.versions(represented.id) }
end
property :id, render_nil: true

@ -36,14 +36,17 @@ module API
class QueryRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include OpenProject::StaticRouting::UrlHelpers
include API::V3::Utilities::PathHelper
self.as_strategy = API::Utilities::CamelCasingStrategy.new
property :_type, exec_context: :decorator
link :self do
{ href: "#{root_path}api/v3/queries/#{represented.id}", title: "#{represented.name}" }
{
href: api_v3_paths.query(represented.id),
title: "#{represented.name}"
}
end
property :id, render_nil: true

@ -35,23 +35,27 @@ module API
class RootRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include OpenProject::StaticRouting::UrlHelpers
include API::V3::Utilities::PathHelper
self.as_strategy = ::API::Utilities::CamelCasingStrategy.new
link 'priorities' do
"#{root_path}api/v3/priorities"
{
href: api_v3_paths.priorities
}
end
link 'project' do
{
href: "#{root_path}api/v3/project/{project_id}",
href: api_v3_paths.projects + '/{project_id}',
templated: true
}
end
link 'statuses' do
"#{root_path}api/v3/statuses"
{
href: api_v3_paths.statuses
}
end
end
end

@ -36,7 +36,7 @@ module API
class StatusRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include OpenProject::StaticRouting::UrlHelpers
include API::V3::Utilities::PathHelper
self.as_strategy = API::Utilities::CamelCasingStrategy.new
@ -44,7 +44,7 @@ module API
link :self do
{
href: "#{root_path}api/v3/statuses/#{represented.id}",
href: api_v3_paths.status(represented.id),
title: "#{represented.name}"
}
end

@ -39,7 +39,7 @@ module API
get do
StatusCollectionRepresenter.new(@statuses,
@statuses.count,
'statuses')
api_v3_paths.statuses)
end
namespace ':id' do

@ -36,7 +36,7 @@ module API
class UserRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include OpenProject::StaticRouting::UrlHelpers
include API::V3::Utilities::PathHelper
include AvatarHelper
self.as_strategy = API::Utilities::CamelCasingStrategy.new
@ -52,12 +52,15 @@ module API
property :_type, exec_context: :decorator
link :self do
{ href: "#{root_path}api/v3/users/#{represented.id}", title: "#{represented.name} - #{represented.login}" }
{
href: api_v3_paths.user(represented.id),
title: "#{represented.name} - #{represented.login}"
}
end
link :removeWatcher do
{
href: "#{root_path}api/v3/work_packages/#{@work_package.id}/watchers/#{represented.id}",
href: api_v3_paths.watcher(represented.id, @work_package.id),
method: :delete,
title: 'Remove watcher'
} if @work_package && current_user_allowed_to(:delete_work_package_watchers, @work_package)

@ -39,7 +39,7 @@ module API
get do
VersionCollectionRepresenter.new(@versions,
@versions.count,
"projects/#{@project.identifier}/versions")
api_v3_paths.versions(@project.identifier))
end
end
end

@ -37,7 +37,7 @@ module API
class FormRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include API::Utilities::UrlHelper
include API::V3::Utilities::PathHelper
include OpenProject::TextFormatting
self.as_strategy = ::API::Utilities::CamelCasingStrategy.new
@ -52,28 +52,27 @@ module API
link :self do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}/form",
href: api_v3_paths.work_package_form(represented.id),
}
end
link :validate do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}/form",
href: api_v3_paths.work_package_form(represented.id),
method: :post
}
end
link :previewMarkup do
{
href: "#{root_path}api/v3/render/textile?"\
"#{root_path}api/v3/work_packages/#{represented.id}",
href: api_v3_paths.preview_textile(api_v3_paths.work_package(represented.id)),
method: :post
}
end
link :commit do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}",
href: api_v3_paths.work_package(represented.id),
method: :patch
} if @current_user.allowed_to?(:edit_work_packages, represented.project) &&
# Calling valid? on represented empties the list of errors

@ -37,14 +37,14 @@ module API
class WorkPackageAttributeLinksRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include API::Utilities::UrlHelper
include API::V3::Utilities::PathHelper
self.as_strategy = ::API::Utilities::CamelCasingStrategy.new
property :status,
exec_context: :decorator,
getter: -> (*) {
{ href: "#{root_path}api/v3/statuses/#{represented.status.id}" }
{ href: api_v3_paths.status(represented.status.id) }
},
setter: -> (value, *) {
resource = parse_resource(:status, :statuses, value['href'])
@ -57,7 +57,7 @@ module API
getter: -> (*) {
id = represented.assigned_to_id
{ href: id ? "#{root_path}api/v3/users/#{id}" : nil }
{ href: id ? api_v3_paths.user(id) : nil }
},
setter: -> (value, *) {
user_id = parse_user_resource(:assignee, value['href'])
@ -70,7 +70,7 @@ module API
getter: -> (*) {
id = represented.responsible_id
{ href: id ? "#{root_path}api/v3/users/#{id}" : nil }
{ href: id ? api_v3_paths.user(id) : nil }
},
setter: -> (value, *) {
user_id = parse_user_resource(:responsible, value['href'])

@ -37,7 +37,7 @@ module API
class WorkPackageSchemaRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include API::Utilities::UrlHelper
include API::V3::Utilities::PathHelper
self.as_strategy = ::API::Utilities::CamelCasingStrategy.new
@ -66,7 +66,7 @@ module API
property :links_to_allowed_statuses,
as: :_links,
getter: -> (*) { self } do
include API::Utilities::UrlHelper
include API::V3::Utilities::PathHelper
self.as_strategy = ::API::Utilities::CamelCasingStrategy.new
@ -74,7 +74,7 @@ module API
def allowed_values
represented.map do |status|
{ href: "#{root_path}api/v3/statuses/#{status.id}", title: status.name }
{ href: api_v3_paths.status(status.id), title: status.name }
end
end
end
@ -96,16 +96,13 @@ module API
property :links_to_available_assignees,
as: :_links,
getter: -> (*) { self } do
include API::Utilities::UrlHelper
include API::V3::Utilities::PathHelper
self.as_strategy = ::API::Utilities::CamelCasingStrategy.new
property :allowed_values,
getter: -> (*) {
project_id = represented.project.id
href = "#{root_path}api/v3/projects/#{project_id}/available_assignees"
{ href: href }
{ href: api_v3_paths.available_assignees(represented.project.id) }
},
exec_context: :decorator
end
@ -121,16 +118,13 @@ module API
property :links_to_available_responsibles,
as: :_links,
getter: -> (*) { self } do
include API::Utilities::UrlHelper
include API::V3::Utilities::PathHelper
self.as_strategy = ::API::Utilities::CamelCasingStrategy.new
property :allowed_values,
getter: -> (*) {
project_id = represented.project.id
href = "#{root_path}api/v3/projects/#{project_id}/available_responsibles"
{ href: href }
{ href: api_v3_paths.available_responsibles(represented.project.id) }
},
exec_context: :decorator
end

@ -36,7 +36,7 @@ module API
class RelationRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include OpenProject::StaticRouting::UrlHelpers
include API::V3::Utilities::PathHelper
self.as_strategy = API::Utilities::CamelCasingStrategy.new
@ -51,20 +51,20 @@ module API
property :_type, exec_context: :decorator
link :self do
{ href: "#{root_path}api/v3/relations/#{represented.id}" }
{ href: api_v3_paths.relation(represented.id) }
end
link :relatedFrom do
{ href: "#{root_path}api/v3/work_packages/#{represented.from_id}" }
{ href: api_v3_paths.work_package(represented.from_id) }
end
link :relatedTo do
{ href: "#{root_path}api/v3/work_packages/#{represented.to_id}" }
{ href: api_v3_paths.work_package(represented.to_id) }
end
link :remove do
{
href: "#{root_path}api/v3/work_packages/#{represented.from.id}/relations/#{represented.id}",
href: api_v3_paths.work_package_relation(represented.id, represented.from.id),
method: :delete,
title: 'Remove relation'
} if current_user_allowed_to(:manage_work_package_relations)

@ -36,7 +36,7 @@ module API
class WorkPackageRepresenter < Roar::Decorator
include Roar::JSON::HAL
include Roar::Hypermedia
include API::Utilities::UrlHelper
include API::V3::Utilities::PathHelper
include OpenProject::TextFormatting
self.as_strategy = ::API::Utilities::CamelCasingStrategy.new
@ -52,14 +52,14 @@ module API
link :self do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}",
href: api_v3_paths.work_package(represented.id),
title: "#{represented.subject}"
}
end
link :update do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}/form",
href: api_v3_paths.work_package_form(represented.id),
method: :post,
title: "Update #{represented.subject}"
} if current_user_allowed_to(:edit_work_packages)
@ -67,7 +67,7 @@ module API
link :updateImmediately do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}",
href: api_v3_paths.work_package(represented.id),
method: :patch,
title: "Update #{represented.subject}"
} if current_user_allowed_to(:edit_work_packages)
@ -107,21 +107,21 @@ module API
link :author do
{
href: "#{root_path}api/v3/users/#{represented.author.id}",
href: api_v3_paths.user(represented.author.id),
title: "#{represented.author.name} - #{represented.author.login}"
} unless represented.author.nil?
end
link :responsible do
{
href: "#{root_path}api/v3/users/#{represented.responsible.id}",
href: api_v3_paths.user(represented.responsible.id),
title: "#{represented.responsible.name} - #{represented.responsible.login}"
} unless represented.responsible.nil?
end
link :assignee do
{
href: "#{root_path}api/v3/users/#{represented.assigned_to.id}",
href: api_v3_paths.user(represented.assigned_to.id),
title: "#{represented.assigned_to.name} - #{represented.assigned_to.login}"
} unless represented.assigned_to.nil?
end
@ -136,14 +136,14 @@ module API
link :availableWatchers do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}/available_watchers",
href: api_v3_paths.available_watchers(represented.id),
title: 'Available Watchers'
}
end
link :watchChanges do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}/watchers",
href: api_v3_paths.work_package_watchers(represented.id),
method: :post,
data: { user_id: @current_user.id },
title: 'Watch work package'
@ -154,7 +154,7 @@ module API
link :unwatchChanges do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}/watchers/#{@current_user.id}",
href: "#{api_v3_paths.work_package_watchers(represented.id)}/#{@current_user.id}",
method: :delete,
title: 'Unwatch work package'
} if current_user_allowed_to(:view_work_packages) &&
@ -163,7 +163,7 @@ module API
link :addWatcher do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}/watchers{?user_id}",
href: "#{api_v3_paths.work_package_watchers(represented.id)}{?user_id}",
method: :post,
title: 'Add watcher',
templated: true
@ -172,7 +172,7 @@ module API
link :addRelation do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}/relations",
href: api_v3_paths.work_package_relations(represented.id),
method: :post,
title: 'Add relation'
} if current_user_allowed_to(:manage_work_package_relations)
@ -188,7 +188,7 @@ module API
link :changeParent do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}",
href: api_v3_paths.work_package(represented.id),
method: :patch,
title: "Change parent of #{represented.subject}"
} if current_user_allowed_to(:manage_subtasks)
@ -196,7 +196,7 @@ module API
link :addComment do
{
href: "#{root_path}api/v3/work_packages/#{represented.id}/activities",
href: api_v3_paths.work_package_activities(represented.id),
method: :post,
title: 'Add comment'
} if current_user_allowed_to(:add_work_package_notes)
@ -204,7 +204,7 @@ module API
link :parent do
{
href: "#{root_path}api/v3/work_packages/#{represented.parent.id}",
href: api_v3_paths.work_package(represented.parent.id),
title: represented.parent.subject
} unless represented.parent.nil? || !represented.parent.visible?
end
@ -219,7 +219,7 @@ module API
link :version do
{
href: version_path(represented.fixed_version),
href: api_v3_paths.versions(represented.fixed_version),
type: 'text/html',
title: "#{represented.fixed_version.to_s_for_project(represented.project)}"
} if represented.fixed_version && @current_user.allowed_to?({ controller: 'versions', action: 'show' }, represented.fixed_version.project, global: false)

Loading…
Cancel
Save