2019-10-18 07:11:44 -04:00
|
|
|
# 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
|
|
|
|
|
2019-11-15 07:06:12 -05:00
|
|
|
def issuable
|
|
|
|
issue || merge_request
|
|
|
|
end
|
|
|
|
|
2019-10-18 07:11:44 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def exactly_one_issuable
|
|
|
|
issuable_count = self.class.issuable_attrs.count { |attr| self["#{attr}_id"] }
|
|
|
|
|
2020-03-03 04:07:54 -05:00
|
|
|
if issuable_count != 1
|
|
|
|
errors.add(
|
|
|
|
:base,
|
|
|
|
_("Exactly one of %{attributes} is required") %
|
|
|
|
{ attributes: self.class.issuable_attrs.join(', ') }
|
|
|
|
)
|
|
|
|
end
|
2019-10-18 07:11:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
DescriptionVersion.prepend_if_ee('EE::DescriptionVersion')
|