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
|
|
|
|
|
|
|
|
scope :between_dates, -> (start_date, end_date) do
|
|
|
|
where('spent_at BETWEEN ? AND ?', start_date, end_date)
|
|
|
|
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
|
|
|
|
errors.add(:base, 'Only Issue ID or Merge Request ID is required')
|
|
|
|
elsif issuable.nil?
|
|
|
|
errors.add(:base, 'Issue or Merge Request ID is required')
|
|
|
|
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
|