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

extract sharable code into module and superclass

ulferts0bc1fb094f55
diff --git a/lib/api/v3/projects/project_eager_loading_wrapper.rb b/lib/api/v3/projects/project_eager_loading_wrapper.rb
index d99a73493ba..05ed487e25a 100644
--- a/lib/api/v3/projects/project_eager_loading_wrapper.rb
+++ b/lib/api/v3/projects/project_eager_loading_wrapper.rb
@@ -31,24 +31,8 @@
module API
module V3
module Projects
- class ProjectEagerLoadingWrapper < SimpleDelegator
- private_class_method :new
-
- # Because of the ruby method lookup,
- # wrapping the work_package here and define the
- # available_custom_fields methods on the wrapper does not suffice.
- # We thus extend each work package.
- def initialize(project)
- super
- project.extend(CustomFieldAccessorPatch)
- end
-
- ##
- # Workaround against warnings in flatten
- # delegator does not forward private method #to_ary
- def to_ary
- __getobj__.send(:to_ary)
- end
+ class ProjectEagerLoadingWrapper < API::V3::Utilities::EagerLoading::EagerLoadingWrapper
+ include API::V3::Utilities::EagerLoading::CustomFieldAccessor
class << self
def wrap(projects)
@@ -56,21 +40,10 @@ module API
projects.first.available_custom_fields
end
- projects
- .map { |project| new(project) }
+ super
.each { |project| project.available_custom_fields = custom_fields }
end
end
-
- module CustomFieldAccessorPatch
- def available_custom_fields
- @available_custom_fields
- end
-
- def available_custom_fields=(fields)
- @available_custom_fields = fields
- end
- end
end
end
end
diff --git a/lib/api/v3/utilities/eager_loading/custom_field_accessor.rb b/lib/api/v3/utilities/eager_loading/custom_field_accessor.rb
new file mode 100644
index 00000000000..b8f82af20d9
--- /dev/null
+++ b/lib/api/v3/utilities/eager_loading/custom_field_accessor.rb
@@ -0,0 +1,62 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module V3
+ module Utilities
+ module EagerLoading
+ module CustomFieldAccessor
+ extend ActiveSupport::Concern
+
+ included do
+ # Because of the ruby method lookup,
+ # wrapping the work_package here and define the
+ # available_custom_fields methods on the wrapper does not suffice.
+ # We thus extend each work package.
+ def initialize(object)
+ super
+ object.extend(CustomFieldAccessorPatch)
+ end
+
+ module CustomFieldAccessorPatch
+ def available_custom_fields
+ @available_custom_fields
+ end
+
+ def available_custom_fields=(fields)
+ @available_custom_fields = fields
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/utilities/eager_loading/eager_loading_wrapper.rb b/lib/api/v3/utilities/eager_loading/eager_loading_wrapper.rb
new file mode 100644
index 00000000000..1ee18e908db
--- /dev/null
+++ b/lib/api/v3/utilities/eager_loading/eager_loading_wrapper.rb
@@ -0,0 +1,55 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module V3
+ module Utilities
+ module EagerLoading
+ class EagerLoadingWrapper < SimpleDelegator
+ private_class_method :new
+
+ ##
+ # Workaround against warnings in flatten
+ # delegator does not forward private method #to_ary
+ def to_ary
+ __getobj__.send(:to_ary)
+ end
+
+ class << self
+ def wrap(objects)
+ objects
+ .map { |object| new(object) }
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/work_packages/eager_loading/custom_value.rb b/lib/api/v3/work_packages/eager_loading/custom_value.rb
index a011eb169f8..42ad72e5777 100644
--- a/lib/api/v3/work_packages/eager_loading/custom_value.rb
+++ b/lib/api/v3/work_packages/eager_loading/custom_value.rb
@@ -40,7 +40,7 @@ module API
end
def self.module
- CustomFieldAccessor
+ ::API::V3::Utilities::EagerLoading::CustomFieldAccessor
end
private
@@ -198,31 +198,6 @@ module API
end
end
end
-
- module CustomFieldAccessor
- extend ActiveSupport::Concern
-
- # Because of the ruby method lookup,
- # wrapping the work_package here and define the
- # available_custom_fields methods on the wrapper does not suffice.
- # We thus extend each work package.
- included do
- def initialize(work_package)
- super
- work_package.extend(CustomFieldAccessorPatch)
- end
- end
- end
-
- module CustomFieldAccessorPatch
- def available_custom_fields
- @available_custom_fields
- end
-
- def available_custom_fields=(fields)
- @available_custom_fields = fields
- end
- end
end
end
end
diff --git a/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb b/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb
index 3cbdd16cbce..5e9d13b26ad 100644
--- a/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb
+++ b/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb
@@ -31,20 +31,11 @@
module API
module V3
module WorkPackages
- class WorkPackageEagerLoadingWrapper < SimpleDelegator
- private_class_method :new
-
+ class WorkPackageEagerLoadingWrapper < API::V3::Utilities::EagerLoading::EagerLoadingWrapper
def wrapped?
true
end
- ##
- # Workaround against warnings in flatten
- # delegator does not forward private method #to_ary
- def to_ary
- __getobj__.send(:to_ary)
- end
-
class << self
def wrap(ids_in_order, current_user)
work_packages = add_eager_loading(WorkPackage.where(id: ids_in_order), current_user).to_a