2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-23 00:44:02 -05:00
|
|
|
# == TimeTrackable concern
|
|
|
|
#
|
|
|
|
# Contains functionality related to objects that support time tracking.
|
|
|
|
#
|
|
|
|
# Used by Issue and MergeRequest.
|
|
|
|
#
|
2017-11-22 04:15:46 -05:00
|
|
|
|
2016-12-23 00:44:02 -05:00
|
|
|
module TimeTrackable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2021-07-27 05:10:08 -04:00
|
|
|
attr_reader :time_spent, :time_spent_user, :spent_at, :summary
|
2016-12-23 00:44:02 -05:00
|
|
|
|
|
|
|
alias_method :time_spent?, :time_spent
|
|
|
|
|
|
|
|
default_value_for :time_estimate, value: 0, allows_nil: false
|
|
|
|
|
2017-01-18 11:48:16 -05:00
|
|
|
validates :time_estimate, numericality: { message: 'has an invalid format' }, allow_nil: false
|
|
|
|
validate :check_negative_time_spent
|
|
|
|
|
2021-07-28 08:10:41 -04:00
|
|
|
has_many :timelogs, dependent: :destroy, autosave: true # rubocop:disable Cop/ActiveRecordDependent
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-01-18 11:48:16 -05:00
|
|
|
def spend_time(options)
|
|
|
|
@time_spent = options[:duration]
|
2020-07-23 02:09:19 -04:00
|
|
|
@time_spent_note_id = options[:note_id]
|
2017-12-14 14:32:55 -05:00
|
|
|
@time_spent_user = User.find(options[:user_id])
|
2017-09-22 12:20:44 -04:00
|
|
|
@spent_at = options[:spent_at]
|
2021-07-27 05:10:08 -04:00
|
|
|
@summary = options[:summary]
|
2017-01-18 11:48:16 -05:00
|
|
|
@original_total_time_spent = nil
|
2016-12-23 00:44:02 -05:00
|
|
|
|
2017-01-18 11:48:16 -05:00
|
|
|
return if @time_spent == 0
|
2016-12-23 00:44:02 -05:00
|
|
|
|
2021-06-11 05:09:58 -04:00
|
|
|
@timelog = if @time_spent == :reset
|
|
|
|
reset_spent_time
|
|
|
|
else
|
|
|
|
add_or_subtract_spent_time
|
|
|
|
end
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
2017-01-18 11:48:16 -05:00
|
|
|
alias_method :spend_time=, :spend_time
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2016-12-23 00:44:02 -05:00
|
|
|
|
|
|
|
def total_time_spent
|
|
|
|
timelogs.sum(:time_spent)
|
|
|
|
end
|
|
|
|
|
|
|
|
def human_total_time_spent
|
|
|
|
Gitlab::TimeTrackingFormatter.output(total_time_spent)
|
|
|
|
end
|
|
|
|
|
2021-06-11 05:09:58 -04:00
|
|
|
def time_change
|
|
|
|
@timelog&.time_spent.to_i # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
end
|
|
|
|
|
|
|
|
def human_time_change
|
|
|
|
Gitlab::TimeTrackingFormatter.output(time_change)
|
|
|
|
end
|
|
|
|
|
2016-12-23 00:44:02 -05:00
|
|
|
def human_time_estimate
|
|
|
|
Gitlab::TimeTrackingFormatter.output(time_estimate)
|
|
|
|
end
|
|
|
|
|
2018-05-15 06:40:17 -04:00
|
|
|
def time_estimate=(val)
|
|
|
|
val.is_a?(Integer) ? super([val, Gitlab::Database::MAX_INT_VALUE].min) : super(val)
|
|
|
|
end
|
|
|
|
|
2016-12-23 00:44:02 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def reset_spent_time
|
2017-11-22 02:50:36 -05:00
|
|
|
timelogs.new(time_spent: total_time_spent * -1, user: @time_spent_user) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2016-12-23 00:44:02 -05:00
|
|
|
def add_or_subtract_spent_time
|
2017-09-22 12:20:44 -04:00
|
|
|
timelogs.new(
|
|
|
|
time_spent: time_spent,
|
2020-07-23 02:09:19 -04:00
|
|
|
note_id: @time_spent_note_id,
|
2017-09-22 12:20:44 -04:00
|
|
|
user: @time_spent_user,
|
2021-07-27 05:10:08 -04:00
|
|
|
spent_at: @spent_at,
|
|
|
|
summary: @summary
|
2017-09-22 12:20:44 -04:00
|
|
|
)
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
2017-11-22 02:50:36 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2017-01-18 11:48:16 -05:00
|
|
|
|
|
|
|
def check_negative_time_spent
|
|
|
|
return if time_spent.nil? || time_spent == :reset
|
|
|
|
|
2017-11-17 07:27:16 -05:00
|
|
|
if time_spent < 0 && (time_spent.abs > original_total_time_spent)
|
2020-03-03 04:07:54 -05:00
|
|
|
errors.add(:base, _('Time to subtract exceeds the total time spent'))
|
2017-01-18 11:48:16 -05:00
|
|
|
end
|
|
|
|
end
|
2017-11-17 07:27:16 -05:00
|
|
|
|
|
|
|
# we need to cache the total time spent so multiple calls to #valid?
|
|
|
|
# doesn't give a false error
|
|
|
|
def original_total_time_spent
|
|
|
|
@original_total_time_spent ||= total_time_spent
|
|
|
|
end
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|