From fc3d1b5e360e9f73426dedb1ddcab1fe1d9b29cb Mon Sep 17 00:00:00 2001 From: Markus Kahl Date: Thu, 8 Sep 2016 18:11:13 +0100 Subject: [PATCH] minor refactoring to allow hooking into logic Signed-off-by: Markus Kahl --- .../work_package_collection_representer.rb | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rb index bea6d36b73..67d7d151e0 100644 --- a/lib/api/v3/work_packages/work_package_collection_representer.rb +++ b/lib/api/v3/work_packages/work_package_collection_representer.rb @@ -81,7 +81,7 @@ module API collection :elements, getter: -> (*) { - work_packages = eager_loaded_work_packages + work_packages = sorted_and_eager_loaded_work_packages generated_classes = ::Hash.new do |hash, work_package| hit = hash.values.find { |klass| @@ -114,19 +114,21 @@ module API # Eager load elements used in the representer later # to avoid n+1 queries triggered from each representer. - def eager_loaded_work_packages + def sorted_and_eager_loaded_work_packages ids_in_order = represented.map(&:id) - - work_packages = WorkPackage - .include_spent_hours(current_user) - .preload(element_decorator.to_eager_load) - .where(id: ids_in_order) - .select('work_packages.*') - .to_a + work_packages = eager_loaded_work_packages(ids_in_order) work_packages.sort_by { |wp| ids_in_order.index(wp.id) } end + def eager_loaded_work_packages(ids) + WorkPackage + .include_spent_hours(current_user) + .preload(element_decorator.to_eager_load) + .where(id: ids) + .select('work_packages.*') + end + private def current_user_allowed_to_add_work_packages?