annotate .svn/pristine/70/70b7f0d80dc51dc76954b69ac02e51134a5172c0.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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