Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class IssueStatus < ActiveRecord::Base Chris@0: before_destroy :check_integrity Chris@1: has_many :workflows, :foreign_key => "old_status_id" Chris@0: acts_as_list Chris@1: Chris@1: before_destroy :delete_workflows Chris@0: Chris@0: validates_presence_of :name Chris@0: validates_uniqueness_of :name Chris@0: validates_length_of :name, :maximum => 30 Chris@0: validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true Chris@507: Chris@507: named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}} Chris@0: Chris@0: def after_save Chris@0: IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default? Chris@0: end Chris@0: Chris@0: # Returns the default status for new issues Chris@0: def self.default Chris@0: find(:first, :conditions =>["is_default=?", true]) Chris@0: end Chris@0: Chris@0: # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+ Chris@0: def self.update_issue_done_ratios Chris@0: if Issue.use_status_for_done_ratio? Chris@0: IssueStatus.find(:all, :conditions => ["default_done_ratio >= 0"]).each do |status| Chris@0: Issue.update_all(["done_ratio = ?", status.default_done_ratio], Chris@0: ["status_id = ?", status.id]) Chris@0: end Chris@0: end Chris@0: Chris@0: return Issue.use_status_for_done_ratio? Chris@0: end Chris@0: Chris@0: # Returns an array of all statuses the given role can switch to Chris@0: # Uses association cache when called more than one time Chris@245: def new_statuses_allowed_to(roles, tracker, author=false, assignee=false) Chris@0: if roles && tracker Chris@0: role_ids = roles.collect(&:id) Chris@245: transitions = workflows.select do |w| Chris@245: role_ids.include?(w.role_id) && Chris@245: w.tracker_id == tracker.id && Chris@245: (author || !w.author) && Chris@245: (assignee || !w.assignee) Chris@245: end Chris@245: transitions.collect{|w| w.new_status}.compact.sort Chris@0: else Chris@0: [] Chris@0: end Chris@0: end Chris@0: Chris@0: # Same thing as above but uses a database query Chris@0: # More efficient than the previous method if called just once Chris@245: def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false) Chris@0: if roles && tracker Chris@245: conditions = {:role_id => roles.collect(&:id), :tracker_id => tracker.id} Chris@245: conditions[:author] = false unless author Chris@245: conditions[:assignee] = false unless assignee Chris@245: Chris@0: workflows.find(:all, Chris@0: :include => :new_status, Chris@245: :conditions => conditions).collect{|w| w.new_status}.compact.sort Chris@0: else Chris@0: [] Chris@0: end Chris@0: end Chris@0: Chris@0: def <=>(status) Chris@0: position <=> status.position Chris@0: end Chris@0: Chris@0: def to_s; name end Chris@0: Chris@0: private Chris@0: def check_integrity Chris@0: raise "Can't delete status" if Issue.find(:first, :conditions => ["status_id=?", self.id]) Chris@0: end Chris@1: Chris@1: # Deletes associated workflows Chris@1: def delete_workflows Chris@1: Workflow.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}]) Chris@1: end Chris@0: end