2020-02-19 13:09:10 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-03-01 19:07:41 -05:00
|
|
|
class ResourceEvent < ApplicationRecord
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
include Importable
|
2020-02-19 13:09:10 -05:00
|
|
|
|
2020-03-01 19:07:41 -05:00
|
|
|
self.abstract_class = true
|
2020-02-19 13:09:10 -05:00
|
|
|
|
2020-03-01 19:07:41 -05:00
|
|
|
validates :user, presence: { unless: :importing? }, on: :create
|
2020-02-19 13:09:10 -05:00
|
|
|
|
2020-03-01 19:07:41 -05:00
|
|
|
belongs_to :user
|
2020-02-19 13:09:10 -05:00
|
|
|
|
2020-03-01 19:07:41 -05:00
|
|
|
scope :created_after, ->(time) { where('created_at > ?', time) }
|
2020-07-16 20:09:37 -04:00
|
|
|
scope :created_on_or_before, ->(time) { where('created_at <= ?', time) }
|
2020-03-01 19:07:41 -05:00
|
|
|
|
|
|
|
def discussion_id
|
|
|
|
strong_memoize(:discussion_id) do
|
|
|
|
Digest::SHA1.hexdigest(discussion_id_key.join("-"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def discussion_id_key
|
2020-03-04 04:08:20 -05:00
|
|
|
[self.class.name, id, user_id]
|
2020-02-19 13:09:10 -05: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 04:07:54 -05:00
|
|
|
errors.add(
|
|
|
|
:base, _("Exactly one of %{attributes} is required") %
|
|
|
|
{ attributes: self.class.issuable_attrs.join(', ') }
|
|
|
|
)
|
2020-02-19 13:09:10 -05:00
|
|
|
end
|
|
|
|
end
|