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