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

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