Class: Version

Inherits:
ApplicationRecord show all
Includes:
Redmine::SafeAttributes
Defined in:
app/models/version.rb

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, included, #safe_attribute?, #safe_attribute_names

Methods inherited from ApplicationRecord

human_attribute_name

Class Method Details

.fields_for_order_statement(table = nil) ⇒ Object



369
370
371
372
# File 'app/models/version.rb', line 369

def self.fields_for_order_statement(table=nil)
  table ||= table_name
  [Arel.sql("(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)



352
353
354
355
356
357
358
359
360
# File 'app/models/version.rb', line 352

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



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'app/models/version.rb', line 329

def <=>(version)
  return nil unless version.is_a?(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



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'app/models/version.rb', line 377

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)


218
219
220
# File 'app/models/version.rb', line 218

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)


214
215
216
# File 'app/models/version.rb', line 214

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

#base_reloadObject



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

alias :base_reload :reload

#behind_schedule?Boolean

Returns:

  • (Boolean)


271
272
273
274
275
276
277
# File 'app/models/version.rb', line 271

def behind_schedule?
  # Blank due date, no issues, or 100% completed, so it's not late
  return false if due_date.nil? || start_date.nil? || completed_percent == 100

  done_date = start_date + ((due_date - start_date + 1) * completed_percent / 100).floor
  done_date <= User.current.today
end

#closed?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'app/models/version.rb', line 258

def closed?
  status == 'closed'
end

#closed_issues_countObject

Returns the total amount of closed issues for this version.



306
307
308
# File 'app/models/version.rb', line 306

def closed_issues_count
  fixed_issues.closed_count
end

#closed_percentObject

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



286
287
288
# File 'app/models/version.rb', line 286

def closed_percent
  fixed_issues.closed_percent
end

#completed?Boolean

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

Returns:

  • (Boolean)


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

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.



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

def completed_percent
  fixed_issues.completed_percent
end

#css_classesObject



362
363
364
365
366
367
# File 'app/models/version.rb', line 362

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

#default_project_versionObject



406
407
408
409
410
411
412
# File 'app/models/version.rb', line 406

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



414
415
416
# File 'app/models/version.rb', line 414

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

#deletable?Boolean

Returns:

  • (Boolean)


402
403
404
# File 'app/models/version.rb', line 402

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

#due_dateObject



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

def due_date
  effective_date
end

#due_date=(arg) ⇒ Object



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

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

#editable_custom_field_values(user = nil) ⇒ Object

Returns the custom_field_values that can be edited by the given user



202
203
204
# File 'app/models/version.rb', line 202

def editable_custom_field_values(user=nil)
  visible_custom_field_values(user)
end

#estimated_hoursObject

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



243
244
245
# File 'app/models/version.rb', line 243

def estimated_hours
  fixed_issues.estimated_hours
end

#estimated_remaining_hoursObject

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



249
250
251
# File 'app/models/version.rb', line 249

def estimated_remaining_hours
  @estimated_remaining_hours ||= fixed_issues.estimated_remaining_hours
end

#issues_countObject

Returns assigned issues count



296
297
298
# File 'app/models/version.rb', line 296

def issues_count
  fixed_issues.count
end

#open?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'app/models/version.rb', line 262

def open?
  status == 'open'
end

#open_issues_countObject

Returns the total amount of open issues for this version.



301
302
303
# File 'app/models/version.rb', line 301

def open_issues_count
  fixed_issues.open_count
end

#overdue?Boolean

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

Returns:

  • (Boolean)


291
292
293
# File 'app/models/version.rb', line 291

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

#reload(*args) ⇒ Object



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

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

#safe_attributes=(attrs, user = User.current) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/version.rb', line 174

def safe_attributes=(attrs, user=User.current)
  if attrs.respond_to?(:to_unsafe_hash)
    attrs = attrs.to_unsafe_hash
  end
  return unless attrs.is_a?(Hash)

  attrs = attrs.deep_dup
  # Reject custom fields values not visible by the user
  if attrs['custom_field_values'].present?
    editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s}
    attrs['custom_field_values'].reject! {|k, v| !editable_custom_field_ids.include?(k.to_s)}
  end

  # Reject custom fields not visible by the user
  if attrs['custom_fields'].present?
    editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s}
    attrs['custom_fields'].reject! {|c| !editable_custom_field_ids.include?(c['id'].to_s)}
  end

  super
end

#shared?Boolean

Returns true if the version is shared, otherwise false

Returns:

  • (Boolean)


398
399
400
# File 'app/models/version.rb', line 398

def shared?
  sharing != 'none'
end

#spent_hoursObject

Returns the total reported time for this version



254
255
256
# File 'app/models/version.rb', line 254

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

#start_dateObject



229
230
231
# File 'app/models/version.rb', line 229

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

#to_sObject



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

def to_s; name end

#to_s_with_projectObject



323
324
325
# File 'app/models/version.rb', line 323

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)


197
198
199
# File 'app/models/version.rb', line 197

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

#visible_custom_field_values(user = nil) ⇒ Object



206
207
208
209
210
211
# File 'app/models/version.rb', line 206

def visible_custom_field_values(user = nil)
  user ||= User.current
  custom_field_values.select do |value|
    value.custom_field.visible_by?(project, user)
  end
end

#visible_fixed_issuesObject



310
311
312
# File 'app/models/version.rb', line 310

def visible_fixed_issues
  @visible_fixed_issues ||= fixed_issues.visible
end

#wiki_pageObject



314
315
316
317
318
319
# File 'app/models/version.rb', line 314

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