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

Create Projects::Types and Projects::Versions concerns.

Dombi Attila736cce6377ac
diff --git a/app/models/project.rb b/app/models/project.rb
index 2b00fb259fe..cd7c1a4cf46 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -34,7 +34,10 @@ class Project < ApplicationRecord
include Projects::CustomFields
include Projects::Hierarchy
include Projects::Storage
+ include Projects::Types
+ include Projects::Versions
include Projects::WorkPackageCustomFields
+
include ::Scopes::Scoped
@@ -202,63 +205,6 @@ class Project < ApplicationRecord
Project.visible.select { |p| User.current.member_of? p }.sort_by(&:to_s)
end
- def types_used_by_work_packages
- ::Type.where(id: WorkPackage.where(project_id: project.id)
- .select(:type_id)
- .distinct)
- end
-
- # Returns a scope of the types used by the project and its active sub projects
- def rolled_up_types
- ::Type
- .joins(:projects)
- .select("DISTINCT #{::Type.table_name}.*")
- .where(projects: { id: self_and_descendants.select(:id) })
- .merge(Project.active)
- .order("#{::Type.table_name}.position")
- end
-
- # Closes open and locked project versions that are completed
- def close_completed_versions
- Version.transaction do
- versions.where(status: %w(open locked)).find_each do |version|
- if version.completed?
- version.update_attribute(:status, "closed")
- end
- end
- end
- end
-
- # Returns a scope of the Versions on subprojects
- def rolled_up_versions
- Version.rolled_up(self)
- end
-
- # Returns a scope of the Versions used by the project
- def shared_versions
- Version.shared_with(self)
- end
-
- # Returns all versions a work package can be assigned to. Opposed to
- # #shared_versions this returns an array of Versions, not a scope.
- #
- # The main benefit is in scenarios where work packages' projects are eager
- # loaded. Because eager loading the project e.g. via
- # WorkPackage.includes(:project).where(type: 5) will assign the same instance
- # (same object_id) for every work package having the same project this will
- # reduce the number of db queries when performing operations including the
- # project's versions.
- #
- # For custom fields configured with "Allow non-open versions" this can be called
- # with only_open: false, in which case locked and closed versions are returned as well.
- def assignable_versions(only_open: true)
- if only_open
- @assignable_versions ||= shared_versions.references(:project).with_status_open.order_by_semver_name.to_a
- else
- @assignable_versions_including_non_open ||= shared_versions.references(:project).order_by_semver_name.to_a
- end
- end
-
def project
self
end
diff --git a/app/models/projects/types.rb b/app/models/projects/types.rb
new file mode 100644
index 00000000000..0f0f7e429f5
--- /dev/null
+++ b/app/models/projects/types.rb
@@ -0,0 +1,49 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 Projects::Types
+ extend ActiveSupport::Concern
+
+ included do
+ def types_used_by_work_packages
+ ::Type.where(id: WorkPackage.where(project_id: project.id)
+ .select(:type_id)
+ .distinct)
+ end
+
+ # Returns a scope of the types used by the project and its active sub projects
+ def rolled_up_types
+ ::Type
+ .joins(:projects)
+ .select("DISTINCT #{::Type.table_name}.*")
+ .where(projects: { id: self_and_descendants.select(:id) })
+ .merge(Project.active)
+ .order("#{::Type.table_name}.position")
+ end
+ end
+end
diff --git a/app/models/projects/versions.rb b/app/models/projects/versions.rb
new file mode 100644
index 00000000000..31be3b3594b
--- /dev/null
+++ b/app/models/projects/versions.rb
@@ -0,0 +1,76 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 Projects::Versions
+ extend ActiveSupport::Concern
+
+ included do
+ # Closes open and locked project versions that are completed
+ def close_completed_versions
+ Version.transaction do
+ versions.where(status: %w(open locked)).find_each do |version|
+ if version.completed?
+ version.update_attribute(:status, "closed")
+ end
+ end
+ end
+ end
+
+ # Returns a scope of the Versions on subprojects
+ def rolled_up_versions
+ Version.rolled_up(self)
+ end
+
+ # Returns a scope of the Versions used by the project
+ def shared_versions
+ Version.shared_with(self)
+ end
+
+ # Returns all versions a work package can be assigned to. Opposed to
+ # #shared_versions this returns an array of Versions, not a scope.
+ #
+ # The main benefit is in scenarios where work packages' projects are eager
+ # loaded. Because eager loading the project e.g. via
+ # WorkPackage.includes(:project).where(type: 5) will assign the same instance
+ # (same object_id) for every work package having the same project this will
+ # reduce the number of db queries when performing operations including the
+ # project's versions.
+ #
+ # For custom fields configured with "Allow non-open versions" this can be called
+ # with only_open: false, in which case locked and closed versions are returned as well.
+ def assignable_versions(only_open: true)
+ if only_open
+ @assignable_versions ||=
+ shared_versions.references(:project).with_status_open.order_by_semver_name.to_a
+ else
+ @assignable_versions_including_non_open ||= # rubocop:disable Naming/MemoizedInstanceVariableName
+ shared_versions.references(:project).order_by_semver_name.to_a
+ end
+ end
+ end
+end