comparison app/models/issue_status.rb @ 909:cbb26bc654de redmine-1.3

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