2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class Timelog < ApplicationRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
validates :time_spent, :user, presence: true
|
|
|
|
validate :issuable_id_is_present
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
belongs_to :issue, touch: true
|
|
|
|
belongs_to :merge_request, touch: true
|
2017-08-17 22:00:37 +05:30
|
|
|
belongs_to :user
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
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-08-17 22:00:37 +05:30
|
|
|
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-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
# Rails5 defaults to :touch_later, overwrite for normal touch
|
|
|
|
def belongs_to_touch_method
|
|
|
|
:touch
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|