summaryrefslogtreecommitdiff
path: root/app/models/timelog.rb
blob: 7c394736560c7566efc2a508daf58b07a4274fb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

class Timelog < ApplicationRecord
  include Importable

  before_save :set_project

  validates :time_spent, :user, presence: true
  validates :summary, length: { maximum: 255 }
  validate :issuable_id_is_present, unless: :importing?

  belongs_to :issue, touch: true
  belongs_to :merge_request, touch: true
  belongs_to :project
  belongs_to :user
  belongs_to :note

  scope :in_group, -> (group) do
    joins(:project).where(projects: { namespace: group.self_and_descendants })
  end

  scope :in_project, -> (project) do
    where(project: project)
  end

  scope :for_user, -> (user) do
    where(user: user)
  end

  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)
  end

  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

  def set_project
    self.project_id = issuable.project_id
  end

  # Rails5 defaults to :touch_later, overwrite for normal touch
  def belongs_to_touch_method
    :touch
  end
end