2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class Timelog < ApplicationRecord
|
2021-02-22 17:27:13 +05:30
|
|
|
include Importable
|
2023-04-23 21:23:45 +05:30
|
|
|
include IgnorableColumns
|
|
|
|
|
|
|
|
ignore_column :note_id_convert_to_bigint, remove_with: '16.0', remove_after: '2023-05-22'
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
before_save :set_project
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
validates :time_spent, :user, presence: true
|
2021-09-30 23:02:18 +05:30
|
|
|
validates :summary, length: { maximum: 255 }
|
2021-02-22 17:27:13 +05:30
|
|
|
validate :issuable_id_is_present, unless: :importing?
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
belongs_to :issue, touch: true
|
|
|
|
belongs_to :merge_request, touch: true
|
2021-06-08 01:23:25 +05:30
|
|
|
belongs_to :project
|
2017-08-17 22:00:37 +05:30
|
|
|
belongs_to :user
|
2020-11-24 15:15:51 +05:30
|
|
|
belongs_to :note
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
scope :in_group, -> (group) do
|
|
|
|
joins(:project).where(projects: { namespace: group.self_and_descendants })
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
scope :in_project, -> (project) do
|
|
|
|
where(project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :for_user, -> (user) do
|
|
|
|
where(user: user)
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
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)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
scope :order_scope_asc, ->(field) { order(arel_table[field].asc.nulls_last) }
|
|
|
|
scope :order_scope_desc, ->(field) { order(arel_table[field].desc.nulls_last) }
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def issuable
|
|
|
|
issue || merge_request
|
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def self.sort_by_field(field, direction)
|
|
|
|
if direction == :asc
|
|
|
|
order_scope_asc(field)
|
|
|
|
else
|
|
|
|
order_scope_desc(field)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def issuable_id_is_present
|
|
|
|
if issue_id && merge_request_id
|
2021-04-29 21:17:54 +05:30
|
|
|
errors.add(:base, _('Only Issue ID or merge request ID is required'))
|
2017-08-17 22:00:37 +05:30
|
|
|
elsif issuable.nil?
|
2021-04-29 21:17:54 +05:30
|
|
|
errors.add(:base, _('Issue or merge request ID is required'))
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def set_project
|
|
|
|
self.project_id = issuable.project_id
|
|
|
|
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
|