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

class DescriptionVersion < ApplicationRecord
  belongs_to :issue
  belongs_to :merge_request

  validate :exactly_one_issuable

  def self.issuable_attrs
    %i(issue merge_request).freeze
  end

  def issuable
    issue || merge_request
  end

  private

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

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

DescriptionVersion.prepend_if_ee('EE::DescriptionVersion')