annotate .svn/pristine/a4/a4c45a7af3a5e670c765d740cec096dac5d5314d.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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