Welcome to mirror list, hosted at ThFree Co, Russian Federation.

issuable_link.rb « concerns « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e884e5acecf5affbf467f69a6e2a6cb0d4085d33 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true

# == IssuableLink concern
#
# Contains common functionality shared between related Issues and related Epics
#
# Used by IssueLink, Epic::RelatedEpicLink
#
module IssuableLink
  extend ActiveSupport::Concern

  TYPE_RELATES_TO = 'relates_to'
  TYPE_BLOCKS = 'blocks' ## EE-only. Kept here to be used on link_type enum.

  class_methods do
    def inverse_link_type(type)
      type
    end

    def issuable_type
      raise NotImplementedError
    end

    def issuable_name
      issuable_type.to_s.humanize(capitalize: false)
    end

    # Used to get the available types for the API
    # overriden in EE
    def available_link_types
      [TYPE_RELATES_TO]
    end
  end

  included do
    validates :source, presence: true
    validates :target, presence: true
    validates :source, uniqueness: { scope: :target_id, message: 'is already related' }
    validate :check_self_relation
    validate :check_opposite_relation

    scope :for_source_or_target, ->(issuable) { where(source: issuable).or(where(target: issuable)) }

    enum link_type: { TYPE_RELATES_TO => 0, TYPE_BLOCKS => 1 }

    private

    def check_self_relation
      return unless source && target

      if source == target
        errors.add(:source, 'cannot be related to itself')
      end
    end

    def check_opposite_relation
      return unless source && target

      if self.class.base_class.find_by(source: target, target: source)
        errors.add(:source, "is already related to this #{self.class.issuable_name}")
      end
    end
  end
end

IssuableLink.prepend_mod_with('IssuableLink')
IssuableLink::ClassMethods.prepend_mod_with('IssuableLink::ClassMethods')