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_transition.rb 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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 {|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. }
  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 transition == "1" || transition == true
  47. unless w
  48. w = WorkflowTransition.new(
  49. :old_status_id => old_status_id,
  50. :new_status_id => new_status_id,
  51. :tracker_id => tracker.id,
  52. :role_id => role.id
  53. )
  54. records << w
  55. end
  56. w.author = true if rule == "author"
  57. w.assignee = true if rule == "assignee"
  58. w.save if w.changed?
  59. elsif w
  60. if rule == 'always'
  61. w.destroy
  62. elsif rule == 'author'
  63. if w.assignee
  64. w.author = false
  65. w.save if w.changed?
  66. else
  67. w.destroy
  68. end
  69. elsif rule == 'assignee'
  70. if w.author
  71. w.assignee = false
  72. w.save if w.changed?
  73. else
  74. w.destroy
  75. end
  76. end
  77. end
  78. end
  79. end
  80. end
  81. end
  82. end
  83. end
  84. end
  85. end