Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Davila <rdavila84@gmail.com>2016-12-23 08:44:02 +0300
committerRuben Davila <rdavila84@gmail.com>2017-01-15 19:10:04 +0300
commit17196a2ff31c4eb65fa9ecff6f7208171e26059b (patch)
treee9a491799b764c42f3c0c20cb33fa763ebb520df /app/models/concerns/time_trackable.rb
parent64dd41a0e21360c380cab394f8a5c9b4945b7fd1 (diff)
Backport backend work for time tracking.
Diffstat (limited to 'app/models/concerns/time_trackable.rb')
-rw-r--r--app/models/concerns/time_trackable.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/app/models/concerns/time_trackable.rb b/app/models/concerns/time_trackable.rb
new file mode 100644
index 00000000000..6fa2af4e4e6
--- /dev/null
+++ b/app/models/concerns/time_trackable.rb
@@ -0,0 +1,58 @@
+# == TimeTrackable concern
+#
+# Contains functionality related to objects that support time tracking.
+#
+# Used by Issue and MergeRequest.
+#
+
+module TimeTrackable
+ extend ActiveSupport::Concern
+
+ included do
+ attr_reader :time_spent
+
+ alias_method :time_spent?, :time_spent
+
+ default_value_for :time_estimate, value: 0, allows_nil: false
+
+ has_many :timelogs, as: :trackable, dependent: :destroy
+ end
+
+ def spend_time(seconds, user)
+ return if seconds == 0
+
+ @time_spent = seconds
+ @time_spent_user = user
+
+ if seconds == :reset
+ reset_spent_time
+ else
+ add_or_subtract_spent_time
+ end
+ end
+
+ def total_time_spent
+ timelogs.sum(:time_spent)
+ end
+
+ def human_total_time_spent
+ Gitlab::TimeTrackingFormatter.output(total_time_spent)
+ end
+
+ def human_time_estimate
+ Gitlab::TimeTrackingFormatter.output(time_estimate)
+ end
+
+ private
+
+ def reset_spent_time
+ timelogs.new(time_spent: total_time_spent * -1, user: @time_spent_user)
+ end
+
+ def add_or_subtract_spent_time
+ # Exit if time to subtract exceeds the total time spent.
+ return if time_spent < 0 && (time_spent.abs > total_time_spent)
+
+ timelogs.new(time_spent: time_spent, user: @time_spent_user)
+ end
+end