summaryrefslogtreecommitdiff
path: root/app/models/resource_label_event.rb
blob: 42c255fcd1e04042760c09389e45dd69c48c9136 (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
# frozen_string_literal: true

# This model is not used yet, it will be used for:
# https://gitlab.com/gitlab-org/gitlab-ce/issues/48483
class ResourceLabelEvent < ActiveRecord::Base
  belongs_to :user
  belongs_to :issue
  belongs_to :merge_request
  belongs_to :label

  validates :user, presence: true, on: :create
  validates :label, presence: true, on: :create
  validate :exactly_one_issuable

  enum action: {
    add: 1,
    remove: 2
  }

  def self.issuable_columns
    %i(issue_id merge_request_id).freeze
  end

  def issuable
    issue || merge_request
  end

  private

  def exactly_one_issuable
    if self.class.issuable_columns.count { |attr| self[attr] } != 1
      errors.add(:base, "Exactly one of #{self.class.issuable_columns.join(', ')} is required")
    end
  end
end