2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Timelog < ApplicationRecord
|
2016-12-23 00:44:02 -05:00
|
|
|
validates :time_spent, :user, presence: true
|
2017-01-25 20:16:09 -05:00
|
|
|
validate :issuable_id_is_present
|
2016-12-23 00:44:02 -05:00
|
|
|
|
2018-06-01 11:09:08 -04:00
|
|
|
belongs_to :issue, touch: true
|
|
|
|
belongs_to :merge_request, touch: true
|
2016-12-23 00:44:02 -05:00
|
|
|
belongs_to :user
|
2017-01-25 20:16:09 -05:00
|
|
|
|
2019-12-10 07:07:55 -05:00
|
|
|
scope :for_issues_in_group, -> (group) do
|
|
|
|
joins(:issue).where(
|
|
|
|
'EXISTS (?)',
|
|
|
|
Project.select(1).where(namespace: group.self_and_descendants)
|
|
|
|
.where('issues.project_id = projects.id')
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-05-05 08:09:31 -04:00
|
|
|
scope :between_times, -> (start_time, end_time) do
|
|
|
|
where('spent_at BETWEEN ? AND ?', start_time, end_time)
|
2019-12-10 07:07:55 -05:00
|
|
|
end
|
|
|
|
|
2017-01-25 20:16:09 -05:00
|
|
|
def issuable
|
|
|
|
issue || merge_request
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def issuable_id_is_present
|
|
|
|
if issue_id && merge_request_id
|
2020-03-03 04:07:54 -05:00
|
|
|
errors.add(:base, _('Only Issue ID or Merge Request ID is required'))
|
2017-01-25 20:16:09 -05:00
|
|
|
elsif issuable.nil?
|
2020-03-03 04:07:54 -05:00
|
|
|
errors.add(:base, _('Issue or Merge Request ID is required'))
|
2017-01-25 20:16:09 -05:00
|
|
|
end
|
|
|
|
end
|
2018-06-13 01:48:05 -04:00
|
|
|
|
|
|
|
# Rails5 defaults to :touch_later, overwrite for normal touch
|
|
|
|
def belongs_to_touch_method
|
|
|
|
:touch
|
|
|
|
end
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|