Class: Version

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/version.rb

Overview

Redmine - project management software Copyright © 2006-2010 Jean-Philippe Lang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Constant Summary collapse

VERSION_STATUSES =
%w(open locked closed)
VERSION_SHARINGS =
%w(none descendants hierarchy tree system)

Instance Method Summary collapse

Instance Method Details

#<=>(version) ⇒ Object

Versions are sorted by effective_date and “Project Name - Version name” Those with no effective_date are at the end, sorted by “Project Name - Version name”



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/version.rb', line 144

def <=>(version)
  if self.effective_date
    if version.effective_date
      if self.effective_date == version.effective_date
        "#{self.project.name} - #{self.name}" <=> "#{version.project.name} - #{version.name}"
      else
        self.effective_date <=> version.effective_date
      end
    else
      -1
    end
  else
    if version.effective_date
      1
    else
      "#{self.project.name} - #{self.name}" <=> "#{version.project.name} - #{version.name}"
    end
  end
end

#allowed_sharings(user = User.current) ⇒ Object

Returns the sharings that user can set the version to



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/version.rb', line 165

def allowed_sharings(user = User.current)
  VERSION_SHARINGS.select do |s|
    if sharing == s
      true
    else
      case s
      when 'system'
        # Only admin users can set a systemwide sharing
        user.admin?
      when 'hierarchy', 'tree'
        # Only users allowed to manage versions of the root project can
        # set sharing to hierarchy or tree
        project.nil? || user.allowed_to?(:manage_versions, project.root)
      else
        true
      end
    end
  end
end

#behind_schedule?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
# File 'app/models/version.rb', line 77

def behind_schedule?
  if completed_pourcent == 100
    return false
  elsif due_date && start_date
    done_date = start_date + ((due_date - start_date+1)* completed_pourcent/100).floor
    return done_date <= Date.today
  else
    false # No issues so it's not late
  end
end

#closed?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/version.rb', line 64

def closed?
  status == 'closed'
end

#closed_issues_countObject

Returns the total amount of closed issues for this version.



125
126
127
# File 'app/models/version.rb', line 125

def closed_issues_count
  @closed_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, true], :include => :status)
end

#closed_pourcentObject

Returns the percentage of issues that have been marked as ‘closed’.



101
102
103
104
105
106
107
# File 'app/models/version.rb', line 101

def closed_pourcent
  if issues_count == 0
    0
  else
    issues_progress(false)
  end
end

#completed?Boolean

Returns true if the version is completed: due date reached and no open issues

Returns:

  • (Boolean)


73
74
75
# File 'app/models/version.rb', line 73

def completed?
  effective_date && (effective_date <= Date.today) && (open_issues_count == 0)
end

#completed_pourcentObject

Returns the completion percentage of this version based on the amount of open/closed issues and the time spent on the open issues.



90
91
92
93
94
95
96
97
98
# File 'app/models/version.rb', line 90

def completed_pourcent
  if issues_count == 0
    0
  elsif open_issues_count == 0
    100
  else
    issues_progress(false) + issues_progress(true)
  end
end

#due_dateObject



49
50
51
# File 'app/models/version.rb', line 49

def due_date
  effective_date
end

#estimated_hoursObject

Returns the total estimated time for this version (sum of leaves estimated_hours)



55
56
57
# File 'app/models/version.rb', line 55

def estimated_hours
  @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f
end

#issues_countObject

Returns assigned issues count



115
116
117
# File 'app/models/version.rb', line 115

def issues_count
  @issue_count ||= fixed_issues.count
end

#open?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/version.rb', line 68

def open?
  status == 'open'
end

#open_issues_countObject

Returns the total amount of open issues for this version.



120
121
122
# File 'app/models/version.rb', line 120

def open_issues_count
  @open_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, false], :include => :status)
end

#overdue?Boolean

Returns true if the version is overdue: due date reached and some open issues

Returns:

  • (Boolean)


110
111
112
# File 'app/models/version.rb', line 110

def overdue?
  effective_date && (effective_date < Date.today) && (open_issues_count > 0)
end

#spent_hoursObject

Returns the total reported time for this version



60
61
62
# File 'app/models/version.rb', line 60

def spent_hours
  @spent_hours ||= TimeEntry.sum(:hours, :include => :issue, :conditions => ["#{Issue.table_name}.fixed_version_id = ?", id]).to_f
end

#start_dateObject



45
46
47
# File 'app/models/version.rb', line 45

def start_date
  @start_date ||= fixed_issues.minimum('start_date')
end

#to_sObject



136
# File 'app/models/version.rb', line 136

def to_s; name end

#to_s_with_projectObject



138
139
140
# File 'app/models/version.rb', line 138

def to_s_with_project
  "#{project} - #{name}"
end

#visible?(user = User.current) ⇒ Boolean

Returns true if user or current user is allowed to view the version

Returns:

  • (Boolean)


41
42
43
# File 'app/models/version.rb', line 41

def visible?(user=User.current)
  user.allowed_to?(:view_issues, self.project)
end

#wiki_pageObject



129
130
131
132
133
134
# File 'app/models/version.rb', line 129

def wiki_page
  if project.wiki && !wiki_page_title.blank?
    @wiki_page ||= project.wiki.find_page(wiki_page_title)
  end
  @wiki_page
end