summaryrefslogtreecommitdiff
path: root/app/models/resource_event.rb
blob: 54fa4137f730ba5fcca193ae1b08b9aa84c5bddc (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
# frozen_string_literal: true

class ResourceEvent < ApplicationRecord
  include Gitlab::Utils::StrongMemoize
  include Importable

  self.abstract_class = true

  validates :user, presence: { unless: :importing? }, on: :create

  belongs_to :user

  scope :created_after, ->(time) { where('created_at > ?', time) }
  scope :created_on_or_before, ->(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, id, user_id]
  end

  def exactly_one_issuable
    issuable_count = self.class.issuable_attrs.count { |attr| self["#{attr}_id"] }

    return true if issuable_count == 1

    errors.add(
      :base, _("Exactly one of %{attributes} is required") %
        { attributes: self.class.issuable_attrs.join(', ') }
    )
  end
end