Add Category List API resource

Signed-off-by: Alex Coles <alex@alexbcoles.com>
pull/1637/head
Alex Coles 10 years ago
parent bc6074c0f7
commit 91ce7e955f
  1. 49
      lib/api/v3/categories/categories_api.rb
  2. 64
      lib/api/v3/categories/category_collection_representer.rb
  3. 43
      lib/api/v3/categories/category_model.rb
  4. 54
      lib/api/v3/categories/category_representer.rb
  5. 1
      lib/api/v3/projects/projects_api.rb
  6. 64
      spec/lib/api/v3/categories/category_collection_representer_spec.rb
  7. 37
      spec/lib/api/v3/categories/category_model_spec.rb
  8. 51
      spec/lib/api/v3/categories/category_representer_spec.rb
  9. 68
      spec/requests/api/v3/category_resource_spec.rb

@ -0,0 +1,49 @@
#-- encoding: UTF-8
#-- 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 API
module V3
module Categories
class CategoriesAPI < Grape::API
resources :categories do
before do
@categories = @project.categories
@categories = @categories.map { |category| CategoryModel.new(category) }
end
get do
CategoryCollectionRepresenter.new(@categories, project: @project)
end
end
end
end
end
end

@ -0,0 +1,64 @@
#-- encoding: UTF-8
#-- 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.
#++
require 'roar/decorator'
require 'representable/json/collection'
require 'roar/representer/json/hal'
module API
module V3
module Categories
class CategoryCollectionRepresenter < Roar::Decorator
include Roar::Representer::JSON::HAL
include OpenProject::StaticRouting::UrlHelpers
self.as_strategy = API::Utilities::CamelCasingStrategy.new
attr_reader :project
def initialize(model, options = {})
@project = options.fetch(:project)
super(model)
end
link :self do
"#{root_path}api/v3/projects/#{project.id}/categories"
end
property :_type, exec_context: :decorator
collection :categories, embedded: true, extend: CategoryRepresenter, getter: ->(_) { self }
def _type
'Categories'
end
end
end
end
end

@ -0,0 +1,43 @@
#-- encoding: UTF-8
#-- 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.
#++
require 'reform'
require 'reform/form/coercion'
module API
module V3
module Categories
class CategoryModel < Reform::Form
include Coercion
property :name, type: String
end
end
end
end

@ -0,0 +1,54 @@
#-- encoding: UTF-8
#-- 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.
#++
require 'roar/decorator'
require 'roar/representer/json/hal'
module API
module V3
module Categories
class CategoryRepresenter < Roar::Decorator
include Roar::Representer::JSON::HAL
include Roar::Representer::Feature::Hypermedia
include OpenProject::StaticRouting::UrlHelpers
self.as_strategy = API::Utilities::CamelCasingStrategy.new
property :_type, exec_context: :decorator
property :id, getter: -> (*) { model.id }, render_nil: true
property :name, render_nil: true
def _type
'Category'
end
end
end
end
end

@ -47,6 +47,7 @@ module API
ProjectRepresenter.new(@model)
end
mount API::V3::Categories::CategoriesAPI
mount API::V3::Versions::VersionsAPI
end

@ -0,0 +1,64 @@
#-- 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.
#++
require 'spec_helper'
describe ::API::V3::Categories::CategoryCollectionRepresenter do
let(:project) { FactoryGirl.build(:project, id: 888) }
let(:categories) { FactoryGirl.build_list(:category, 3) }
let(:models) { categories.map { |category|
::API::V3::Categories::CategoryModel.new(category)
} }
let(:representer) { described_class.new(models, project: project) }
describe '#initialize' do
context 'with incorrect parameters' do
it 'should raise without a project' do
expect { described_class.new(models) }.to raise_error
end
end
end
context 'generation' do
subject(:generated) { representer.to_json }
it { should include_json('Categories'.to_json).at_path('_type') }
it { should have_json_type(Object).at_path('_links') }
it 'should link to self' do
expect(generated).to have_json_path('_links/self/href')
expect(parse_json(generated, '_links/self/href')).to match %r{/api/v3/projects/888/categories$}
end
describe 'categories' do
it { should have_json_path('_embedded/categories') }
it { should have_json_size(3).at_path('_embedded/categories') }
it { should have_json_path('_embedded/categories/2/name') }
end
end
end

@ -0,0 +1,37 @@
#-- 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.
#++
require 'spec_helper'
describe ::API::V3::Categories::CategoryModel do
subject(:model) { ::API::V3::Categories::CategoryModel.new(category) }
let(:category) { FactoryGirl.build(:category, attributes) }
let(:attributes) { { name: 'Specific Category' } }
its(:name) { should eq 'Specific Category' }
end

@ -0,0 +1,51 @@
#-- 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.
#++
require 'spec_helper'
describe ::API::V3::Categories::CategoryRepresenter do
let(:category) { FactoryGirl.build(:category) }
let(:model) { ::API::V3::Categories::CategoryModel.new(category) }
let(:representer) { described_class.new(model) }
context 'generation' do
subject(:generated) { representer.to_json }
it { should include_json('Category'.to_json).at_path('_type') }
xit { should have_json_type(Object).at_path('_links') }
xit 'should link to self' do
expect(subject).to have_json_path('_links/self/href')
end
describe 'category' do
it { should have_json_path('id') }
it { should have_json_path('name') }
end
end
end

@ -0,0 +1,68 @@
#-- 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.
#++
require 'spec_helper'
require 'rack/test'
describe 'API v3 Category resource' do
include Rack::Test::Methods
let(:current_user) { FactoryGirl.create(:user) }
let(:role) { FactoryGirl.create(:role, permissions: []) }
let(:project) { FactoryGirl.create(:project, is_public: false) }
let(:categories) { FactoryGirl.create_list(:category, 3, project: project) }
let(:other_categories) { FactoryGirl.create_list(:category, 2) }
describe '#get' do
subject(:response) { last_response }
context 'logged in user' do
let(:get_path) { "/api/v3/projects/#{project.id}/categories" }
before do
allow(User).to receive(:current).and_return current_user
member = FactoryGirl.build(:member, user: current_user, project: project)
member.role_ids = [role.id]
member.save!
categories
other_categories
get get_path
end
it 'should respond with 200' do
expect(subject.status).to eq(200)
end
it 'should respond with categories' do
expect(subject.body).to include_json('Categories'.to_json).at_path('_type')
expect(subject.body).to have_json_size(3).at_path('_embedded/categories')
end
end
end
end
Loading…
Cancel
Save