##// END OF EJS Templates
remove trailing white space from app/models/version.rb...
Toshi MARUYAMA -
r11421:32391269ae21
parent child
Show More
@@ -1,289 +1,289
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 :effective_date, :date => true
34 34 validates_inclusion_of :status, :in => VERSION_STATUSES
35 35 validates_inclusion_of :sharing, :in => VERSION_SHARINGS
36 36
37 37 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
38 38 scope :open, lambda { where(:status => 'open') }
39 39 scope :visible, lambda {|*args|
40 40 includes(:project).where(Project.allowed_to_condition(args.first || User.current, :view_issues))
41 41 }
42 42
43 safe_attributes 'name',
43 safe_attributes 'name',
44 44 'description',
45 45 'effective_date',
46 46 'due_date',
47 47 'wiki_page_title',
48 48 'status',
49 49 'sharing',
50 50 'custom_field_values'
51 51
52 52 # Returns true if +user+ or current user is allowed to view the version
53 53 def visible?(user=User.current)
54 54 user.allowed_to?(:view_issues, self.project)
55 55 end
56 56
57 57 # Version files have same visibility as project files
58 58 def attachments_visible?(*args)
59 59 project.present? && project.attachments_visible?(*args)
60 60 end
61 61
62 62 def start_date
63 63 @start_date ||= fixed_issues.minimum('start_date')
64 64 end
65 65
66 66 def due_date
67 67 effective_date
68 68 end
69 69
70 70 def due_date=(arg)
71 71 self.effective_date=(arg)
72 72 end
73 73
74 74 # Returns the total estimated time for this version
75 75 # (sum of leaves estimated_hours)
76 76 def estimated_hours
77 77 @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f
78 78 end
79 79
80 80 # Returns the total reported time for this version
81 81 def spent_hours
82 82 @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f
83 83 end
84 84
85 85 def closed?
86 86 status == 'closed'
87 87 end
88 88
89 89 def open?
90 90 status == 'open'
91 91 end
92 92
93 93 # Returns true if the version is completed: due date reached and no open issues
94 94 def completed?
95 95 effective_date && (effective_date < Date.today) && (open_issues_count == 0)
96 96 end
97 97
98 98 def behind_schedule?
99 99 if completed_percent == 100
100 100 return false
101 101 elsif due_date && start_date
102 102 done_date = start_date + ((due_date - start_date+1)* completed_percent/100).floor
103 103 return done_date <= Date.today
104 104 else
105 105 false # No issues so it's not late
106 106 end
107 107 end
108 108
109 109 # Returns the completion percentage of this version based on the amount of open/closed issues
110 110 # and the time spent on the open issues.
111 111 def completed_percent
112 112 if issues_count == 0
113 113 0
114 114 elsif open_issues_count == 0
115 115 100
116 116 else
117 117 issues_progress(false) + issues_progress(true)
118 118 end
119 119 end
120 120
121 121 # TODO: remove in Redmine 3.0
122 122 def completed_pourcent
123 123 ActiveSupport::Deprecation.warn "Version#completed_pourcent is deprecated and will be removed in Redmine 3.0. Please use #completed_percent instead."
124 124 completed_percent
125 125 end
126 126
127 127 # Returns the percentage of issues that have been marked as 'closed'.
128 128 def closed_percent
129 129 if issues_count == 0
130 130 0
131 131 else
132 132 issues_progress(false)
133 133 end
134 134 end
135 135
136 136 # TODO: remove in Redmine 3.0
137 137 def closed_pourcent
138 138 ActiveSupport::Deprecation.warn "Version#closed_pourcent is deprecated and will be removed in Redmine 3.0. Please use #closed_percent instead."
139 139 closed_percent
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 < Date.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 200 def self.fields_for_order_statement(table=nil)
201 201 table ||= table_name
202 202 ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"]
203 203 end
204 204
205 205 scope :sorted, order(fields_for_order_statement)
206 206
207 207 # Returns the sharings that +user+ can set the version to
208 208 def allowed_sharings(user = User.current)
209 209 VERSION_SHARINGS.select do |s|
210 210 if sharing == s
211 211 true
212 212 else
213 213 case s
214 214 when 'system'
215 215 # Only admin users can set a systemwide sharing
216 216 user.admin?
217 217 when 'hierarchy', 'tree'
218 218 # Only users allowed to manage versions of the root project can
219 219 # set sharing to hierarchy or tree
220 220 project.nil? || user.allowed_to?(:manage_versions, project.root)
221 221 else
222 222 true
223 223 end
224 224 end
225 225 end
226 226 end
227 227
228 228 private
229 229
230 230 def load_issue_counts
231 231 unless @issue_count
232 232 @open_issues_count = 0
233 233 @closed_issues_count = 0
234 234 fixed_issues.count(:all, :group => :status).each do |status, count|
235 235 if status.is_closed?
236 236 @closed_issues_count += count
237 237 else
238 238 @open_issues_count += count
239 239 end
240 240 end
241 241 @issue_count = @open_issues_count + @closed_issues_count
242 242 end
243 243 end
244 244
245 245 # Update the issue's fixed versions. Used if a version's sharing changes.
246 246 def update_issues_from_sharing_change
247 247 if sharing_changed?
248 248 if VERSION_SHARINGS.index(sharing_was).nil? ||
249 249 VERSION_SHARINGS.index(sharing).nil? ||
250 250 VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing)
251 251 Issue.update_versions_from_sharing_change self
252 252 end
253 253 end
254 254 end
255 255
256 256 # Returns the average estimated time of assigned issues
257 257 # or 1 if no issue has an estimated time
258 258 # Used to weigth unestimated issues in progress calculation
259 259 def estimated_average
260 260 if @estimated_average.nil?
261 261 average = fixed_issues.average(:estimated_hours).to_f
262 262 if average == 0
263 263 average = 1
264 264 end
265 265 @estimated_average = average
266 266 end
267 267 @estimated_average
268 268 end
269 269
270 270 # Returns the total progress of open or closed issues. The returned percentage takes into account
271 271 # the amount of estimated time set for this version.
272 272 #
273 273 # Examples:
274 274 # issues_progress(true) => returns the progress percentage for open issues.
275 275 # issues_progress(false) => returns the progress percentage for closed issues.
276 276 def issues_progress(open)
277 277 @issues_progress ||= {}
278 278 @issues_progress[open] ||= begin
279 279 progress = 0
280 280 if issues_count > 0
281 281 ratio = open ? 'done_ratio' : 100
282 282
283 283 done = fixed_issues.open(open).sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}").to_f
284 284 progress = done / (estimated_average * issues_count)
285 285 end
286 286 progress
287 287 end
288 288 end
289 289 end
General Comments 0
You need to be logged in to leave comments. Login now