comparison .svn/pristine/75/753c25cc403b95007aa051a7ebe5a044110d3fc0.svn-base @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents
children
comparison
equal deleted inserted replaced
1294:3e4c3460b6ca 1295:622f24f53b42
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class Version < ActiveRecord::Base
19 include Redmine::SafeAttributes
20 after_update :update_issues_from_sharing_change
21 belongs_to :project
22 has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify
23 acts_as_customizable
24 acts_as_attachable :view_permission => :view_files,
25 :delete_permission => :manage_files
26
27 VERSION_STATUSES = %w(open locked closed)
28 VERSION_SHARINGS = %w(none descendants hierarchy tree system)
29
30 validates_presence_of :name
31 validates_uniqueness_of :name, :scope => [:project_id]
32 validates_length_of :name, :maximum => 60
33 validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true
34 validates_inclusion_of :status, :in => VERSION_STATUSES
35 validates_inclusion_of :sharing, :in => VERSION_SHARINGS
36 validate :validate_version
37
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 }
43
44 safe_attributes 'name',
45 'description',
46 'effective_date',
47 'due_date',
48 'wiki_page_title',
49 'status',
50 'sharing',
51 'custom_field_values'
52
53 # Returns true if +user+ or current user is allowed to view the version
54 def visible?(user=User.current)
55 user.allowed_to?(:view_issues, self.project)
56 end
57
58 # Version files have same visibility as project files
59 def attachments_visible?(*args)
60 project.present? && project.attachments_visible?(*args)
61 end
62
63 def start_date
64 @start_date ||= fixed_issues.minimum('start_date')
65 end
66
67 def due_date
68 effective_date
69 end
70
71 def due_date=(arg)
72 self.effective_date=(arg)
73 end
74
75 # Returns the total estimated time for this version
76 # (sum of leaves estimated_hours)
77 def estimated_hours
78 @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f
79 end
80
81 # Returns the total reported time for this version
82 def spent_hours
83 @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f
84 end
85
86 def closed?
87 status == 'closed'
88 end
89
90 def open?
91 status == 'open'
92 end
93
94 # Returns true if the version is completed: due date reached and no open issues
95 def completed?
96 effective_date && (effective_date < Date.today) && (open_issues_count == 0)
97 end
98
99 def behind_schedule?
100 if completed_pourcent == 100
101 return false
102 elsif due_date && start_date
103 done_date = start_date + ((due_date - start_date+1)* completed_pourcent/100).floor
104 return done_date <= Date.today
105 else
106 false # No issues so it's not late
107 end
108 end
109
110 # Returns the completion percentage of this version based on the amount of open/closed issues
111 # and the time spent on the open issues.
112 def completed_pourcent
113 if issues_count == 0
114 0
115 elsif open_issues_count == 0
116 100
117 else
118 issues_progress(false) + issues_progress(true)
119 end
120 end
121
122 # Returns the percentage of issues that have been marked as 'closed'.
123 def closed_pourcent
124 if issues_count == 0
125 0
126 else
127 issues_progress(false)
128 end
129 end
130
131 # Returns true if the version is overdue: due date reached and some open issues
132 def overdue?
133 effective_date && (effective_date < Date.today) && (open_issues_count > 0)
134 end
135
136 # Returns assigned issues count
137 def issues_count
138 load_issue_counts
139 @issue_count
140 end
141
142 # Returns the total amount of open issues for this version.
143 def open_issues_count
144 load_issue_counts
145 @open_issues_count
146 end
147
148 # Returns the total amount of closed issues for this version.
149 def closed_issues_count
150 load_issue_counts
151 @closed_issues_count
152 end
153
154 def wiki_page
155 if project.wiki && !wiki_page_title.blank?
156 @wiki_page ||= project.wiki.find_page(wiki_page_title)
157 end
158 @wiki_page
159 end
160
161 def to_s; name end
162
163 def to_s_with_project
164 "#{project} - #{name}"
165 end
166
167 # Versions are sorted by effective_date and name
168 # Those with no effective_date are at the end, sorted by name
169 def <=>(version)
170 if self.effective_date
171 if version.effective_date
172 if self.effective_date == version.effective_date
173 name == version.name ? id <=> version.id : name <=> version.name
174 else
175 self.effective_date <=> version.effective_date
176 end
177 else
178 -1
179 end
180 else
181 if version.effective_date
182 1
183 else
184 name == version.name ? id <=> version.id : name <=> version.name
185 end
186 end
187 end
188
189 def self.fields_for_order_statement(table=nil)
190 table ||= table_name
191 ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"]
192 end
193
194 scope :sorted, order(fields_for_order_statement)
195
196 # Returns the sharings that +user+ can set the version to
197 def allowed_sharings(user = User.current)
198 VERSION_SHARINGS.select do |s|
199 if sharing == s
200 true
201 else
202 case s
203 when 'system'
204 # Only admin users can set a systemwide sharing
205 user.admin?
206 when 'hierarchy', 'tree'
207 # Only users allowed to manage versions of the root project can
208 # set sharing to hierarchy or tree
209 project.nil? || user.allowed_to?(:manage_versions, project.root)
210 else
211 true
212 end
213 end
214 end
215 end
216
217 private
218
219 def load_issue_counts
220 unless @issue_count
221 @open_issues_count = 0
222 @closed_issues_count = 0
223 fixed_issues.count(:all, :group => :status).each do |status, count|
224 if status.is_closed?
225 @closed_issues_count += count
226 else
227 @open_issues_count += count
228 end
229 end
230 @issue_count = @open_issues_count + @closed_issues_count
231 end
232 end
233
234 # Update the issue's fixed versions. Used if a version's sharing changes.
235 def update_issues_from_sharing_change
236 if sharing_changed?
237 if VERSION_SHARINGS.index(sharing_was).nil? ||
238 VERSION_SHARINGS.index(sharing).nil? ||
239 VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing)
240 Issue.update_versions_from_sharing_change self
241 end
242 end
243 end
244
245 # Returns the average estimated time of assigned issues
246 # or 1 if no issue has an estimated time
247 # Used to weigth unestimated issues in progress calculation
248 def estimated_average
249 if @estimated_average.nil?
250 average = fixed_issues.average(:estimated_hours).to_f
251 if average == 0
252 average = 1
253 end
254 @estimated_average = average
255 end
256 @estimated_average
257 end
258
259 # Returns the total progress of open or closed issues. The returned percentage takes into account
260 # the amount of estimated time set for this version.
261 #
262 # Examples:
263 # issues_progress(true) => returns the progress percentage for open issues.
264 # issues_progress(false) => returns the progress percentage for closed issues.
265 def issues_progress(open)
266 @issues_progress ||= {}
267 @issues_progress[open] ||= begin
268 progress = 0
269 if issues_count > 0
270 ratio = open ? 'done_ratio' : 100
271
272 done = fixed_issues.open(open).sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}").to_f
273 progress = done / (estimated_average * issues_count)
274 end
275 progress
276 end
277 end
278
279 def validate_version
280 if effective_date.nil? && @attributes['effective_date'].present?
281 errors.add :effective_date, :not_a_date
282 end
283 end
284 end