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


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

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)


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

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


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

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


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

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)

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

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)

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

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

#base_reloadObject


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

alias :base_reload :reload

#behind_schedule?Boolean

Returns:

  • (Boolean)

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

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)

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

def closed?
  status == 'closed'
end

#closed_issues_countObject

Returns the total amount of closed issues for this version.


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

def closed_issues_count
  fixed_issues.closed_count
end

#closed_percentObject

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


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

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)

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

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.


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

def completed_percent
  fixed_issues.completed_percent
end

#css_classesObject


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

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

#default_project_versionObject


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

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


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

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

#deletable?Boolean

Returns:

  • (Boolean)

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

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

#due_dateObject


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

def due_date
  effective_date
end

#due_date=(arg) ⇒ Object


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

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


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

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)


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

def estimated_hours
  fixed_issues.estimated_hours
end

#issues_countObject

Returns assigned issues count


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

def issues_count
  fixed_issues.count
end

#open?Boolean

Returns:

  • (Boolean)

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

def open?
  status == 'open'
end

#open_issues_countObject

Returns the total amount of open issues for this version.


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

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)

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

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

#reload(*args) ⇒ Object


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

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
187
# 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)

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

def shared?
  sharing != 'none'
end

#spent_hoursObject

Returns the total reported time for this version


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

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

#start_dateObject


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

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

#to_sObject


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

def to_s; name end

#to_s_with_projectObject


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

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)

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

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

#visible_custom_field_values(user = nil) ⇒ Object


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

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


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

def visible_fixed_issues
  @visible_fixed_issues ||= fixed_issues.visible
end

#wiki_pageObject


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

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