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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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 WorkflowTransition < WorkflowRule
  19. validates_presence_of :new_status
  20. def self.replace_transitions(trackers, roles, transitions)
  21. trackers = Array.wrap trackers
  22. roles = Array.wrap roles
  23. transaction do
  24. records = WorkflowTransition.where(:tracker_id => trackers.map(&:id), :role_id => roles.map(&:id)).to_a
  25. transitions.each do |old_status_id, transitions_by_new_status|
  26. transitions_by_new_status.each do |new_status_id, transition_by_rule|
  27. transition_by_rule.each do |rule, transition|
  28. trackers.each do |tracker|
  29. roles.each do |role|
  30. w = records.select do |r|
  31. r.old_status_id == old_status_id.to_i &&
  32. r.new_status_id == new_status_id.to_i &&
  33. r.tracker_id == tracker.id &&
  34. r.role_id == role.id &&
  35. !r.destroyed?
  36. end
  37. if rule == 'always'
  38. w = w.select {|r| !r.author && !r.assignee}
  39. else
  40. w = w.select {|r| r.author || r.assignee}
  41. end
  42. if w.size > 1
  43. w[1..-1].each(&:destroy)
  44. end
  45. w = w.first
  46. if ["1", true].include?(transition)
  47. unless w
  48. w = WorkflowTransition.
  49. new(
  50. :old_status_id => old_status_id,
  51. :new_status_id => new_status_id,
  52. :tracker_id => tracker.id,
  53. :role_id => role.id
  54. )
  55. records << w
  56. end
  57. w.author = true if rule == "author"
  58. w.assignee = true if rule == "assignee"
  59. w.save if w.changed?
  60. elsif w
  61. if rule == 'always'
  62. w.destroy
  63. elsif rule == 'author'
  64. if w.assignee
  65. w.author = false
  66. w.save if w.changed?
  67. else
  68. w.destroy
  69. end
  70. elsif rule == 'assignee'
  71. if w.author
  72. w.assignee = false
  73. w.save if w.changed?
  74. else
  75. w.destroy
  76. end
  77. end
  78. end
  79. end
  80. end
  81. end
  82. end
  83. end
  84. end
  85. end
  86. end