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

Introduce registry to ensure we eager load all of them

Oliver Güntherd0ef27dcb620
diff --git a/app/controllers/enumerations_controller.rb b/app/controllers/enumerations_controller.rb
index 07841657eea..e9dfc358734 100644
--- a/app/controllers/enumerations_controller.rb
+++ b/app/controllers/enumerations_controller.rb
@@ -115,6 +115,6 @@ class EnumerationsController < ApplicationController
##
# Find an enumeration class with the given name
def enumeration_class(type)
- Enumeration.subclasses.detect { |cls| cls.name == type }
+ Enumeration.registered_subclasses.detect { |cls| cls.name == type }
end
end
diff --git a/app/models/concerns/subclass_registry.rb b/app/models/concerns/subclass_registry.rb
new file mode 100644
index 00000000000..37159680d38
--- /dev/null
+++ b/app/models/concerns/subclass_registry.rb
@@ -0,0 +1,45 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 SubclassRegistry
+ extend ActiveSupport::Concern
+
+ included do
+ class_attribute :registered_subclasses, default: []
+ end
+
+ class_methods do
+ def register_subclass(subclass)
+ raise ArgumentError, "#{subclass} needs to inherit from #{self}" unless subclass < self
+
+ unless registered_subclasses.include?(subclass)
+ registered_subclasses << subclass
+ end
+ end
+ end
+end
diff --git a/app/models/enumeration.rb b/app/models/enumeration.rb
index 2aba1f53f90..da80df87146 100644
--- a/app/models/enumeration.rb
+++ b/app/models/enumeration.rb
@@ -27,6 +27,8 @@
#++
class Enumeration < ApplicationRecord
+ include SubclassRegistry
+
default_scope { order("#{Enumeration.table_name}.position ASC") }
belongs_to :project, optional: true
diff --git a/config/initializers/sti_eager_load.rb b/config/initializers/sti_eager_load.rb
new file mode 100644
index 00000000000..686afe5db85
--- /dev/null
+++ b/config/initializers/sti_eager_load.rb
@@ -0,0 +1,31 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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.
+#++
+
+Rails.application.config.to_prepare do
+ Enumeration.register_subclass(IssuePriority)
+end
diff --git a/modules/costs/lib/costs/engine.rb b/modules/costs/lib/costs/engine.rb
index 47a1adb79e6..c495985b10e 100644
--- a/modules/costs/lib/costs/engine.rb
+++ b/modules/costs/lib/costs/engine.rb
@@ -282,8 +282,8 @@ module Costs
end
config.to_prepare do
+ Enumeration.register_subclass(TimeEntryActivity)
OpenProject::ProjectLatestActivity.register on: "TimeEntry"
-
Costs::Patches::MembersPatch.mixin!
##
diff --git a/modules/documents/lib/open_project/documents/engine.rb b/modules/documents/lib/open_project/documents/engine.rb
index 810328b43dd..c8d6e1e85d5 100644
--- a/modules/documents/lib/open_project/documents/engine.rb
+++ b/modules/documents/lib/open_project/documents/engine.rb
@@ -77,5 +77,9 @@ module OpenProject::Documents
# Add documents to allowed search params
additional_permitted_attributes search: %i(documents)
+
+ config.to_prepare do
+ Enumeration.register_subclass(DocumentCategory)
+ end
end
end