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.2KB

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