Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

paginate relations collection

ulfertsfe06d885dbc9
diff --git a/lib/api/v3/relations/relation_collection_representer.rb b/lib/api/v3/relations/relation_collection_representer.rb
index d101a6f38ef..1b63b0f1134 100644
--- a/lib/api/v3/relations/relation_collection_representer.rb
+++ b/lib/api/v3/relations/relation_collection_representer.rb
@@ -31,6 +31,8 @@
module API
module V3
module Relations
+ # TODO: Usage of this is to be replaced by its paginated equivalent once the front end
+ # handles paginated responses.
class RelationCollectionRepresenter < ::API::Decorators::UnpaginatedCollection
self.to_eager_load = ::API::V3::Relations::RelationRepresenter.to_eager_load
end
diff --git a/lib/api/v3/relations/relation_paginated_collection_representer.rb b/lib/api/v3/relations/relation_paginated_collection_representer.rb
new file mode 100644
index 00000000000..e4d21f47029
--- /dev/null
+++ b/lib/api/v3/relations/relation_paginated_collection_representer.rb
@@ -0,0 +1,41 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module API
+ module V3
+ module Relations
+ class RelationPaginatedCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollection
+ self.to_eager_load = ::API::V3::Relations::RelationRepresenter.to_eager_load
+
+ element_decorator RelationRepresenter
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/relations/relations_api.rb b/lib/api/v3/relations/relations_api.rb
index f2017bf88b4..583e6649b3a 100644
--- a/lib/api/v3/relations/relations_api.rb
+++ b/lib/api/v3/relations/relations_api.rb
@@ -26,26 +26,19 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-require 'api/v3/relations/relation_representer'
-require 'api/v3/relations/relation_collection_representer'
-
-require 'relations/create_service'
-require 'relations/update_service'
-
module API
module V3
module Relations
class RelationsAPI < ::API::OpenProjectAPI
resources :relations do
- get do
- scope = Relation
- .non_hierarchy
- .includes(::API::V3::Relations::RelationRepresenter.to_eager_load)
-
- ::API::V3::Utilities::ParamsToQuery.collection_response(scope,
- current_user,
- params)
- end
+ get &::API::V3::Utilities::Endpoints::Index.new(model: Relation,
+ scope: -> {
+ Relation
+ .non_hierarchy
+ .includes(::API::V3::Relations::RelationRepresenter.to_eager_load)
+ },
+ render_representer: RelationPaginatedCollectionRepresenter)
+ .mount
route_param :id, type: Integer, desc: 'Relation ID' do
after_validation do
diff --git a/spec/lib/api/v3/relations/relation_collection_representer_spec.rb b/spec/lib/api/v3/relations/relation_collection_representer_spec.rb
index bbbfabbf09b..b7ff9d774f7 100644
--- a/spec/lib/api/v3/relations/relation_collection_representer_spec.rb
+++ b/spec/lib/api/v3/relations/relation_collection_representer_spec.rb
@@ -34,11 +34,10 @@ describe ::API::V3::Relations::RelationCollectionRepresenter do
end
let(:relations) do
- (1..3).map do
- FactoryBot.build_stubbed(:relation,
- from: work_package,
- to: FactoryBot.build_stubbed(:work_package))
- end
+ FactoryBot.build_stubbed_list(:relation,
+ 3,
+ from: work_package,
+ to: FactoryBot.build_stubbed(:work_package))
end
let(:user) do
diff --git a/spec/lib/api/v3/relations/relation_paginated_collection_representer_spec.rb b/spec/lib/api/v3/relations/relation_paginated_collection_representer_spec.rb
new file mode 100644
index 00000000000..69383447ae4
--- /dev/null
+++ b/spec/lib/api/v3/relations/relation_paginated_collection_representer_spec.rb
@@ -0,0 +1,75 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe ::API::V3::Relations::RelationPaginatedCollectionRepresenter do
+ let(:work_package) do
+ FactoryBot.build_stubbed(:work_package)
+ end
+
+ let(:relations) do
+ FactoryBot.build_stubbed_list(:relation, total).tap do |relations|
+ allow(relations)
+ .to receive(:per_page)
+ .with(page_size)
+ .and_return(relations)
+
+ allow(relations)
+ .to receive(:page)
+ .with(page)
+ .and_return(relations)
+
+ allow(relations)
+ .to receive(:count)
+ .and_return(relations.length)
+ end
+ end
+
+ let(:user) do
+ FactoryBot.build_stubbed(:user)
+ end
+
+ let(:representer) do
+ described_class.new(relations,
+ self_link: self_base_link,
+ page: page,
+ per_page: page_size,
+ current_user: user)
+ end
+ let(:total) { 3 }
+ let(:page) { 1 }
+ let(:page_size) { 20 }
+ let(:actual_count) { total }
+ let(:self_base_link) { 'api/v3/relations' }
+ let(:collection_inner_type) { 'Relation' }
+
+ subject(:collection) { representer.to_json }
+
+ it_behaves_like 'offset-paginated APIv3 collection', 3, 'relations', 'Relation'
+end
diff --git a/spec/requests/api/v3/relations_resource_spec.rb b/spec/requests/api/v3/relations_resource_spec.rb
index f81c5801bae..b28e6d4256a 100644
--- a/spec/requests/api/v3/relations_resource_spec.rb
+++ b/spec/requests/api/v3/relations_resource_spec.rb
@@ -86,7 +86,9 @@ describe 'API v3 Relation resource', type: :request do
get path
end
- it_behaves_like 'API V3 collection response', 1, 1, 'Relation'
+ it_behaves_like 'API V3 collection response', 1, 1, 'Relation' do
+ let(:elements) { [visible_relation] }
+ end
end
context 'when not having view_work_packages' do