##// END OF EJS Templates
Query IssueStatus model to prevent workflows instanciation....
Jean-Philippe Lang -
r13134:bdbfe4f7ba36
parent child
Show More
@@ -1,106 +1,110
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 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 IssueStatus < ActiveRecord::Base
19 19 before_destroy :check_integrity
20 20 has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id"
21 has_many :workflow_transitions_as_new_status, :class_name => 'WorkflowTransition', :foreign_key => "new_status_id"
21 22 acts_as_list
22 23
23 24 before_destroy :delete_workflow_rules
24 25 after_save :update_default
25 26
26 27 validates_presence_of :name
27 28 validates_uniqueness_of :name
28 29 validates_length_of :name, :maximum => 30
29 30 validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
30 31 attr_protected :id
31 32
32 33 scope :sorted, lambda { order("#{table_name}.position ASC") }
33 34 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
34 35
35 36 def update_default
36 37 IssueStatus.where(['id <> ?', id]).update_all({:is_default => false}) if self.is_default?
37 38 end
38 39
39 40 # Returns the default status for new issues
40 41 def self.default
41 42 where(:is_default => true).first
42 43 end
43 44
44 45 # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
45 46 def self.update_issue_done_ratios
46 47 if Issue.use_status_for_done_ratio?
47 48 IssueStatus.where("default_done_ratio >= 0").each do |status|
48 49 Issue.where({:status_id => status.id}).update_all({:done_ratio => status.default_done_ratio})
49 50 end
50 51 end
51 52
52 53 return Issue.use_status_for_done_ratio?
53 54 end
54 55
55 56 # Returns an array of all statuses the given role can switch to
56 57 # Uses association cache when called more than one time
57 58 def new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
58 59 if roles && tracker
59 60 role_ids = roles.collect(&:id)
60 61 transitions = workflows.select do |w|
61 62 role_ids.include?(w.role_id) &&
62 63 w.tracker_id == tracker.id &&
63 64 ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee))
64 65 end
65 66 transitions.map(&:new_status).compact.sort
66 67 else
67 68 []
68 69 end
69 70 end
70 71
71 72 # Same thing as above but uses a database query
72 73 # More efficient than the previous method if called just once
73 74 def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
74 75 if roles.present? && tracker
75 conditions = "(author = :false AND assignee = :false)"
76 conditions << " OR author = :true" if author
77 conditions << " OR assignee = :true" if assignee
78
79 workflows.
80 includes(:new_status).
81 where(["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})",
82 {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false}
83 ]).to_a.
84 map(&:new_status).compact.sort
76 scope = IssueStatus.
77 joins(:workflow_transitions_as_new_status).
78 where(:workflows => {:old_status_id => id, :role_id => roles.map(&:id), :tracker_id => tracker.id})
79
80 unless author && assignee
81 if author || assignee
82 scope = scope.where("author = ? OR assignee = ?", author, assignee)
83 else
84 scope = scope.where("author = ? AND assignee = ?", false, false)
85 end
86 end
87
88 scope.uniq.to_a.sort
85 89 else
86 90 []
87 91 end
88 92 end
89 93
90 94 def <=>(status)
91 95 position <=> status.position
92 96 end
93 97
94 98 def to_s; name end
95 99
96 100 private
97 101
98 102 def check_integrity
99 103 raise "Can't delete status" if Issue.where(:status_id => id).any?
100 104 end
101 105
102 106 # Deletes associated workflows
103 107 def delete_workflow_rules
104 108 WorkflowRule.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
105 109 end
106 110 end
General Comments 0
You need to be logged in to leave comments. Login now