annotate .svn/pristine/a8/a8aa1c2321ca25de25fb93436b542852bd6be325.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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