##// END OF EJS Templates
Code cleanup....
Jean-Philippe Lang -
r10660:9a270c46c0aa
parent child
Show More
@@ -1,48 +1,48
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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 IssueCategory < ActiveRecord::Base
19 19 include Redmine::SafeAttributes
20 20 belongs_to :project
21 21 belongs_to :assigned_to, :class_name => 'Principal', :foreign_key => 'assigned_to_id'
22 22 has_many :issues, :foreign_key => 'category_id', :dependent => :nullify
23 23
24 24 validates_presence_of :name
25 25 validates_uniqueness_of :name, :scope => [:project_id]
26 26 validates_length_of :name, :maximum => 30
27 27
28 28 safe_attributes 'name', 'assigned_to_id'
29 29
30 scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
30 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
31 31
32 32 alias :destroy_without_reassign :destroy
33 33
34 34 # Destroy the category
35 35 # If a category is specified, issues are reassigned to this category
36 36 def destroy(reassign_to = nil)
37 37 if reassign_to && reassign_to.is_a?(IssueCategory) && reassign_to.project == self.project
38 38 Issue.update_all("category_id = #{reassign_to.id}", "category_id = #{id}")
39 39 end
40 40 destroy_without_reassign
41 41 end
42 42
43 43 def <=>(category)
44 44 name <=> category.name
45 45 end
46 46
47 47 def to_s; name end
48 48 end
@@ -1,105 +1,105
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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 21 acts_as_list
22 22
23 23 before_destroy :delete_workflow_rules
24 24 after_save :update_default
25 25
26 26 validates_presence_of :name
27 27 validates_uniqueness_of :name
28 28 validates_length_of :name, :maximum => 30
29 29 validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
30 30
31 31 scope :sorted, order("#{table_name}.position ASC")
32 scope :named, lambda {|arg| where(["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip])}
32 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
33 33
34 34 def update_default
35 35 IssueStatus.update_all({:is_default => false}, ['id <> ?', id]) if self.is_default?
36 36 end
37 37
38 38 # Returns the default status for new issues
39 39 def self.default
40 40 where(:is_default => true).first
41 41 end
42 42
43 43 # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
44 44 def self.update_issue_done_ratios
45 45 if Issue.use_status_for_done_ratio?
46 46 IssueStatus.where("default_done_ratio >= 0").all.each do |status|
47 47 Issue.update_all({:done_ratio => status.default_done_ratio}, {:status_id => status.id})
48 48 end
49 49 end
50 50
51 51 return Issue.use_status_for_done_ratio?
52 52 end
53 53
54 54 # Returns an array of all statuses the given role can switch to
55 55 # Uses association cache when called more than one time
56 56 def new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
57 57 if roles && tracker
58 58 role_ids = roles.collect(&:id)
59 59 transitions = workflows.select do |w|
60 60 role_ids.include?(w.role_id) &&
61 61 w.tracker_id == tracker.id &&
62 62 ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee))
63 63 end
64 64 transitions.map(&:new_status).compact.sort
65 65 else
66 66 []
67 67 end
68 68 end
69 69
70 70 # Same thing as above but uses a database query
71 71 # More efficient than the previous method if called just once
72 72 def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
73 73 if roles.present? && tracker
74 74 conditions = "(author = :false AND assignee = :false)"
75 75 conditions << " OR author = :true" if author
76 76 conditions << " OR assignee = :true" if assignee
77 77
78 78 workflows.
79 79 includes(:new_status).
80 80 where(["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})",
81 81 {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false}
82 82 ]).all.
83 83 map(&:new_status).compact.sort
84 84 else
85 85 []
86 86 end
87 87 end
88 88
89 89 def <=>(status)
90 90 position <=> status.position
91 91 end
92 92
93 93 def to_s; name end
94 94
95 95 private
96 96
97 97 def check_integrity
98 98 raise "Can't delete status" if Issue.where(:status_id => id).any?
99 99 end
100 100
101 101 # Deletes associated workflows
102 102 def delete_workflow_rules
103 103 WorkflowRule.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
104 104 end
105 105 end
@@ -1,285 +1,286
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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 Version < ActiveRecord::Base
19 19 include Redmine::SafeAttributes
20 20 after_update :update_issues_from_sharing_change
21 21 belongs_to :project
22 22 has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify
23 23 acts_as_customizable
24 24 acts_as_attachable :view_permission => :view_files,
25 25 :delete_permission => :manage_files
26 26
27 27 VERSION_STATUSES = %w(open locked closed)
28 28 VERSION_SHARINGS = %w(none descendants hierarchy tree system)
29 29
30 30 validates_presence_of :name
31 31 validates_uniqueness_of :name, :scope => [:project_id]
32 32 validates_length_of :name, :maximum => 60
33 33 validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true
34 34 validates_inclusion_of :status, :in => VERSION_STATUSES
35 35 validates_inclusion_of :sharing, :in => VERSION_SHARINGS
36 36 validate :validate_version
37 37
38 scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
39 scope :open, :conditions => {:status => 'open'}
40 scope :visible, lambda {|*args| { :include => :project,
41 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
38 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
39 scope :open, where(:status => 'open')
40 scope :visible, lambda {|*args|
41 includes(:project).where(Project.allowed_to_condition(args.first || User.current, :view_issues))
42 }
42 43
43 44 safe_attributes 'name',
44 45 'description',
45 46 'effective_date',
46 47 'due_date',
47 48 'wiki_page_title',
48 49 'status',
49 50 'sharing',
50 51 'custom_field_values'
51 52
52 53 # Returns true if +user+ or current user is allowed to view the version
53 54 def visible?(user=User.current)
54 55 user.allowed_to?(:view_issues, self.project)
55 56 end
56 57
57 58 # Version files have same visibility as project files
58 59 def attachments_visible?(*args)
59 60 project.present? && project.attachments_visible?(*args)
60 61 end
61 62
62 63 def start_date
63 64 @start_date ||= fixed_issues.minimum('start_date')
64 65 end
65 66
66 67 def due_date
67 68 effective_date
68 69 end
69 70
70 71 def due_date=(arg)
71 72 self.effective_date=(arg)
72 73 end
73 74
74 75 # Returns the total estimated time for this version
75 76 # (sum of leaves estimated_hours)
76 77 def estimated_hours
77 78 @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f
78 79 end
79 80
80 81 # Returns the total reported time for this version
81 82 def spent_hours
82 @spent_hours ||= TimeEntry.sum(:hours, :joins => :issue, :conditions => ["#{Issue.table_name}.fixed_version_id = ?", id]).to_f
83 @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f
83 84 end
84 85
85 86 def closed?
86 87 status == 'closed'
87 88 end
88 89
89 90 def open?
90 91 status == 'open'
91 92 end
92 93
93 94 # Returns true if the version is completed: due date reached and no open issues
94 95 def completed?
95 96 effective_date && (effective_date < Date.today) && (open_issues_count == 0)
96 97 end
97 98
98 99 def behind_schedule?
99 100 if completed_pourcent == 100
100 101 return false
101 102 elsif due_date && start_date
102 103 done_date = start_date + ((due_date - start_date+1)* completed_pourcent/100).floor
103 104 return done_date <= Date.today
104 105 else
105 106 false # No issues so it's not late
106 107 end
107 108 end
108 109
109 110 # Returns the completion percentage of this version based on the amount of open/closed issues
110 111 # and the time spent on the open issues.
111 112 def completed_pourcent
112 113 if issues_count == 0
113 114 0
114 115 elsif open_issues_count == 0
115 116 100
116 117 else
117 118 issues_progress(false) + issues_progress(true)
118 119 end
119 120 end
120 121
121 122 # Returns the percentage of issues that have been marked as 'closed'.
122 123 def closed_pourcent
123 124 if issues_count == 0
124 125 0
125 126 else
126 127 issues_progress(false)
127 128 end
128 129 end
129 130
130 131 # Returns true if the version is overdue: due date reached and some open issues
131 132 def overdue?
132 133 effective_date && (effective_date < Date.today) && (open_issues_count > 0)
133 134 end
134 135
135 136 # Returns assigned issues count
136 137 def issues_count
137 138 load_issue_counts
138 139 @issue_count
139 140 end
140 141
141 142 # Returns the total amount of open issues for this version.
142 143 def open_issues_count
143 144 load_issue_counts
144 145 @open_issues_count
145 146 end
146 147
147 148 # Returns the total amount of closed issues for this version.
148 149 def closed_issues_count
149 150 load_issue_counts
150 151 @closed_issues_count
151 152 end
152 153
153 154 def wiki_page
154 155 if project.wiki && !wiki_page_title.blank?
155 156 @wiki_page ||= project.wiki.find_page(wiki_page_title)
156 157 end
157 158 @wiki_page
158 159 end
159 160
160 161 def to_s; name end
161 162
162 163 def to_s_with_project
163 164 "#{project} - #{name}"
164 165 end
165 166
166 167 # Versions are sorted by effective_date and name
167 168 # Those with no effective_date are at the end, sorted by name
168 169 def <=>(version)
169 170 if self.effective_date
170 171 if version.effective_date
171 172 if self.effective_date == version.effective_date
172 173 name == version.name ? id <=> version.id : name <=> version.name
173 174 else
174 175 self.effective_date <=> version.effective_date
175 176 end
176 177 else
177 178 -1
178 179 end
179 180 else
180 181 if version.effective_date
181 182 1
182 183 else
183 184 name == version.name ? id <=> version.id : name <=> version.name
184 185 end
185 186 end
186 187 end
187 188
188 189 def self.fields_for_order_statement(table=nil)
189 190 table ||= table_name
190 191 ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"]
191 192 end
192 193
193 194 scope :sorted, order(fields_for_order_statement)
194 195
195 196 # Returns the sharings that +user+ can set the version to
196 197 def allowed_sharings(user = User.current)
197 198 VERSION_SHARINGS.select do |s|
198 199 if sharing == s
199 200 true
200 201 else
201 202 case s
202 203 when 'system'
203 204 # Only admin users can set a systemwide sharing
204 205 user.admin?
205 206 when 'hierarchy', 'tree'
206 207 # Only users allowed to manage versions of the root project can
207 208 # set sharing to hierarchy or tree
208 209 project.nil? || user.allowed_to?(:manage_versions, project.root)
209 210 else
210 211 true
211 212 end
212 213 end
213 214 end
214 215 end
215 216
216 217 private
217 218
218 219 def load_issue_counts
219 220 unless @issue_count
220 221 @open_issues_count = 0
221 222 @closed_issues_count = 0
222 223 fixed_issues.count(:all, :group => :status).each do |status, count|
223 224 if status.is_closed?
224 225 @closed_issues_count += count
225 226 else
226 227 @open_issues_count += count
227 228 end
228 229 end
229 230 @issue_count = @open_issues_count + @closed_issues_count
230 231 end
231 232 end
232 233
233 234 # Update the issue's fixed versions. Used if a version's sharing changes.
234 235 def update_issues_from_sharing_change
235 236 if sharing_changed?
236 237 if VERSION_SHARINGS.index(sharing_was).nil? ||
237 238 VERSION_SHARINGS.index(sharing).nil? ||
238 239 VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing)
239 240 Issue.update_versions_from_sharing_change self
240 241 end
241 242 end
242 243 end
243 244
244 245 # Returns the average estimated time of assigned issues
245 246 # or 1 if no issue has an estimated time
246 247 # Used to weigth unestimated issues in progress calculation
247 248 def estimated_average
248 249 if @estimated_average.nil?
249 250 average = fixed_issues.average(:estimated_hours).to_f
250 251 if average == 0
251 252 average = 1
252 253 end
253 254 @estimated_average = average
254 255 end
255 256 @estimated_average
256 257 end
257 258
258 259 # Returns the total progress of open or closed issues. The returned percentage takes into account
259 260 # the amount of estimated time set for this version.
260 261 #
261 262 # Examples:
262 263 # issues_progress(true) => returns the progress percentage for open issues.
263 264 # issues_progress(false) => returns the progress percentage for closed issues.
264 265 def issues_progress(open)
265 266 @issues_progress ||= {}
266 267 @issues_progress[open] ||= begin
267 268 progress = 0
268 269 if issues_count > 0
269 270 ratio = open ? 'done_ratio' : 100
270 271
271 272 done = fixed_issues.sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}",
272 273 :joins => :status,
273 274 :conditions => ["#{IssueStatus.table_name}.is_closed = ?", !open]).to_f
274 275 progress = done / (estimated_average * issues_count)
275 276 end
276 277 progress
277 278 end
278 279 end
279 280
280 281 def validate_version
281 282 if effective_date.nil? && @attributes['effective_date'].present?
282 283 errors.add :effective_date, :not_a_date
283 284 end
284 285 end
285 286 end
General Comments 0
You need to be logged in to leave comments. Login now