Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

150 lines
5.1 KiB
Ruby
Raw Permalink Normal View History

2025-07-18 17:35:57 +01:00
# frozen_string_literal: true
#-- copyright
2020-01-15 11:31:26 +01:00
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
2011-05-30 20:52:25 +02:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
2011-05-30 20:52:25 +02:00
#
2013-09-16 17:59:31 +02:00
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
2021-01-13 17:47:45 +01:00
# Copyright (C) 2006-2013 Jean-Philippe Lang
2013-09-16 17:59:31 +02:00
# 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.
#++
class Workflow < ApplicationRecord
belongs_to :role
2014-11-03 21:10:39 +01:00
belongs_to :old_status, class_name: "Status"
belongs_to :new_status, class_name: "Status"
2017-10-18 10:39:08 +02:00
belongs_to :type, inverse_of: "workflows"
validates :role, :old_status, :new_status, presence: true
2011-05-30 20:52:25 +02:00
2013-07-18 16:17:02 +02:00
# Returns workflow transitions count by type and role
def self.count_by_type_and_role
2017-07-20 16:49:00 +02:00
counts = connection
.select_all("SELECT role_id, type_id, count(id) AS c FROM #{Workflow.table_name} GROUP BY role_id, type_id")
2018-11-20 16:23:33 +01:00
roles = Role.order(Arel.sql("builtin, position"))
types = ::Type.order(Arel.sql("position"))
2011-05-30 20:52:25 +02:00
2008-09-28 12:03:17 +00:00
result = []
2013-07-18 16:17:02 +02:00
types.each do |type|
2008-09-28 12:03:17 +00:00
t = []
roles.each do |role|
2014-11-03 21:35:22 +01:00
row = counts.detect { |c| c["role_id"].to_s == role.id.to_s && c["type_id"].to_s == type.id.to_s }
2008-09-28 12:03:17 +00:00
t << [role, (row.nil? ? 0 : row["c"].to_i)]
end
2013-07-18 16:17:02 +02:00
result << [type, t]
2008-09-28 12:03:17 +00:00
end
2011-05-30 20:52:25 +02:00
2008-09-28 12:03:17 +00:00
result
end
2009-12-04 22:46:12 +00:00
# Gets all work flows originating from the provided status
# that:
# * are defined for the type
# * are defined for any of the roles
#
# Workflows specific to author or assignee are ignored unless author and/or assignee are set to true. In
# such a case, those work flows are additionally returned.
def self.from_status(old_status_id, type_id, role_ids, author = false, assignee = false)
workflows = Workflow
.where(old_status_id:, type_id:, role_id: role_ids)
if author && assignee
workflows
elsif author || assignee
workflows
.merge(Workflow.where(author:).or(Workflow.where(assignee:)))
else
workflows
.where(author:)
.where(assignee:)
end
end
2009-12-04 22:46:12 +00:00
# Find potential statuses the user could be allowed to switch issues to
2014-11-03 21:35:22 +01:00
def self.available_statuses(project, user = User.current)
2017-07-20 16:49:00 +02:00
Workflow
.includes(:new_status)
2015-06-27 19:05:19 +02:00
.where(role_id: user.roles_for_project(project).map(&:id))
2023-09-06 11:06:36 +02:00
.filter_map(&:new_status)
2014-11-03 21:35:22 +01:00
.uniq
.sort
2009-12-04 22:46:12 +00:00
end
2011-05-30 20:52:25 +02:00
2009-12-12 10:06:07 +00:00
# Copies workflows from source to targets
2013-07-18 16:17:02 +02:00
def self.copy(source_type, source_role, target_types, target_roles)
2014-11-05 00:32:23 +01:00
unless source_type.is_a?(::Type) || source_role.is_a?(Role)
2014-11-03 21:35:22 +01:00
raise ArgumentError.new("source_type or source_role must be specified")
2009-12-12 10:06:07 +00:00
end
2011-05-30 20:52:25 +02:00
2013-07-18 16:17:02 +02:00
target_types = Array(target_types)
2014-10-30 11:26:31 +01:00
target_types = ::Type.all if target_types.empty?
2013-01-14 16:51:31 +01:00
target_roles = Array(target_roles)
2009-12-12 10:06:07 +00:00
target_roles = Role.all if target_roles.empty?
2011-05-30 20:52:25 +02:00
2013-07-18 16:17:02 +02:00
target_types.each do |target_type|
2009-12-12 10:06:07 +00:00
target_roles.each do |target_role|
2013-07-18 16:17:02 +02:00
copy_one(source_type || target_type,
2014-11-03 21:35:22 +01:00
source_role || target_role,
target_type,
target_role)
2009-12-12 10:06:07 +00:00
end
end
end
2011-05-30 20:52:25 +02:00
2009-12-12 10:06:07 +00:00
# Copies a single set of workflows from source to target
2013-07-18 16:17:02 +02:00
def self.copy_one(source_type, source_role, target_type, target_role)
2014-11-05 00:32:23 +01:00
unless source_type.is_a?(::Type) && !source_type.new_record? &&
2014-11-03 21:35:22 +01:00
source_role.is_a?(Role) && !source_role.new_record? &&
2014-11-05 00:32:23 +01:00
target_type.is_a?(::Type) && !target_type.new_record? &&
2014-11-03 21:35:22 +01:00
target_role.is_a?(Role) && !target_role.new_record?
2011-05-30 20:52:25 +02:00
2014-11-03 21:35:22 +01:00
raise ArgumentError.new("arguments can not be nil or unsaved objects")
2009-12-12 10:06:07 +00:00
end
2011-05-30 20:52:25 +02:00
2013-07-18 16:17:02 +02:00
if source_type == target_type && source_role == target_role
2009-12-12 10:06:07 +00:00
false
else
transaction do
2016-09-08 08:49:50 +02:00
where(type_id: target_type.id, role_id: target_role.id).delete_all
2014-02-21 08:15:51 +01:00
connection.insert <<-SQL
INSERT INTO #{Workflow.table_name} (type_id, role_id, old_status_id, new_status_id, author, assignee)
SELECT #{target_type.id}, #{target_role.id}, old_status_id, new_status_id, author, assignee
FROM #{Workflow.table_name}
WHERE type_id = #{source_type.id} AND role_id = #{source_role.id}
SQL
2009-12-12 10:06:07 +00:00
end
true
end
end
def self.eligible_roles
roles = Role.where(type: ProjectRole.name)
if EnterpriseToken.allows_to?(:work_package_sharing)
roles.or(Role.where(builtin: Role::BUILTIN_WORK_PACKAGE_EDITOR))
else
roles
end
end
2006-06-28 18:11:03 +00:00
end