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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 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. before_destroy :check_integrity
  19. has_many :workflows, :foreign_key => "old_status_id"
  20. acts_as_list
  21. before_destroy :delete_workflows
  22. after_save :update_default
  23. validates_presence_of :name
  24. validates_uniqueness_of :name
  25. validates_length_of :name, :maximum => 30
  26. validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
  27. named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
  28. def update_default
  29. IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default?
  30. end
  31. # Returns the default status for new issues
  32. def self.default
  33. find(:first, :conditions =>["is_default=?", true])
  34. end
  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.find(:all, :conditions => ["default_done_ratio >= 0"]).each do |status|
  39. Issue.update_all(["done_ratio = ?", status.default_done_ratio],
  40. ["status_id = ?", status.id])
  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. # Uses association cache when called more than one time
  47. def new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
  48. if roles && tracker
  49. role_ids = roles.collect(&:id)
  50. transitions = workflows.select do |w|
  51. role_ids.include?(w.role_id) &&
  52. w.tracker_id == tracker.id &&
  53. ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee))
  54. end
  55. transitions.collect{|w| w.new_status}.compact.sort
  56. else
  57. []
  58. end
  59. end
  60. # Same thing as above but uses a database query
  61. # More efficient than the previous method if called just once
  62. def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
  63. if roles.present? && tracker
  64. conditions = "(author = :false AND assignee = :false)"
  65. conditions << " OR author = :true" if author
  66. conditions << " OR assignee = :true" if assignee
  67. workflows.find(:all,
  68. :include => :new_status,
  69. :conditions => ["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})",
  70. {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false}
  71. ]
  72. ).collect{|w| w.new_status}.compact.sort
  73. else
  74. []
  75. end
  76. end
  77. def <=>(status)
  78. position <=> status.position
  79. end
  80. def to_s; name end
  81. private
  82. def check_integrity
  83. raise "Can't delete status" if Issue.find(:first, :conditions => ["status_id=?", self.id])
  84. end
  85. # Deletes associated workflows
  86. def delete_workflows
  87. Workflow.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
  88. end
  89. end