annotate .svn/pristine/75/753c25cc403b95007aa051a7ebe5a044110d3fc0.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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