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.

issue_status.rb 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 IssueStatus < ApplicationRecord
  19. include Redmine::SafeAttributes
  20. before_destroy :check_integrity
  21. has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id"
  22. has_many :workflow_transitions_as_new_status, :class_name => 'WorkflowTransition', :foreign_key => "new_status_id"
  23. acts_as_positioned
  24. after_update :handle_is_closed_change
  25. before_destroy :delete_workflow_rules
  26. validates_presence_of :name
  27. validates_uniqueness_of :name, :case_sensitive => true
  28. validates_length_of :name, :maximum => 30
  29. validates_length_of :description, :maximum => 255
  30. validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
  31. scope :sorted, lambda {order(:position)}
  32. scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
  33. safe_attributes(
  34. 'name',
  35. 'description',
  36. 'is_closed',
  37. 'position',
  38. 'default_done_ratio')
  39. # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
  40. def self.update_issue_done_ratios
  41. if Issue.use_status_for_done_ratio?
  42. IssueStatus.where("default_done_ratio >= 0").each do |status|
  43. Issue.where({:status_id => status.id}).update_all({:done_ratio => status.default_done_ratio})
  44. end
  45. end
  46. return Issue.use_status_for_done_ratio?
  47. end
  48. # Returns an array of all statuses the given role can switch to
  49. def new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
  50. self.class.new_statuses_allowed(self, roles, tracker, author, assignee)
  51. end
  52. alias :find_new_statuses_allowed_to :new_statuses_allowed_to
  53. def self.new_statuses_allowed(status, roles, tracker, author=false, assignee=false)
  54. if roles.present? && tracker
  55. status_id = status.try(:id) || 0
  56. scope = IssueStatus.
  57. joins(:workflow_transitions_as_new_status).
  58. where(:workflows => {:old_status_id => status_id, :role_id => roles.map(&:id), :tracker_id => tracker.id})
  59. unless author && assignee
  60. if author || assignee
  61. scope = scope.where("author = ? OR assignee = ?", author, assignee)
  62. else
  63. scope = scope.where("author = ? AND assignee = ?", false, false)
  64. end
  65. end
  66. scope.distinct.to_a.sort
  67. else
  68. []
  69. end
  70. end
  71. def <=>(status)
  72. return nil unless status.is_a?(IssueStatus)
  73. position <=> status.position
  74. end
  75. def to_s; name end
  76. private
  77. # Updates issues closed_on attribute when an existing status is set as closed.
  78. def handle_is_closed_change
  79. if saved_change_to_is_closed? && is_closed == true
  80. # First we update issues that have a journal for when the current status was set,
  81. # a subselect is used to update all issues with a single query
  82. subquery = Journal.joins(:details).
  83. where(:journalized_type => 'Issue').
  84. where("journalized_id = #{Issue.table_name}.id").
  85. where(:journal_details => {:property => 'attr', :prop_key => 'status_id', :value => id.to_s}).
  86. select("MAX(created_on)").
  87. to_sql
  88. Issue.where(:status_id => id, :closed_on => nil).update_all("closed_on = (#{subquery})")
  89. # Then we update issues that don't have a journal which means the
  90. # current status was set on creation
  91. Issue.where(:status_id => id, :closed_on => nil).update_all("closed_on = created_on")
  92. end
  93. end
  94. def check_integrity
  95. if Issue.where(:status_id => id).any?
  96. raise "This status is used by some issues"
  97. elsif Tracker.where(:default_status_id => id).any?
  98. raise "This status is used as the default status by some trackers"
  99. end
  100. end
  101. # Deletes associated workflows
  102. def delete_workflow_rules
  103. WorkflowRule.where(:old_status_id => id).delete_all
  104. WorkflowRule.where(:new_status_id => id).delete_all
  105. end
  106. end