timelog.rb 660 Bytes
Newer Older
1 2
# frozen_string_literal: true

3 4
class Timelog < ActiveRecord::Base
  validates :time_spent, :user, presence: true
5
  validate :issuable_id_is_present
6

7 8
  belongs_to :issue, touch: true
  belongs_to :merge_request, touch: true
9
  belongs_to :user
10 11 12 13 14 15 16 17 18 19 20 21 22 23

  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
24 25 26 27 28

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