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

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