annotate app/models/issue_status.rb @ 853:969bb872d4bf feature_142

Close obsolete branch feature_142
author Chris Cannam
date Thu, 14 Jul 2011 14:26:44 +0100
parents 051f544170fe
children 0c939c159af4
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class IssueStatus < ActiveRecord::Base
Chris@0 19 before_destroy :check_integrity
Chris@1 20 has_many :workflows, :foreign_key => "old_status_id"
Chris@0 21 acts_as_list
Chris@1 22
Chris@1 23 before_destroy :delete_workflows
Chris@0 24
Chris@0 25 validates_presence_of :name
Chris@0 26 validates_uniqueness_of :name
Chris@0 27 validates_length_of :name, :maximum => 30
Chris@0 28 validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
Chris@0 29
Chris@0 30 def after_save
Chris@0 31 IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default?
Chris@0 32 end
Chris@0 33
Chris@0 34 # Returns the default status for new issues
Chris@0 35 def self.default
Chris@0 36 find(:first, :conditions =>["is_default=?", true])
Chris@0 37 end
Chris@0 38
Chris@0 39 # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
Chris@0 40 def self.update_issue_done_ratios
Chris@0 41 if Issue.use_status_for_done_ratio?
Chris@0 42 IssueStatus.find(:all, :conditions => ["default_done_ratio >= 0"]).each do |status|
Chris@0 43 Issue.update_all(["done_ratio = ?", status.default_done_ratio],
Chris@0 44 ["status_id = ?", status.id])
Chris@0 45 end
Chris@0 46 end
Chris@0 47
Chris@0 48 return Issue.use_status_for_done_ratio?
Chris@0 49 end
Chris@0 50
Chris@0 51 # Returns an array of all statuses the given role can switch to
Chris@0 52 # Uses association cache when called more than one time
Chris@245 53 def new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
Chris@0 54 if roles && tracker
Chris@0 55 role_ids = roles.collect(&:id)
Chris@245 56 transitions = workflows.select do |w|
Chris@245 57 role_ids.include?(w.role_id) &&
Chris@245 58 w.tracker_id == tracker.id &&
Chris@245 59 (author || !w.author) &&
Chris@245 60 (assignee || !w.assignee)
Chris@245 61 end
Chris@245 62 transitions.collect{|w| w.new_status}.compact.sort
Chris@0 63 else
Chris@0 64 []
Chris@0 65 end
Chris@0 66 end
Chris@0 67
Chris@0 68 # Same thing as above but uses a database query
Chris@0 69 # More efficient than the previous method if called just once
Chris@245 70 def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
Chris@0 71 if roles && tracker
Chris@245 72 conditions = {:role_id => roles.collect(&:id), :tracker_id => tracker.id}
Chris@245 73 conditions[:author] = false unless author
Chris@245 74 conditions[:assignee] = false unless assignee
Chris@245 75
Chris@0 76 workflows.find(:all,
Chris@0 77 :include => :new_status,
Chris@245 78 :conditions => conditions).collect{|w| w.new_status}.compact.sort
Chris@0 79 else
Chris@0 80 []
Chris@0 81 end
Chris@0 82 end
Chris@0 83
Chris@0 84 def <=>(status)
Chris@0 85 position <=> status.position
Chris@0 86 end
Chris@0 87
Chris@0 88 def to_s; name end
Chris@0 89
Chris@0 90 private
Chris@0 91 def check_integrity
Chris@0 92 raise "Can't delete status" if Issue.find(:first, :conditions => ["status_id=?", self.id])
Chris@0 93 end
Chris@1 94
Chris@1 95 # Deletes associated workflows
Chris@1 96 def delete_workflows
Chris@1 97 Workflow.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
Chris@1 98 end
Chris@0 99 end