Class: Version

Inherits:
ActiveRecord::Base
  • Object
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

Class Method Details

.fields_for_order_statement(table = nil) ⇒ Object



356
357
358
359
# File 'app/models/version.rb', line 356

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)



339
340
341
342
343
344
345
346
347
# File 'app/models/version.rb', line 339

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



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'app/models/version.rb', line 318

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



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'app/models/version.rb', line 364

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)


210
211
212
# File 'app/models/version.rb', line 210

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)


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

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

#base_reloadObject



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

alias :base_reload :reload

#behind_schedule?Boolean

Returns:

  • (Boolean)


257
258
259
260
261
262
263
264
265
266
# File 'app/models/version.rb', line 257

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)


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

def closed?
  status == 'closed'
end

#closed_issues_countObject

Returns the total amount of closed issues for this version.



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

def closed_issues_count
  fixed_issues.closed_count
end

#closed_percentObject

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



275
276
277
# File 'app/models/version.rb', line 275

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)


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

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.



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

def completed_percent
  fixed_issues.completed_percent
end

#css_classesObject



349
350
351
352
353
354
# File 'app/models/version.rb', line 349

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

#default_project_versionObject



393
394
395
396
397
398
399
# File 'app/models/version.rb', line 393

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



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

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

#deletable?Boolean

Returns:

  • (Boolean)


389
390
391
# File 'app/models/version.rb', line 389

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

#due_dateObject



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

def due_date
  effective_date
end

#due_date=(arg) ⇒ Object



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

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



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

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)



235
236
237
# File 'app/models/version.rb', line 235

def estimated_hours
  fixed_issues.estimated_hours
end

#issues_countObject

Returns assigned issues count



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

def issues_count
  fixed_issues.count
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?
  status == 'open'
end

#open_issues_countObject

Returns the total amount of open issues for this version.



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

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)


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

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

#reload(*args) ⇒ Object



215
216
217
218
219
# File 'app/models/version.rb', line 215

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

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



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

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(attrs, user)
end

#shared?Boolean

Returns true if the version is shared, otherwise false

Returns:

  • (Boolean)


385
386
387
# File 'app/models/version.rb', line 385

def shared?
  sharing != 'none'
end

#spent_hoursObject

Returns the total reported time for this version



240
241
242
# File 'app/models/version.rb', line 240

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

#start_dateObject



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

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

#to_sObject



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

def to_s; name end

#to_s_with_projectObject



312
313
314
# File 'app/models/version.rb', line 312

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)


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

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

#visible_custom_field_values(user = nil) ⇒ Object



198
199
200
201
202
203
# File 'app/models/version.rb', line 198

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



299
300
301
# File 'app/models/version.rb', line 299

def visible_fixed_issues
  @visible_fixed_issues ||= fixed_issues.visible
end

#wiki_pageObject



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

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