Class: Version

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

Overview

Redmine - project management software Copyright (C) 2006-2017 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.

Since:

  • 0.4.0

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redmine::SafeAttributes

#delete_unsafe_attributes, #safe_attribute?, #safe_attribute_names, #safe_attributes=

Class Method Details

.fields_for_order_statement(table = nil) ⇒ Object

Since:

  • 2.1.0



237
238
239
240
# File 'app/models/version.rb', line 237

def self.fields_for_order_statement(table=nil)
  table ||= table_name
  ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"]
end

.sort_by_status(versions) ⇒ Object

Sort versions by status (open, locked then closed versions)

Since:

  • 3.4.0



220
221
222
223
224
225
226
227
228
# File 'app/models/version.rb', line 220

def self.sort_by_status(versions)
  versions.sort do |a, b|
    if a.status == b.status
      a <=> b
    else
      b.status <=> a.status
    end
  end
end

Instance Method Details

#<=>(version) ⇒ Object

Versions are sorted by effective_date and name Those with no effective_date are at the end, sorted by name

Since:

  • 0.5.1



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/models/version.rb', line 199

def <=>(version)
  if self.effective_date
    if version.effective_date
      if self.effective_date == version.effective_date
        name == version.name ? id <=> version.id : name <=> version.name
      else
        self.effective_date <=> version.effective_date
      end
    else
      -1
    end
  else
    if version.effective_date
      1
    else
      name == version.name ? id <=> version.id : name <=> version.name
    end
  end
end

#allowed_sharings(user = User.current) ⇒ Object

Returns the sharings that user can set the version to

Since:

  • 0.9.0



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'app/models/version.rb', line 245

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

#attachments_deletable?(usr = User.current) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 2.5.3



83
84
85
# File 'app/models/version.rb', line 83

def attachments_deletable?(usr=User.current)
  project.present? && project.attachments_deletable?(usr)
end

#attachments_visible?(*args) ⇒ Boolean

Version files have same visibility as project files

Returns:

  • (Boolean)

Since:

  • 1.2.3



79
80
81
# File 'app/models/version.rb', line 79

def attachments_visible?(*args)
  project.present? && project.attachments_visible?(*args)
end

#base_reloadObject

Since:

  • 3.4.0



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

alias :base_reload :reload

#behind_schedule?Boolean

Returns:

  • (Boolean)

Since:

  • 1.1.0



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

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

#closed?Boolean

Returns:

  • (Boolean)

Since:

  • 0.9.0



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

def closed?
  status == 'closed'
end

#closed_issues_countObject

Returns the total amount of closed issues for this version.

Since:

  • 0.6.0



179
180
181
182
# File 'app/models/version.rb', line 179

def closed_issues_count
  load_issue_counts
  @closed_issues_count
end

#closed_percentObject

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

Since:

  • 2.3.0



153
154
155
156
157
158
159
# File 'app/models/version.rb', line 153

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

#completed?Boolean

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

Returns:

  • (Boolean)

Since:

  • 0.5.1



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

def completed?
  closed? || (effective_date && (effective_date < User.current.today) && (open_issues_count == 0))
end

#completed_percentObject

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

Since:

  • 2.3.0



142
143
144
145
146
147
148
149
150
# File 'app/models/version.rb', line 142

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

#css_classesObject

Since:

  • 3.3.0



230
231
232
233
234
235
# File 'app/models/version.rb', line 230

def css_classes
  [
    completed? ? 'version-completed' : 'version-incompleted',
    "version-#{status}"
  ].join(' ')
end

#default_project_versionObject

Since:

  • 3.4.0



274
275
276
277
278
279
280
# File 'app/models/version.rb', line 274

def default_project_version
  if @default_project_version.nil?
    project.present? && project.default_version == self
  else
    @default_project_version
  end
end

#default_project_version=(arg) ⇒ Object

Since:

  • 3.4.0



282
283
284
# File 'app/models/version.rb', line 282

def default_project_version=(arg)
  @default_project_version = (arg == '1' || arg == true)
end

#deletable?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.0



270
271
272
# File 'app/models/version.rb', line 270

def deletable?
  fixed_issues.empty? && !referenced_by_a_custom_field?
end

#due_dateObject

Since:

  • 0.5.0



97
98
99
# File 'app/models/version.rb', line 97

def due_date
  effective_date
end

#due_date=(arg) ⇒ Object

Since:

  • 1.3.1



101
102
103
# File 'app/models/version.rb', line 101

def due_date=(arg)
  self.effective_date=(arg)
end

#estimated_hoursObject

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

Since:

  • 0.7.0



107
108
109
# File 'app/models/version.rb', line 107

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

#issues_countObject

Returns assigned issues count

Since:

  • 0.9.0



167
168
169
170
# File 'app/models/version.rb', line 167

def issues_count
  load_issue_counts
  @issue_count
end

#open?Boolean

Returns:

  • (Boolean)

Since:

  • 0.9.0



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

def open?
  status == 'open'
end

#open_issues_countObject

Returns the total amount of open issues for this version.

Since:

  • 0.6.0



173
174
175
176
# File 'app/models/version.rb', line 173

def open_issues_count
  load_issue_counts
  @open_issues_count
end

#overdue?Boolean

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

Returns:

  • (Boolean)

Since:

  • 0.6.0



162
163
164
# File 'app/models/version.rb', line 162

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

#reload(*args) ⇒ Object

Since:

  • 3.4.0



88
89
90
91
# File 'app/models/version.rb', line 88

def reload(*args)
  @default_project_version = nil
  base_reload(*args)
end

#shared?Boolean

Returns true if the version is shared, otherwise false

Returns:

  • (Boolean)

Since:

  • 2.6.0



266
267
268
# File 'app/models/version.rb', line 266

def shared?
  sharing != 'none'
end

#spent_hoursObject

Returns the total reported time for this version

Since:

  • 0.7.0



112
113
114
# File 'app/models/version.rb', line 112

def spent_hours
  @spent_hours ||= TimeEntry.joins(:issue).where("#{Issue.table_name}.fixed_version_id = ?", id).sum(:hours).to_f
end

#start_dateObject

Since:

  • 0.5.0



93
94
95
# File 'app/models/version.rb', line 93

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

#to_sObject

Since:

  • 0.6.0



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

def to_s; name end

#to_s_with_projectObject

Since:

  • 1.1.0



193
194
195
# File 'app/models/version.rb', line 193

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)

Since:

  • 0.9.0



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

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

#wiki_pageObject

Since:

  • 0.5.1



184
185
186
187
188
189
# File 'app/models/version.rb', line 184

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