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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
reduce count calls in collection representer
ulferts71e6f28df5ce
diff --git a/lib/api/decorators/offset_paginated_collection.rb b/lib/api/decorators/offset_paginated_collection.rbindex ee0f5ce812c..7a76779192d 100644--- a/lib/api/decorators/offset_paginated_collection.rb+++ b/lib/api/decorators/offset_paginated_collection.rb@@ -107,9 +107,20 @@ module APIenddef paged_models(models)- # FIXME: calling :to_a is a hack to circumvent a counting error in will_paginate- # see https://github.com/mislav/will_paginate/issues/449- models.page(@page).per_page(@per_page).to_a+ if @per_page == 0+ # Optimization. If we are not interested in the model, we can+ # save the round trip to the database.+ models.none+ else+ # Using WillPaginate as we have used it before but avoid the builtin+ # page(@page).per_page(@per_page) way of fetching+ # as it will, on top of fetching the values, also do a count of all elements matching the query+ # which we do not need at this place.+ page_number = ::WillPaginate::PageNumber(@page.nil? ? 1 : @page)+ models+ .offset(page_number.to_offset(@per_page).to_i)+ .limit(@per_page)+ endendendenddiff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rbindex ab04dfb81f6..3f681092f44 100644--- a/lib/api/v3/work_packages/work_package_collection_representer.rb+++ b/lib/api/v3/work_packages/work_package_collection_representer.rb@@ -191,12 +191,11 @@ module APIdef all_cfs_of_project@all_cfs_of_project ||= represented.group_by(&:project_id)- .map { |id, wps| [id, wps.map(&:available_custom_fields).flatten.uniq] }- .to_h+ .transform_values { |wps| wps.map(&:available_custom_fields).flatten.uniq }enddef paged_models(models)- models.page(@page).per_page(@per_page).pluck(:id)+ super.pluck(:id)enddef _typediff --git a/spec/lib/api/v3/groups/group_collection_representer_spec.rb b/spec/lib/api/v3/groups/group_collection_representer_spec.rbindex f044e7ac9a5..c54ff482f96 100644--- a/spec/lib/api/v3/groups/group_collection_representer_spec.rb+++ b/spec/lib/api/v3/groups/group_collection_representer_spec.rb@@ -33,13 +33,13 @@ describe ::API::V3::Groups::GroupCollectionRepresenter dolet(:groups) dobuild_stubbed_list(:group, 3).tap do |groups|allow(groups)- .to receive(:per_page)- .with(page_size)+ .to receive(:offset)+ .with(page - 1).and_return(groups)allow(groups)- .to receive(:page)- .with(page)+ .to receive(:limit)+ .with(page_size).and_return(groups)allow(groups)diff --git a/spec/lib/api/v3/memberships/membership_collection_representer_spec.rb b/spec/lib/api/v3/memberships/membership_collection_representer_spec.rbindex f42fc426a08..ad61f6cb202 100644--- a/spec/lib/api/v3/memberships/membership_collection_representer_spec.rb+++ b/spec/lib/api/v3/memberships/membership_collection_representer_spec.rb@@ -33,13 +33,13 @@ describe ::API::V3::Memberships::MembershipCollectionRepresenter dolet(:members) dobuild_stubbed_list(:member, 3).tap do |members|allow(members)- .to receive(:per_page)+ .to receive(:limit).with(page_size).and_return(members)allow(members)- .to receive(:page)- .with(page)+ .to receive(:offset)+ .with(page - 1).and_return(members)allow(members)diff --git a/spec/lib/api/v3/notifications/notification_collection_representer_spec.rb b/spec/lib/api/v3/notifications/notification_collection_representer_spec.rbindex aa0530c2926..c00bd0cef78 100644--- a/spec/lib/api/v3/notifications/notification_collection_representer_spec.rb+++ b/spec/lib/api/v3/notifications/notification_collection_representer_spec.rb@@ -35,13 +35,13 @@ describe ::API::V3::Notifications::NotificationCollectionRepresenter dobuild_stubbed_list(:notification,3).tap do |items|allow(items)- .to receive(:per_page)+ .to receive(:limit).with(page_size).and_return(items)allow(items)- .to receive(:page)- .with(page)+ .to receive(:offset)+ .with(page - 1).and_return(items)allow(items)diff --git a/spec/lib/api/v3/placeholder_users/placeholder_user_collection_representer_spec.rb b/spec/lib/api/v3/placeholder_users/placeholder_user_collection_representer_spec.rbindex 25df4ab415d..9306a30e262 100644--- a/spec/lib/api/v3/placeholder_users/placeholder_user_collection_representer_spec.rb+++ b/spec/lib/api/v3/placeholder_users/placeholder_user_collection_representer_spec.rb@@ -41,13 +41,13 @@ describe ::API::V3::PlaceholderUsers::PlaceholderUserCollectionRepresenter doplaceholders = build_stubbed_list(:placeholder_user,actual_count)allow(placeholders)- .to receive(:per_page)+ .to receive(:limit).with(page_size).and_return(placeholders)allow(placeholders)- .to receive(:page)- .with(page)+ .to receive(:offset)+ .with(page - 1).and_return(placeholders)allow(placeholders)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.rbindex a9004716421..2e1bdb0fa4a 100644--- a/spec/lib/api/v3/relations/relation_paginated_collection_representer_spec.rb+++ b/spec/lib/api/v3/relations/relation_paginated_collection_representer_spec.rb@@ -36,13 +36,13 @@ describe ::API::V3::Relations::RelationPaginatedCollectionRepresenter dolet(:relations) dobuild_stubbed_list(:relation, total).tap do |relations|allow(relations)- .to receive(:per_page)+ .to receive(:limit).with(page_size).and_return(relations)allow(relations)- .to receive(:page)- .with(page)+ .to receive(:offset)+ .with(page - 1).and_return(relations)allow(relations)diff --git a/spec/lib/api/v3/users/user_collection_representer_spec.rb b/spec/lib/api/v3/users/user_collection_representer_spec.rbindex 6a4538e0a23..8658beff4b9 100644--- a/spec/lib/api/v3/users/user_collection_representer_spec.rb+++ b/spec/lib/api/v3/users/user_collection_representer_spec.rb@@ -40,13 +40,13 @@ describe ::API::V3::Users::UserCollectionRepresenter dousers = build_stubbed_list(:user,actual_count)allow(users)- .to receive(:per_page)+ .to receive(:limit).with(page_size).and_return(users)allow(users)- .to receive(:page)- .with(page)+ .to receive(:offset)+ .with(page - 1).and_return(users)allow(users)