To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / version.rb @ 1566:ac2e4a54a6a6
History | View | Annotate | Download (8.46 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1494:e248c7af89ec | Chris | # Copyright (C) 2006-2014 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 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 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # 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 | 929:5f33065ddc4b | Chris | include Redmine::SafeAttributes |
| 20 | 0:513646585e45 | Chris | 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 | 909:cbb26bc654de | Chris | |
| 30 | 0:513646585e45 | Chris | validates_presence_of :name
|
| 31 | validates_uniqueness_of :name, :scope => [:project_id] |
||
| 32 | validates_length_of :name, :maximum => 60 |
||
| 33 | 1464:261b3d9a4903 | Chris | validates :effective_date, :date => true |
| 34 | 0:513646585e45 | Chris | validates_inclusion_of :status, :in => VERSION_STATUSES |
| 35 | validates_inclusion_of :sharing, :in => VERSION_SHARINGS |
||
| 36 | |||
| 37 | 1115:433d4f72a19b | Chris | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} |
| 38 | 1464:261b3d9a4903 | Chris | scope :open, lambda { where(:status => 'open') } |
| 39 | 1115:433d4f72a19b | Chris | scope :visible, lambda {|*args|
|
| 40 | includes(:project).where(Project.allowed_to_condition(args.first || User.current, :view_issues)) |
||
| 41 | } |
||
| 42 | 0:513646585e45 | Chris | |
| 43 | 1464:261b3d9a4903 | Chris | safe_attributes 'name',
|
| 44 | 929:5f33065ddc4b | Chris | 'description',
|
| 45 | 'effective_date',
|
||
| 46 | 'due_date',
|
||
| 47 | 'wiki_page_title',
|
||
| 48 | 'status',
|
||
| 49 | 'sharing',
|
||
| 50 | 1464:261b3d9a4903 | Chris | 'custom_field_values',
|
| 51 | 'custom_fields'
|
||
| 52 | 929:5f33065ddc4b | Chris | |
| 53 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | |
| 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 | 0:513646585e45 | Chris | def start_date |
| 64 | 119:8661b858af72 | Chris | @start_date ||= fixed_issues.minimum('start_date') |
| 65 | 0:513646585e45 | Chris | end
|
| 66 | 909:cbb26bc654de | Chris | |
| 67 | 0:513646585e45 | Chris | def due_date |
| 68 | effective_date |
||
| 69 | end
|
||
| 70 | 909:cbb26bc654de | Chris | |
| 71 | def due_date=(arg) |
||
| 72 | self.effective_date=(arg)
|
||
| 73 | end
|
||
| 74 | |||
| 75 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | |
| 81 | 0:513646585e45 | Chris | # Returns the total reported time for this version
|
| 82 | def spent_hours |
||
| 83 | 1115:433d4f72a19b | Chris | @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f |
| 84 | 0:513646585e45 | Chris | end
|
| 85 | 909:cbb26bc654de | Chris | |
| 86 | 0:513646585e45 | Chris | def closed? |
| 87 | status == 'closed'
|
||
| 88 | end
|
||
| 89 | |||
| 90 | def open? |
||
| 91 | status == 'open'
|
||
| 92 | end
|
||
| 93 | 909:cbb26bc654de | Chris | |
| 94 | 0:513646585e45 | Chris | # Returns true if the version is completed: due date reached and no open issues
|
| 95 | def completed? |
||
| 96 | 1115:433d4f72a19b | Chris | effective_date && (effective_date < Date.today) && (open_issues_count == 0) |
| 97 | 0:513646585e45 | Chris | end
|
| 98 | 22:40f7cfd4df19 | chris | |
| 99 | def behind_schedule? |
||
| 100 | 1464:261b3d9a4903 | Chris | if completed_percent == 100 |
| 101 | 22:40f7cfd4df19 | chris | return false |
| 102 | 119:8661b858af72 | Chris | elsif due_date && start_date
|
| 103 | 1464:261b3d9a4903 | Chris | done_date = start_date + ((due_date - start_date+1)* completed_percent/100).floor |
| 104 | 22:40f7cfd4df19 | chris | return done_date <= Date.today |
| 105 | else
|
||
| 106 | false # No issues so it's not late |
||
| 107 | end
|
||
| 108 | end
|
||
| 109 | 909:cbb26bc654de | Chris | |
| 110 | 0:513646585e45 | Chris | # 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 | 1464:261b3d9a4903 | Chris | def completed_percent |
| 113 | 0:513646585e45 | Chris | 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 | 909:cbb26bc654de | Chris | |
| 122 | 1464:261b3d9a4903 | Chris | # TODO: remove in Redmine 3.0
|
| 123 | def completed_pourcent |
||
| 124 | ActiveSupport::Deprecation.warn "Version#completed_pourcent is deprecated and will be removed in Redmine 3.0. Please use #completed_percent instead." |
||
| 125 | completed_percent |
||
| 126 | end
|
||
| 127 | |||
| 128 | 0:513646585e45 | Chris | # Returns the percentage of issues that have been marked as 'closed'.
|
| 129 | 1464:261b3d9a4903 | Chris | def closed_percent |
| 130 | 0:513646585e45 | Chris | if issues_count == 0 |
| 131 | 0
|
||
| 132 | else
|
||
| 133 | issues_progress(false)
|
||
| 134 | end
|
||
| 135 | end
|
||
| 136 | 909:cbb26bc654de | Chris | |
| 137 | 1464:261b3d9a4903 | Chris | # TODO: remove in Redmine 3.0
|
| 138 | def closed_pourcent |
||
| 139 | ActiveSupport::Deprecation.warn "Version#closed_pourcent is deprecated and will be removed in Redmine 3.0. Please use #closed_percent instead." |
||
| 140 | closed_percent |
||
| 141 | end
|
||
| 142 | |||
| 143 | 0:513646585e45 | Chris | # Returns true if the version is overdue: due date reached and some open issues
|
| 144 | def overdue? |
||
| 145 | effective_date && (effective_date < Date.today) && (open_issues_count > 0) |
||
| 146 | end
|
||
| 147 | 909:cbb26bc654de | Chris | |
| 148 | 0:513646585e45 | Chris | # Returns assigned issues count
|
| 149 | def issues_count |
||
| 150 | 1115:433d4f72a19b | Chris | load_issue_counts |
| 151 | @issue_count
|
||
| 152 | 0:513646585e45 | Chris | end
|
| 153 | 909:cbb26bc654de | Chris | |
| 154 | 0:513646585e45 | Chris | # Returns the total amount of open issues for this version.
|
| 155 | def open_issues_count |
||
| 156 | 1115:433d4f72a19b | Chris | load_issue_counts |
| 157 | @open_issues_count
|
||
| 158 | 0:513646585e45 | Chris | end
|
| 159 | |||
| 160 | # Returns the total amount of closed issues for this version.
|
||
| 161 | def closed_issues_count |
||
| 162 | 1115:433d4f72a19b | Chris | load_issue_counts |
| 163 | @closed_issues_count
|
||
| 164 | 0:513646585e45 | Chris | end
|
| 165 | 909:cbb26bc654de | Chris | |
| 166 | 0:513646585e45 | Chris | def wiki_page |
| 167 | if project.wiki && !wiki_page_title.blank?
|
||
| 168 | @wiki_page ||= project.wiki.find_page(wiki_page_title)
|
||
| 169 | end
|
||
| 170 | @wiki_page
|
||
| 171 | end
|
||
| 172 | 909:cbb26bc654de | Chris | |
| 173 | 0:513646585e45 | Chris | def to_s; name end |
| 174 | 22:40f7cfd4df19 | chris | |
| 175 | def to_s_with_project |
||
| 176 | "#{project} - #{name}"
|
||
| 177 | end
|
||
| 178 | 909:cbb26bc654de | Chris | |
| 179 | 1115:433d4f72a19b | Chris | # Versions are sorted by effective_date and name
|
| 180 | # Those with no effective_date are at the end, sorted by name
|
||
| 181 | 0:513646585e45 | Chris | def <=>(version) |
| 182 | if self.effective_date |
||
| 183 | if version.effective_date
|
||
| 184 | if self.effective_date == version.effective_date |
||
| 185 | 1115:433d4f72a19b | Chris | name == version.name ? id <=> version.id : name <=> version.name |
| 186 | 0:513646585e45 | Chris | else
|
| 187 | self.effective_date <=> version.effective_date
|
||
| 188 | end
|
||
| 189 | else
|
||
| 190 | -1
|
||
| 191 | end
|
||
| 192 | else
|
||
| 193 | if version.effective_date
|
||
| 194 | 1
|
||
| 195 | else
|
||
| 196 | 1115:433d4f72a19b | Chris | name == version.name ? id <=> version.id : name <=> version.name |
| 197 | 0:513646585e45 | Chris | end
|
| 198 | end
|
||
| 199 | end
|
||
| 200 | 909:cbb26bc654de | Chris | |
| 201 | 1115:433d4f72a19b | Chris | def self.fields_for_order_statement(table=nil) |
| 202 | table ||= table_name |
||
| 203 | ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"] |
||
| 204 | end
|
||
| 205 | |||
| 206 | 1517:dffacf8a6908 | Chris | scope :sorted, lambda { order(fields_for_order_statement) }
|
| 207 | 1115:433d4f72a19b | Chris | |
| 208 | 0:513646585e45 | Chris | # Returns the sharings that +user+ can set the version to
|
| 209 | def allowed_sharings(user = User.current) |
||
| 210 | VERSION_SHARINGS.select do |s| |
||
| 211 | if sharing == s
|
||
| 212 | true
|
||
| 213 | else
|
||
| 214 | case s
|
||
| 215 | when 'system' |
||
| 216 | # Only admin users can set a systemwide sharing
|
||
| 217 | user.admin? |
||
| 218 | when 'hierarchy', 'tree' |
||
| 219 | # Only users allowed to manage versions of the root project can
|
||
| 220 | # set sharing to hierarchy or tree
|
||
| 221 | project.nil? || user.allowed_to?(:manage_versions, project.root)
|
||
| 222 | else
|
||
| 223 | true
|
||
| 224 | end
|
||
| 225 | end
|
||
| 226 | end
|
||
| 227 | end
|
||
| 228 | 909:cbb26bc654de | Chris | |
| 229 | 0:513646585e45 | Chris | private |
| 230 | |||
| 231 | 1115:433d4f72a19b | Chris | def load_issue_counts |
| 232 | unless @issue_count |
||
| 233 | @open_issues_count = 0 |
||
| 234 | @closed_issues_count = 0 |
||
| 235 | 1517:dffacf8a6908 | Chris | fixed_issues.group(:status).count.each do |status, count| |
| 236 | 1115:433d4f72a19b | Chris | if status.is_closed?
|
| 237 | @closed_issues_count += count
|
||
| 238 | else
|
||
| 239 | @open_issues_count += count
|
||
| 240 | end
|
||
| 241 | end
|
||
| 242 | @issue_count = @open_issues_count + @closed_issues_count |
||
| 243 | end
|
||
| 244 | end
|
||
| 245 | |||
| 246 | 0:513646585e45 | Chris | # Update the issue's fixed versions. Used if a version's sharing changes.
|
| 247 | def update_issues_from_sharing_change |
||
| 248 | if sharing_changed?
|
||
| 249 | if VERSION_SHARINGS.index(sharing_was).nil? || |
||
| 250 | VERSION_SHARINGS.index(sharing).nil? ||
|
||
| 251 | VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing) |
||
| 252 | Issue.update_versions_from_sharing_change self |
||
| 253 | end
|
||
| 254 | end
|
||
| 255 | end
|
||
| 256 | 909:cbb26bc654de | Chris | |
| 257 | 0:513646585e45 | Chris | # Returns the average estimated time of assigned issues
|
| 258 | # or 1 if no issue has an estimated time
|
||
| 259 | # Used to weigth unestimated issues in progress calculation
|
||
| 260 | def estimated_average |
||
| 261 | if @estimated_average.nil? |
||
| 262 | average = fixed_issues.average(:estimated_hours).to_f
|
||
| 263 | if average == 0 |
||
| 264 | average = 1
|
||
| 265 | end
|
||
| 266 | @estimated_average = average
|
||
| 267 | end
|
||
| 268 | @estimated_average
|
||
| 269 | end
|
||
| 270 | 909:cbb26bc654de | Chris | |
| 271 | 0:513646585e45 | Chris | # Returns the total progress of open or closed issues. The returned percentage takes into account
|
| 272 | # the amount of estimated time set for this version.
|
||
| 273 | #
|
||
| 274 | # Examples:
|
||
| 275 | # issues_progress(true) => returns the progress percentage for open issues.
|
||
| 276 | # issues_progress(false) => returns the progress percentage for closed issues.
|
||
| 277 | def issues_progress(open) |
||
| 278 | @issues_progress ||= {}
|
||
| 279 | @issues_progress[open] ||= begin |
||
| 280 | progress = 0
|
||
| 281 | if issues_count > 0 |
||
| 282 | ratio = open ? 'done_ratio' : 100 |
||
| 283 | 909:cbb26bc654de | Chris | |
| 284 | 1115:433d4f72a19b | Chris | done = fixed_issues.open(open).sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}").to_f
|
| 285 | 0:513646585e45 | Chris | progress = done / (estimated_average * issues_count) |
| 286 | end
|
||
| 287 | progress |
||
| 288 | end
|
||
| 289 | end
|
||
| 290 | end |