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