You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

workflow_rule.rb 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class WorkflowRule < ApplicationRecord
  19. self.table_name = "#{table_name_prefix}workflows#{table_name_suffix}"
  20. belongs_to :role
  21. belongs_to :tracker
  22. belongs_to :old_status, :class_name => 'IssueStatus'
  23. belongs_to :new_status, :class_name => 'IssueStatus'
  24. validates_presence_of :role, :tracker
  25. # Copies workflows from source to targets
  26. def self.copy(source_tracker, source_role, target_trackers, target_roles)
  27. unless source_tracker.is_a?(Tracker) || source_role.is_a?(Role)
  28. raise ArgumentError.new(
  29. "source_tracker or source_role must be specified, given: " \
  30. "#{source_tracker.class.name} and #{source_role.class.name}"
  31. )
  32. end
  33. target_trackers = [target_trackers].flatten.compact
  34. target_roles = [target_roles].flatten.compact
  35. target_trackers = Tracker.sorted.to_a if target_trackers.empty?
  36. target_roles = Role.all.select(&:consider_workflow?) if target_roles.empty?
  37. target_trackers.each do |target_tracker|
  38. target_roles.each do |target_role|
  39. copy_one(source_tracker || target_tracker,
  40. source_role || target_role,
  41. target_tracker,
  42. target_role)
  43. end
  44. end
  45. end
  46. # Copies a single set of workflows from source to target
  47. def self.copy_one(source_tracker, source_role, target_tracker, target_role)
  48. unless source_tracker.is_a?(Tracker) && !source_tracker.new_record? &&
  49. source_role.is_a?(Role) && !source_role.new_record? &&
  50. target_tracker.is_a?(Tracker) && !target_tracker.new_record? &&
  51. target_role.is_a?(Role) && !target_role.new_record?
  52. raise ArgumentError.new("arguments can not be nil or unsaved objects")
  53. end
  54. if source_tracker == target_tracker && source_role == target_role
  55. false
  56. else
  57. transaction do
  58. where(:tracker_id => target_tracker.id, :role_id => target_role.id).delete_all
  59. connection.insert(
  60. "INSERT INTO #{WorkflowRule.table_name}" \
  61. " (tracker_id, role_id, old_status_id, new_status_id," \
  62. " author, assignee, field_name, #{connection.quote_column_name 'rule'}, type)" \
  63. " SELECT #{target_tracker.id}, #{target_role.id}, old_status_id, new_status_id," \
  64. " author, assignee, field_name, #{connection.quote_column_name 'rule'}, type" \
  65. " FROM #{WorkflowRule.table_name}" \
  66. " WHERE tracker_id = #{source_tracker.id} AND role_id = #{source_role.id}"
  67. )
  68. end
  69. true
  70. end
  71. end
  72. end