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

Fix broken specs plus some refactoring.

parent 50573a63
...@@ -16,13 +16,15 @@ module TimeTrackable ...@@ -16,13 +16,15 @@ module TimeTrackable
has_many :timelogs, as: :trackable, dependent: :destroy has_many :timelogs, as: :trackable, dependent: :destroy
end end
def spend_time=(args) def spend_time=(seconds:, user:)
return unless valid_spend_time_args?(args) # Exit if time to subtract exceeds the total time spent.
return if seconds < 0 && (seconds.abs > total_time_spent)
seconds = args[:seconds] # When seconds = 0 we reset the total time spent by creating a new Timelog
new_time_spent = seconds.zero? ? -(total_time_spent) : seconds # 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 @time_spent = seconds
end end
...@@ -30,16 +32,4 @@ module TimeTrackable ...@@ -30,16 +32,4 @@ module TimeTrackable
def total_time_spent def total_time_spent
timelogs.sum(:time_spent) timelogs.sum(:time_spent)
end 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 end
...@@ -149,7 +149,7 @@ module SystemNoteService ...@@ -149,7 +149,7 @@ module SystemNoteService
# Returns the created Note object # Returns the created Note object
def change_time_spent(noteable, project, author) def change_time_spent(noteable, project, author)
time_spent = noteable.time_spent time_spent = noteable.time_spent
if time_spent.zero? if time_spent.zero?
body = "Removed time spent on this #{noteable.human_class_name}" body = "Removed time spent on this #{noteable.human_class_name}"
......
...@@ -353,5 +353,6 @@ Timelog: ...@@ -353,5 +353,6 @@ Timelog:
- time_spent - time_spent
- trackable_id - trackable_id
- trackable_type - trackable_type
- user_id
- created_at - created_at
- updated_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