annotate app/models/version.rb @ 1478:5ca1f4a47171 bibplugin_db_migrations

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