2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Timelog < ApplicationRecord
|
2020-12-16 10:10:18 -05:00
|
|
|
include Importable
|
|
|
|
|
2021-04-27 17:10:09 -04:00
|
|
|
before_save :set_project
|
|
|
|
|
2016-12-23 00:44:02 -05:00
|
|
|
validates :time_spent, :user, presence: true
|
2021-07-02 02:07:28 -04:00
|
|
|
validates :summary, length: { maximum: 255 }
|
2020-12-16 10:10:18 -05:00
|
|
|
validate :issuable_id_is_present, unless: :importing?
|
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
|
2021-04-27 17:10:09 -04:00
|
|
|
belongs_to :project
|
2016-12-23 00:44:02 -05:00
|
|
|
belongs_to :user
|
2020-09-08 11:08:41 -04:00
|
|
|
belongs_to :note
|
2017-01-25 20:16:09 -05:00
|
|
|
|
2021-05-06 08:10:38 -04:00
|
|
|
scope :in_group, -> (group) do
|
|
|
|
joins(:project).where(projects: { namespace: group.self_and_descendants })
|
2019-12-10 07:07:55 -05:00
|
|
|
end
|
|
|
|
|
2021-08-10 08:11:00 -04:00
|
|
|
scope :in_project, -> (project) do
|
|
|
|
where(project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :for_user, -> (user) do
|
|
|
|
where(user: user)
|
|
|
|
end
|
|
|
|
|
2021-06-01 17:10:06 -04:00
|
|
|
scope :at_or_after, -> (start_time) do
|
|
|
|
where('spent_at >= ?', start_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :at_or_before, -> (end_time) do
|
|
|
|
where('spent_at <= ?', 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
|
2021-04-05 17:09:19 -04:00
|
|
|
errors.add(:base, _('Only Issue ID or merge request ID is required'))
|
2017-01-25 20:16:09 -05:00
|
|
|
elsif issuable.nil?
|
2021-04-05 17:09:19 -04: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
|
|
|
|
2021-04-27 17:10:09 -04:00
|
|
|
def set_project
|
|
|
|
self.project_id = issuable.project_id
|
|
|
|
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
|