Commit 658fc330 authored by Ruben Davila's avatar Ruben Davila

Fix broken specs plus some refactoring.

parent 50573a63
......@@ -16,13 +16,15 @@ module TimeTrackable
has_many :timelogs, as: :trackable, dependent: :destroy
end
def spend_time=(args)
return unless valid_spend_time_args?(args)
def spend_time=(seconds:, user:)
# Exit if time to subtract exceeds the total time spent.
return if seconds < 0 && (seconds.abs > total_time_spent)
seconds = args[:seconds]
new_time_spent = seconds.zero? ? -(total_time_spent) : seconds
# When seconds = 0 we reset the total time spent by creating a new Timelog
# record with a negative value that is equal to the current total time spent.
new_time_spent = seconds.zero? ? (total_time_spent * -1) : seconds
timelogs.new(user: args[:user], time_spent: new_time_spent)
timelogs.new(user: user, time_spent: new_time_spent)
@time_spent = seconds
end
......@@ -30,16 +32,4 @@ module TimeTrackable
def total_time_spent
timelogs.sum(:time_spent)
end
private
def valid_spend_time_args?(args)
return false if [:seconds, :user].any? { |k| args[k].blank? }
# time to subtract exceeds the total time spent
seconds = args[:seconds]
return false if seconds < 0 && (seconds.abs > total_time_spent)
true
end
end
......@@ -149,7 +149,7 @@ module SystemNoteService
# Returns the created Note object
def change_time_spent(noteable, project, author)
time_spent = noteable.time_spent
time_spent = noteable.time_spent
if time_spent.zero?
body = "Removed time spent on this #{noteable.human_class_name}"
......
......@@ -353,5 +353,6 @@ Timelog:
- time_spent
- trackable_id
- trackable_type
- user_id
- created_at
- updated_at
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment