@@ -1,100 +1,100 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class Workflow < ActiveRecord::Base |
|
19 | 19 | belongs_to :role |
|
20 | 20 | belongs_to :old_status, :class_name => 'IssueStatus', :foreign_key => 'old_status_id' |
|
21 | 21 | belongs_to :new_status, :class_name => 'IssueStatus', :foreign_key => 'new_status_id' |
|
22 | 22 | |
|
23 | 23 | validates_presence_of :role, :old_status, :new_status |
|
24 | 24 | |
|
25 | 25 | # Returns workflow transitions count by tracker and role |
|
26 | 26 | def self.count_by_tracker_and_role |
|
27 | 27 | counts = connection.select_all("SELECT role_id, tracker_id, count(id) AS c FROM #{Workflow.table_name} GROUP BY role_id, tracker_id") |
|
28 | 28 | roles = Role.find(:all, :order => 'builtin, position') |
|
29 | 29 | trackers = Tracker.find(:all, :order => 'position') |
|
30 | 30 | |
|
31 | 31 | result = [] |
|
32 | 32 | trackers.each do |tracker| |
|
33 | 33 | t = [] |
|
34 | 34 | roles.each do |role| |
|
35 | 35 | row = counts.detect {|c| c['role_id'] == role.id.to_s && c['tracker_id'] == tracker.id.to_s} |
|
36 | 36 | t << [role, (row.nil? ? 0 : row['c'].to_i)] |
|
37 | 37 | end |
|
38 | 38 | result << [tracker, t] |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | result |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | # Find potential statuses the user could be allowed to switch issues to |
|
45 | 45 | def self.available_statuses(project, user=User.current) |
|
46 | 46 | Workflow.find(:all, |
|
47 | 47 | :include => :new_status, |
|
48 | 48 | :conditions => {:role_id => user.roles_for_project(project).collect(&:id)}). |
|
49 | 49 | collect(&:new_status). |
|
50 | 50 | compact. |
|
51 | 51 | uniq. |
|
52 | 52 | sort |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | # Copies workflows from source to targets |
|
56 | 56 | def self.copy(source_tracker, source_role, target_trackers, target_roles) |
|
57 | 57 | unless source_tracker.is_a?(Tracker) || source_role.is_a?(Role) |
|
58 |
raise ArgumentError.new |
|
|
58 | raise ArgumentError.new("source_tracker or source_role must be specified") | |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | target_trackers = [target_trackers].flatten.compact |
|
62 | 62 | target_roles = [target_roles].flatten.compact |
|
63 | 63 | |
|
64 | 64 | target_trackers = Tracker.all if target_trackers.empty? |
|
65 | 65 | target_roles = Role.all if target_roles.empty? |
|
66 | 66 | |
|
67 | 67 | target_trackers.each do |target_tracker| |
|
68 | 68 | target_roles.each do |target_role| |
|
69 | 69 | copy_one(source_tracker || target_tracker, |
|
70 | 70 | source_role || target_role, |
|
71 | 71 | target_tracker, |
|
72 | 72 | target_role) |
|
73 | 73 | end |
|
74 | 74 | end |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | # Copies a single set of workflows from source to target |
|
78 | 78 | def self.copy_one(source_tracker, source_role, target_tracker, target_role) |
|
79 | 79 | unless source_tracker.is_a?(Tracker) && !source_tracker.new_record? && |
|
80 | 80 | source_role.is_a?(Role) && !source_role.new_record? && |
|
81 | 81 | target_tracker.is_a?(Tracker) && !target_tracker.new_record? && |
|
82 | 82 | target_role.is_a?(Role) && !target_role.new_record? |
|
83 | 83 | |
|
84 | 84 | raise ArgumentError.new("arguments can not be nil or unsaved objects") |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | if source_tracker == target_tracker && source_role == target_role |
|
88 | 88 | false |
|
89 | 89 | else |
|
90 | 90 | transaction do |
|
91 | 91 | delete_all :tracker_id => target_tracker.id, :role_id => target_role.id |
|
92 | 92 | connection.insert "INSERT INTO #{Workflow.table_name} (tracker_id, role_id, old_status_id, new_status_id)" + |
|
93 | 93 | " SELECT #{target_tracker.id}, #{target_role.id}, old_status_id, new_status_id" + |
|
94 | 94 | " FROM #{Workflow.table_name}" + |
|
95 | 95 | " WHERE tracker_id = #{source_tracker.id} AND role_id = #{source_role.id}" |
|
96 | 96 | end |
|
97 | 97 | true |
|
98 | 98 | end |
|
99 | 99 | end |
|
100 | 100 | end |
General Comments 0
You need to be logged in to leave comments.
Login now