##// END OF EJS Templates
2 spaces instead of a tab....
Jean-Philippe Lang -
r15220:050d2bee8423
parent child
Show More
@@ -1,325 +1,325
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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
21 21 after_update :update_issues_from_sharing_change
22 22 before_destroy :nullify_projects_default_version
23 23
24 24 belongs_to :project
25 25 has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify
26 26 acts_as_customizable
27 27 acts_as_attachable :view_permission => :view_files,
28 28 :edit_permission => :manage_files,
29 29 :delete_permission => :manage_files
30 30
31 31 VERSION_STATUSES = %w(open locked closed)
32 32 VERSION_SHARINGS = %w(none descendants hierarchy tree system)
33 33
34 34 validates_presence_of :name
35 35 validates_uniqueness_of :name, :scope => [:project_id]
36 36 validates_length_of :name, :maximum => 60
37 37 validates_length_of :description, :maximum => 255
38 38 validates :effective_date, :date => true
39 39 validates_inclusion_of :status, :in => VERSION_STATUSES
40 40 validates_inclusion_of :sharing, :in => VERSION_SHARINGS
41 41 attr_protected :id
42 42
43 43 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
44 44 scope :open, lambda { where(:status => 'open') }
45 45 scope :visible, lambda {|*args|
46 46 joins(:project).
47 47 where(Project.allowed_to_condition(args.first || User.current, :view_issues))
48 48 }
49 49
50 50 safe_attributes 'name',
51 51 'description',
52 52 'effective_date',
53 53 'due_date',
54 54 'wiki_page_title',
55 55 'status',
56 56 'sharing',
57 57 'custom_field_values',
58 58 'custom_fields'
59 59
60 60 # Returns true if +user+ or current user is allowed to view the version
61 61 def visible?(user=User.current)
62 62 user.allowed_to?(:view_issues, self.project)
63 63 end
64 64
65 65 # Version files have same visibility as project files
66 66 def attachments_visible?(*args)
67 67 project.present? && project.attachments_visible?(*args)
68 68 end
69 69
70 70 def attachments_deletable?(usr=User.current)
71 71 project.present? && project.attachments_deletable?(usr)
72 72 end
73 73
74 74 def start_date
75 75 @start_date ||= fixed_issues.minimum('start_date')
76 76 end
77 77
78 78 def due_date
79 79 effective_date
80 80 end
81 81
82 82 def due_date=(arg)
83 83 self.effective_date=(arg)
84 84 end
85 85
86 86 # Returns the total estimated time for this version
87 87 # (sum of leaves estimated_hours)
88 88 def estimated_hours
89 89 @estimated_hours ||= fixed_issues.sum(:estimated_hours).to_f
90 90 end
91 91
92 92 # Returns the total reported time for this version
93 93 def spent_hours
94 94 @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f
95 95 end
96 96
97 97 def closed?
98 98 status == 'closed'
99 99 end
100 100
101 101 def open?
102 102 status == 'open'
103 103 end
104 104
105 105 # Returns true if the version is completed: closed or due date reached and no open issues
106 106 def completed?
107 107 closed? || (effective_date && (effective_date < User.current.today) && (open_issues_count == 0))
108 108 end
109 109
110 110 def behind_schedule?
111 111 if completed_percent == 100
112 112 return false
113 113 elsif due_date && start_date
114 114 done_date = start_date + ((due_date - start_date+1)* completed_percent/100).floor
115 115 return done_date <= User.current.today
116 116 else
117 117 false # No issues so it's not late
118 118 end
119 119 end
120 120
121 121 # Returns the completion percentage of this version based on the amount of open/closed issues
122 122 # and the time spent on the open issues.
123 123 def completed_percent
124 124 if issues_count == 0
125 125 0
126 126 elsif open_issues_count == 0
127 127 100
128 128 else
129 129 issues_progress(false) + issues_progress(true)
130 130 end
131 131 end
132 132
133 133 # Returns the percentage of issues that have been marked as 'closed'.
134 134 def closed_percent
135 135 if issues_count == 0
136 136 0
137 137 else
138 138 issues_progress(false)
139 139 end
140 140 end
141 141
142 142 # Returns true if the version is overdue: due date reached and some open issues
143 143 def overdue?
144 144 effective_date && (effective_date < User.current.today) && (open_issues_count > 0)
145 145 end
146 146
147 147 # Returns assigned issues count
148 148 def issues_count
149 149 load_issue_counts
150 150 @issue_count
151 151 end
152 152
153 153 # Returns the total amount of open issues for this version.
154 154 def open_issues_count
155 155 load_issue_counts
156 156 @open_issues_count
157 157 end
158 158
159 159 # Returns the total amount of closed issues for this version.
160 160 def closed_issues_count
161 161 load_issue_counts
162 162 @closed_issues_count
163 163 end
164 164
165 165 def wiki_page
166 166 if project.wiki && !wiki_page_title.blank?
167 167 @wiki_page ||= project.wiki.find_page(wiki_page_title)
168 168 end
169 169 @wiki_page
170 170 end
171 171
172 172 def to_s; name end
173 173
174 174 def to_s_with_project
175 175 "#{project} - #{name}"
176 176 end
177 177
178 178 # Versions are sorted by effective_date and name
179 179 # Those with no effective_date are at the end, sorted by name
180 180 def <=>(version)
181 181 if self.effective_date
182 182 if version.effective_date
183 183 if self.effective_date == version.effective_date
184 184 name == version.name ? id <=> version.id : name <=> version.name
185 185 else
186 186 self.effective_date <=> version.effective_date
187 187 end
188 188 else
189 189 -1
190 190 end
191 191 else
192 192 if version.effective_date
193 193 1
194 194 else
195 195 name == version.name ? id <=> version.id : name <=> version.name
196 196 end
197 197 end
198 198 end
199 199
200 # Sort versions by status (open, locked then closed versions)
200 # Sort versions by status (open, locked then closed versions)
201 201 def self.sort_by_status(versions)
202 202 versions.sort do |a, b|
203 203 if a.status == b.status
204 204 a <=> b
205 205 else
206 206 b.status <=> a.status
207 207 end
208 208 end
209 209 end
210 210
211 211 def css_classes
212 212 [
213 213 completed? ? 'version-completed' : 'version-incompleted',
214 214 "version-#{status}"
215 215 ].join(' ')
216 216 end
217 217
218 218 def self.fields_for_order_statement(table=nil)
219 219 table ||= table_name
220 220 ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"]
221 221 end
222 222
223 223 scope :sorted, lambda { order(fields_for_order_statement) }
224 224
225 225 # Returns the sharings that +user+ can set the version to
226 226 def allowed_sharings(user = User.current)
227 227 VERSION_SHARINGS.select do |s|
228 228 if sharing == s
229 229 true
230 230 else
231 231 case s
232 232 when 'system'
233 233 # Only admin users can set a systemwide sharing
234 234 user.admin?
235 235 when 'hierarchy', 'tree'
236 236 # Only users allowed to manage versions of the root project can
237 237 # set sharing to hierarchy or tree
238 238 project.nil? || user.allowed_to?(:manage_versions, project.root)
239 239 else
240 240 true
241 241 end
242 242 end
243 243 end
244 244 end
245 245
246 246 # Returns true if the version is shared, otherwise false
247 247 def shared?
248 248 sharing != 'none'
249 249 end
250 250
251 251 def deletable?
252 252 fixed_issues.empty? && !referenced_by_a_custom_field?
253 253 end
254 254
255 255 private
256 256
257 257 def load_issue_counts
258 258 unless @issue_count
259 259 @open_issues_count = 0
260 260 @closed_issues_count = 0
261 261 fixed_issues.group(:status).count.each do |status, count|
262 262 if status.is_closed?
263 263 @closed_issues_count += count
264 264 else
265 265 @open_issues_count += count
266 266 end
267 267 end
268 268 @issue_count = @open_issues_count + @closed_issues_count
269 269 end
270 270 end
271 271
272 272 # Update the issue's fixed versions. Used if a version's sharing changes.
273 273 def update_issues_from_sharing_change
274 274 if sharing_changed?
275 275 if VERSION_SHARINGS.index(sharing_was).nil? ||
276 276 VERSION_SHARINGS.index(sharing).nil? ||
277 277 VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing)
278 278 Issue.update_versions_from_sharing_change self
279 279 end
280 280 end
281 281 end
282 282
283 283 # Returns the average estimated time of assigned issues
284 284 # or 1 if no issue has an estimated time
285 285 # Used to weight unestimated issues in progress calculation
286 286 def estimated_average
287 287 if @estimated_average.nil?
288 288 average = fixed_issues.average(:estimated_hours).to_f
289 289 if average == 0
290 290 average = 1
291 291 end
292 292 @estimated_average = average
293 293 end
294 294 @estimated_average
295 295 end
296 296
297 297 # Returns the total progress of open or closed issues. The returned percentage takes into account
298 298 # the amount of estimated time set for this version.
299 299 #
300 300 # Examples:
301 301 # issues_progress(true) => returns the progress percentage for open issues.
302 302 # issues_progress(false) => returns the progress percentage for closed issues.
303 303 def issues_progress(open)
304 304 @issues_progress ||= {}
305 305 @issues_progress[open] ||= begin
306 306 progress = 0
307 307 if issues_count > 0
308 308 ratio = open ? 'done_ratio' : 100
309 309
310 310 done = fixed_issues.open(open).sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}").to_f
311 311 progress = done / (estimated_average * issues_count)
312 312 end
313 313 progress
314 314 end
315 315 end
316 316
317 317 def referenced_by_a_custom_field?
318 318 CustomValue.joins(:custom_field).
319 319 where(:value => id.to_s, :custom_fields => {:field_format => 'version'}).any?
320 320 end
321 321
322 322 def nullify_projects_default_version
323 323 Project.where(:default_version_id => id).update_all(:default_version_id => nil)
324 324 end
325 325 end
General Comments 0
You need to be logged in to leave comments. Login now