annotate .svn/pristine/66/662e86180d02fd07ea0b83b2c6615af46feab399.svn-base @ 1464:261b3d9a4903 redmine-2.4

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