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

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