debian-mirror-gitlab/app/models/concerns/time_trackable.rb

102 lines
2.7 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
# == TimeTrackable concern
#
# Contains functionality related to objects that support time tracking.
#
# Used by Issue and MergeRequest.
#
module TimeTrackable
extend ActiveSupport::Concern
included do
2021-10-27 15:23:28 +05:30
attr_reader :time_spent, :time_spent_user, :spent_at, :summary
2017-08-17 22:00:37 +05:30
alias_method :time_spent?, :time_spent
default_value_for :time_estimate, value: 0, allows_nil: false
validates :time_estimate, numericality: { message: 'has an invalid format' }, allow_nil: false
validate :check_negative_time_spent
2021-10-27 15:23:28 +05:30
has_many :timelogs, dependent: :destroy, autosave: true # rubocop:disable Cop/ActiveRecordDependent
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
def spend_time(options)
@time_spent = options[:duration]
2020-10-24 23:57:45 +05:30
@time_spent_note_id = options[:note_id]
2018-03-17 18:26:18 +05:30
@time_spent_user = User.find(options[:user_id])
@spent_at = options[:spent_at]
2021-10-27 15:23:28 +05:30
@summary = options[:summary]
2017-08-17 22:00:37 +05:30
@original_total_time_spent = nil
return if @time_spent == 0
2021-09-04 01:27:46 +05:30
@timelog = if @time_spent == :reset
reset_spent_time
else
add_or_subtract_spent_time
end
2017-08-17 22:00:37 +05:30
end
alias_method :spend_time=, :spend_time
2018-03-17 18:26:18 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
def total_time_spent
timelogs.sum(:time_spent)
end
def human_total_time_spent
Gitlab::TimeTrackingFormatter.output(total_time_spent)
end
2021-09-04 01:27:46 +05:30
def time_change
@timelog&.time_spent.to_i # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
def human_time_change
Gitlab::TimeTrackingFormatter.output(time_change)
end
2017-08-17 22:00:37 +05:30
def human_time_estimate
Gitlab::TimeTrackingFormatter.output(time_estimate)
end
2018-11-08 19:23:39 +05:30
def time_estimate=(val)
val.is_a?(Integer) ? super([val, Gitlab::Database::MAX_INT_VALUE].min) : super(val)
end
2017-08-17 22:00:37 +05:30
private
def reset_spent_time
2018-03-17 18:26:18 +05:30
timelogs.new(time_spent: total_time_spent * -1, user: @time_spent_user) # rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
def add_or_subtract_spent_time
2018-03-17 18:26:18 +05:30
timelogs.new(
time_spent: time_spent,
2020-10-24 23:57:45 +05:30
note_id: @time_spent_note_id,
2018-03-17 18:26:18 +05:30
user: @time_spent_user,
2021-10-27 15:23:28 +05:30
spent_at: @spent_at,
summary: @summary
2018-03-17 18:26:18 +05:30
)
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
def check_negative_time_spent
return if time_spent.nil? || time_spent == :reset
2018-03-17 18:26:18 +05:30
if time_spent < 0 && (time_spent.abs > original_total_time_spent)
2020-04-08 14:13:33 +05:30
errors.add(:base, _('Time to subtract exceeds the total time spent'))
2017-08-17 22:00:37 +05:30
end
end
2018-03-17 18:26:18 +05:30
# 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
2017-08-17 22:00:37 +05:30
end