Created some decorators

pull/1435/head
Marek Takac 11 years ago
parent a5cb66e5b6
commit 5cd59c4ad6
  1. 28
      app/api/projects/project_representer.rb
  2. 30
      app/api/users/user_representer.rb
  3. 45
      app/api/work_packages/work_package_representer.rb
  4. 8
      app/api/work_packages/work_packages.rb

@ -0,0 +1,28 @@
require 'roar/decorator'
require 'roar/representer/json'
require 'roar/representer/json/hal'
module Projects
class ProjectRepresenter < Roar::Decorator
include Roar::Representer::JSON
include Roar::Representer::Feature::Hypermedia
include Roar::Representer::JSON::HAL
property :id
property :name
property :description
property :homepage
property :is_public
property :created_on
property :updated_on
property :identifier
property :status
property :summary
link :self do
"/project/#{represented.id}"
end
property :responsible, class: User, decorator: Users::UserRepresenter, embedded: true
end
end

@ -0,0 +1,30 @@
require 'roar/decorator'
require 'roar/representer/json'
require 'roar/representer/json/hal'
module Users
class UserRepresenter < Roar::Decorator
include Roar::Representer::JSON
include Roar::Representer::Feature::Hypermedia
include Roar::Representer::JSON::HAL
property :id
property :login
property :firstname
property :lastname
property :mail
property :admin
property :status
property :last_login_on
property :language
property :created_on
property :updated_on
property :type
property :identity_url
property :mail_notification
link :self do
"/users/#{represented.id}"
end
end
end

@ -24,30 +24,29 @@ module WorkPackages
property :created_at
property :updated_at
link :self do
"/work_packages/#{represented.id}"
property :_links, exec_context: :decorator
def _links
{
root: { href: "/", title: 'OpenProject API entry point' },
self: { href: "/work_packages/#{represented.id}", title: "represented.subject" },
work_packages: { href: "/work_packages", title: "Work packages" },
project_work_packages: { href: "/projects/#{represented.project.identifier}/work_packages", title: "#{represented.project.name} - Work packages" },
descendants: { href: "/projects/#{represented.project.identifier}/work_packages?filter[ancestor_id]=#{represented.id}", title: "#{represented.subject} - Descendant work packages" },
children: { href: "/projects/#{represented.project.identifier}/work_packages?filter[parent_id]=#{represented.id}", title: "#{represented.subject} - Children work packages" },
create: { href: "/projects/#{represented.project.identifier}/work_packages", method: :post, title: "#{represented.project.name} - Create new work package" },
update: { href: "/work_packages/#{represented.id}", method: :patch, title: "Update #{represented.subject}" },
delete: { href: "/work_packages/#{represented.id}", method: :delete, title: "Delete #{represented.subject}" },
project: { href: "/projects/#{represented.project.identifier}", title: "#{represented.project}" },
author: { href: "/users/#{represented.author_id}", title: "?" },
assignee: { href: "/users/#{represented.assigned_to_id}", title: "?" },
responsible: { href: "/users/#{represented.responsible_id}", title: "?" }
}
end
property :project, class: Project, embedded: true do
property :id
end
property :author, class: User, embedded: true do
property :mail
end
property :assigned_to, as: :assignee, class: User, embedded: true do
property :mail
end
property :responsible, class: User, embedded: true do
property :mail
end
# collection :children, class: WorkPackage, embedded: true do
# property :subject
# end
property :project, class: Project, decorator: Projects::ProjectRepresenter, embedded: true
property :author, class: User, decorator: Users::UserRepresenter, embedded: true
property :assigned_to, as: :assignee, class: User, decorator: Users::UserRepresenter, embedded: true
property :responsible, class: User, decorator: Users::UserRepresenter, embedded: true
end
end

@ -2,7 +2,15 @@ module WorkPackages
class API < Grape::API
resources :work_packages do
params do
optional :page, type: Integer, default: 1
optional :per_page, type: Integer, default: 100
optional :filters, type: Array
optional :sort_expression, type: String
optional :extend, type: String
end
get do
binding.pry
work_packages = WorkPackage.all
end

Loading…
Cancel
Save