Module: TimeTrackable

Extended by:
ActiveSupport::Concern
Includes:
Gitlab::Utils::StrongMemoize
Included in:
Issue, MergeRequest
Defined in:
app/models/concerns/time_trackable.rb

Overview

TimeTrackable concern

Contains functionality related to objects that support time tracking.

Used by Issue and MergeRequest.

Instance Method Summary collapse

Instance Method Details

#clear_memoized_total_time_spentObject



28
29
30
# File 'app/models/concerns/time_trackable.rb', line 28

def clear_memoized_total_time_spent
  clear_memoization(:total_time_spent)
end

#human_time_changeObject



84
85
86
# File 'app/models/concerns/time_trackable.rb', line 84

def human_time_change
  Gitlab::TimeTrackingFormatter.output(time_change)
end

#human_time_estimateObject



88
89
90
# File 'app/models/concerns/time_trackable.rb', line 88

def human_time_estimate
  Gitlab::TimeTrackingFormatter.output(time_estimate)
end

#human_total_time_spentObject



76
77
78
# File 'app/models/concerns/time_trackable.rb', line 76

def human_total_time_spent
  Gitlab::TimeTrackingFormatter.output(total_time_spent)
end

#reload(*args) ⇒ Object



38
39
40
41
42
# File 'app/models/concerns/time_trackable.rb', line 38

def reload(*args)
  clear_memoized_total_time_spent

  super(*args)
end

#resetObject



32
33
34
35
36
# File 'app/models/concerns/time_trackable.rb', line 32

def reset
  clear_memoized_total_time_spent

  super
end

#set_time_estimate_default_valueObject



100
101
102
103
104
105
106
107
108
# File 'app/models/concerns/time_trackable.rb', line 100

def set_time_estimate_default_value
  return if new_record?
  return unless has_attribute?(:time_estimate)
  # time estimate can be set to nil, in case of an invalid value, e.g. a String instead of a number, in which case
  # we should not be overwriting it to default value, but rather have the validation catch the error
  return if time_estimate_changed?

  self.time_estimate = self.class.column_defaults['time_estimate'] if read_attribute(:time_estimate).nil?
end

#spend_time(options) ⇒ Object Also known as: spend_time=

rubocop:disable Gitlab/ModuleWithInstanceVariables



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/concerns/time_trackable.rb', line 45

def spend_time(options)
  @time_spent = options[:duration]
  @time_spent_note_id = options[:note_id]
  @time_spent_user = User.find(options[:user_id])
  @spent_at = options[:spent_at]
  @summary = options[:summary]
  @original_total_time_spent = nil
  @category_id = category_id(options[:category])

  return if @time_spent == 0

  @timelog = if @time_spent == :reset
               reset_spent_time
             else
               add_or_subtract_spent_time
             end
end

#time_changeObject



80
81
82
# File 'app/models/concerns/time_trackable.rb', line 80

def time_change
  @timelog&.time_spent.to_i # rubocop:disable Gitlab/ModuleWithInstanceVariables
end

#time_estimateObject



96
97
98
# File 'app/models/concerns/time_trackable.rb', line 96

def time_estimate
  super || self.class.column_defaults['time_estimate']
end

#time_estimate=(val) ⇒ Object



92
93
94
# File 'app/models/concerns/time_trackable.rb', line 92

def time_estimate=(val)
  val.is_a?(Integer) ? super([val, Gitlab::Database::MAX_INT_VALUE].min) : super(val)
end

#total_time_spentObject

rubocop:enable Gitlab/ModuleWithInstanceVariables



65
66
67
68
69
70
71
72
73
# File 'app/models/concerns/time_trackable.rb', line 65

def total_time_spent
  sum = timelogs.sum(:time_spent)

  # A new restriction has been introduced to limit total time spent to -
  # Timelog::MAX_TOTAL_TIME_SPENT or 3.154e+7 seconds (approximately a year, a generous limit)
  # Since there could be existing records that breach the limit, check and return the maximum/minimum allowed value.
  # (some issuable might have total time spent that's negative because a validation was missing.)
  sum.clamp(-Timelog::MAX_TOTAL_TIME_SPENT, Timelog::MAX_TOTAL_TIME_SPENT)
end