2020-02-19 18:09:10 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-03-02 00:07:41 +00:00
|
|
|
class ResourceEvent < ApplicationRecord
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
include Importable
|
2020-02-19 18:09:10 +00:00
|
|
|
|
2020-03-02 00:07:41 +00:00
|
|
|
self.abstract_class = true
|
2020-02-19 18:09:10 +00:00
|
|
|
|
2020-03-02 00:07:41 +00:00
|
|
|
validates :user, presence: { unless: :importing? }, on: :create
|
2020-02-19 18:09:10 +00:00
|
|
|
|
2020-03-02 00:07:41 +00:00
|
|
|
belongs_to :user
|
2020-02-19 18:09:10 +00:00
|
|
|
|
2020-03-02 00:07:41 +00:00
|
|
|
scope :created_after, ->(time) { where('created_at > ?', time) }
|
|
|
|
|
|
|
|
def discussion_id
|
|
|
|
strong_memoize(:discussion_id) do
|
|
|
|
Digest::SHA1.hexdigest(discussion_id_key.join("-"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def discussion_id_key
|
|
|
|
[self.class.name, created_at, user_id]
|
2020-02-19 18:09:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def exactly_one_issuable
|
|
|
|
issuable_count = self.class.issuable_attrs.count { |attr| self["#{attr}_id"] }
|
|
|
|
|
|
|
|
return true if issuable_count == 1
|
|
|
|
|
|
|
|
# if none of issuable IDs is set, check explicitly if nested issuable
|
|
|
|
# object is set, this is used during project import
|
|
|
|
if issuable_count == 0 && importing?
|
|
|
|
issuable_count = self.class.issuable_attrs.count { |attr| self.public_send(attr) } # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
|
|
|
|
return true if issuable_count == 1
|
|
|
|
end
|
|
|
|
|
2020-03-03 09:07:54 +00:00
|
|
|
errors.add(
|
|
|
|
:base, _("Exactly one of %{attributes} is required") %
|
|
|
|
{ attributes: self.class.issuable_attrs.join(', ') }
|
|
|
|
)
|
2020-02-19 18:09:10 +00:00
|
|
|
end
|
|
|
|
end
|