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

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