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 @ 912:5e80956cc792
History | View | Annotate | Download (7.71 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 909:cbb26bc654de | Chris | # Copyright (C) 2006-2011 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 | after_update :update_issues_from_sharing_change
|
||
| 20 | belongs_to :project
|
||
| 21 | has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify |
||
| 22 | acts_as_customizable |
||
| 23 | acts_as_attachable :view_permission => :view_files, |
||
| 24 | :delete_permission => :manage_files |
||
| 25 | |||
| 26 | VERSION_STATUSES = %w(open locked closed) |
||
| 27 | VERSION_SHARINGS = %w(none descendants hierarchy tree system) |
||
| 28 | 909:cbb26bc654de | Chris | |
| 29 | 0:513646585e45 | Chris | validates_presence_of :name
|
| 30 | validates_uniqueness_of :name, :scope => [:project_id] |
||
| 31 | validates_length_of :name, :maximum => 60 |
||
| 32 | validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true |
||
| 33 | validates_inclusion_of :status, :in => VERSION_STATUSES |
||
| 34 | validates_inclusion_of :sharing, :in => VERSION_SHARINGS |
||
| 35 | |||
| 36 | 507:0c939c159af4 | Chris | named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}} |
| 37 | 0:513646585e45 | Chris | named_scope :open, :conditions => {:status => 'open'} |
| 38 | named_scope :visible, lambda {|*args| { :include => :project, |
||
| 39 | :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } } |
||
| 40 | |||
| 41 | # Returns true if +user+ or current user is allowed to view the version
|
||
| 42 | def visible?(user=User.current) |
||
| 43 | user.allowed_to?(:view_issues, self.project) |
||
| 44 | end
|
||
| 45 | 909:cbb26bc654de | Chris | |
| 46 | # Version files have same visibility as project files
|
||
| 47 | def attachments_visible?(*args) |
||
| 48 | project.present? && project.attachments_visible?(*args) |
||
| 49 | end
|
||
| 50 | |||
| 51 | 0:513646585e45 | Chris | def start_date |
| 52 | 119:8661b858af72 | Chris | @start_date ||= fixed_issues.minimum('start_date') |
| 53 | 0:513646585e45 | Chris | end
|
| 54 | 909:cbb26bc654de | Chris | |
| 55 | 0:513646585e45 | Chris | def due_date |
| 56 | effective_date |
||
| 57 | end
|
||
| 58 | 909:cbb26bc654de | Chris | |
| 59 | def due_date=(arg) |
||
| 60 | self.effective_date=(arg)
|
||
| 61 | end
|
||
| 62 | |||
| 63 | 0:513646585e45 | Chris | # Returns the total estimated time for this version
|
| 64 | # (sum of leaves estimated_hours)
|
||
| 65 | def estimated_hours |
||
| 66 | @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f |
||
| 67 | end
|
||
| 68 | 909:cbb26bc654de | Chris | |
| 69 | 0:513646585e45 | Chris | # Returns the total reported time for this version
|
| 70 | def spent_hours |
||
| 71 | @spent_hours ||= TimeEntry.sum(:hours, :include => :issue, :conditions => ["#{Issue.table_name}.fixed_version_id = ?", id]).to_f |
||
| 72 | end
|
||
| 73 | 909:cbb26bc654de | Chris | |
| 74 | 0:513646585e45 | Chris | def closed? |
| 75 | status == 'closed'
|
||
| 76 | end
|
||
| 77 | |||
| 78 | def open? |
||
| 79 | status == 'open'
|
||
| 80 | end
|
||
| 81 | 909:cbb26bc654de | Chris | |
| 82 | 0:513646585e45 | Chris | # Returns true if the version is completed: due date reached and no open issues
|
| 83 | def completed? |
||
| 84 | effective_date && (effective_date <= Date.today) && (open_issues_count == 0) |
||
| 85 | end
|
||
| 86 | 22:40f7cfd4df19 | chris | |
| 87 | def behind_schedule? |
||
| 88 | if completed_pourcent == 100 |
||
| 89 | return false |
||
| 90 | 119:8661b858af72 | Chris | elsif due_date && start_date
|
| 91 | 22:40f7cfd4df19 | chris | done_date = start_date + ((due_date - start_date+1)* completed_pourcent/100).floor |
| 92 | return done_date <= Date.today |
||
| 93 | else
|
||
| 94 | false # No issues so it's not late |
||
| 95 | end
|
||
| 96 | end
|
||
| 97 | 909:cbb26bc654de | Chris | |
| 98 | 0:513646585e45 | Chris | # Returns the completion percentage of this version based on the amount of open/closed issues
|
| 99 | # and the time spent on the open issues.
|
||
| 100 | def completed_pourcent |
||
| 101 | if issues_count == 0 |
||
| 102 | 0
|
||
| 103 | elsif open_issues_count == 0 |
||
| 104 | 100
|
||
| 105 | else
|
||
| 106 | issues_progress(false) + issues_progress(true) |
||
| 107 | end
|
||
| 108 | end
|
||
| 109 | 909:cbb26bc654de | Chris | |
| 110 | 0:513646585e45 | Chris | # Returns the percentage of issues that have been marked as 'closed'.
|
| 111 | def closed_pourcent |
||
| 112 | if issues_count == 0 |
||
| 113 | 0
|
||
| 114 | else
|
||
| 115 | issues_progress(false)
|
||
| 116 | end
|
||
| 117 | end
|
||
| 118 | 909:cbb26bc654de | Chris | |
| 119 | 0:513646585e45 | Chris | # Returns true if the version is overdue: due date reached and some open issues
|
| 120 | def overdue? |
||
| 121 | effective_date && (effective_date < Date.today) && (open_issues_count > 0) |
||
| 122 | end
|
||
| 123 | 909:cbb26bc654de | Chris | |
| 124 | 0:513646585e45 | Chris | # Returns assigned issues count
|
| 125 | def issues_count |
||
| 126 | @issue_count ||= fixed_issues.count
|
||
| 127 | end
|
||
| 128 | 909:cbb26bc654de | Chris | |
| 129 | 0:513646585e45 | Chris | # Returns the total amount of open issues for this version.
|
| 130 | def open_issues_count |
||
| 131 | @open_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, false], :include => :status) |
||
| 132 | end
|
||
| 133 | |||
| 134 | # Returns the total amount of closed issues for this version.
|
||
| 135 | def closed_issues_count |
||
| 136 | @closed_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, true], :include => :status) |
||
| 137 | end
|
||
| 138 | 909:cbb26bc654de | Chris | |
| 139 | 0:513646585e45 | Chris | def wiki_page |
| 140 | if project.wiki && !wiki_page_title.blank?
|
||
| 141 | @wiki_page ||= project.wiki.find_page(wiki_page_title)
|
||
| 142 | end
|
||
| 143 | @wiki_page
|
||
| 144 | end
|
||
| 145 | 909:cbb26bc654de | Chris | |
| 146 | 0:513646585e45 | Chris | def to_s; name end |
| 147 | 22:40f7cfd4df19 | chris | |
| 148 | def to_s_with_project |
||
| 149 | "#{project} - #{name}"
|
||
| 150 | end
|
||
| 151 | 909:cbb26bc654de | Chris | |
| 152 | 0:513646585e45 | Chris | # Versions are sorted by effective_date and "Project Name - Version name"
|
| 153 | # Those with no effective_date are at the end, sorted by "Project Name - Version name"
|
||
| 154 | def <=>(version) |
||
| 155 | if self.effective_date |
||
| 156 | if version.effective_date
|
||
| 157 | if self.effective_date == version.effective_date |
||
| 158 | "#{self.project.name} - #{self.name}" <=> "#{version.project.name} - #{version.name}" |
||
| 159 | else
|
||
| 160 | self.effective_date <=> version.effective_date
|
||
| 161 | end
|
||
| 162 | else
|
||
| 163 | -1
|
||
| 164 | end
|
||
| 165 | else
|
||
| 166 | if version.effective_date
|
||
| 167 | 1
|
||
| 168 | else
|
||
| 169 | "#{self.project.name} - #{self.name}" <=> "#{version.project.name} - #{version.name}" |
||
| 170 | end
|
||
| 171 | end
|
||
| 172 | end
|
||
| 173 | 909:cbb26bc654de | Chris | |
| 174 | 0:513646585e45 | Chris | # Returns the sharings that +user+ can set the version to
|
| 175 | def allowed_sharings(user = User.current) |
||
| 176 | VERSION_SHARINGS.select do |s| |
||
| 177 | if sharing == s
|
||
| 178 | true
|
||
| 179 | else
|
||
| 180 | case s
|
||
| 181 | when 'system' |
||
| 182 | # Only admin users can set a systemwide sharing
|
||
| 183 | user.admin? |
||
| 184 | when 'hierarchy', 'tree' |
||
| 185 | # Only users allowed to manage versions of the root project can
|
||
| 186 | # set sharing to hierarchy or tree
|
||
| 187 | project.nil? || user.allowed_to?(:manage_versions, project.root)
|
||
| 188 | else
|
||
| 189 | true
|
||
| 190 | end
|
||
| 191 | end
|
||
| 192 | end
|
||
| 193 | end
|
||
| 194 | 909:cbb26bc654de | Chris | |
| 195 | 0:513646585e45 | Chris | private |
| 196 | |||
| 197 | # Update the issue's fixed versions. Used if a version's sharing changes.
|
||
| 198 | def update_issues_from_sharing_change |
||
| 199 | if sharing_changed?
|
||
| 200 | if VERSION_SHARINGS.index(sharing_was).nil? || |
||
| 201 | VERSION_SHARINGS.index(sharing).nil? ||
|
||
| 202 | VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing) |
||
| 203 | Issue.update_versions_from_sharing_change self |
||
| 204 | end
|
||
| 205 | end
|
||
| 206 | end
|
||
| 207 | 909:cbb26bc654de | Chris | |
| 208 | 0:513646585e45 | Chris | # Returns the average estimated time of assigned issues
|
| 209 | # or 1 if no issue has an estimated time
|
||
| 210 | # Used to weigth unestimated issues in progress calculation
|
||
| 211 | def estimated_average |
||
| 212 | if @estimated_average.nil? |
||
| 213 | average = fixed_issues.average(:estimated_hours).to_f
|
||
| 214 | if average == 0 |
||
| 215 | average = 1
|
||
| 216 | end
|
||
| 217 | @estimated_average = average
|
||
| 218 | end
|
||
| 219 | @estimated_average
|
||
| 220 | end
|
||
| 221 | 909:cbb26bc654de | Chris | |
| 222 | 0:513646585e45 | Chris | # Returns the total progress of open or closed issues. The returned percentage takes into account
|
| 223 | # the amount of estimated time set for this version.
|
||
| 224 | #
|
||
| 225 | # Examples:
|
||
| 226 | # issues_progress(true) => returns the progress percentage for open issues.
|
||
| 227 | # issues_progress(false) => returns the progress percentage for closed issues.
|
||
| 228 | def issues_progress(open) |
||
| 229 | @issues_progress ||= {}
|
||
| 230 | @issues_progress[open] ||= begin |
||
| 231 | progress = 0
|
||
| 232 | if issues_count > 0 |
||
| 233 | ratio = open ? 'done_ratio' : 100 |
||
| 234 | 909:cbb26bc654de | Chris | |
| 235 | 0:513646585e45 | Chris | done = fixed_issues.sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}",
|
| 236 | :include => :status, |
||
| 237 | :conditions => ["is_closed = ?", !open]).to_f |
||
| 238 | progress = done / (estimated_average * issues_count) |
||
| 239 | end
|
||
| 240 | progress |
||
| 241 | end
|
||
| 242 | end
|
||
| 243 | end |