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

linkable_item.rb « concerns « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 135252727ab688c4dacb1fb4f42677c1f87efbd5 (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
# frozen_string_literal: true

# == LinkableItem concern
#
# Contains common functionality shared between related issue links and related work item links
#
# Used by IssueLink, WorkItems::RelatedWorkItemLink
#
module LinkableItem
  extend ActiveSupport::Concern
  include FromUnion
  include IssuableLink

  included do
    validate :check_existing_parent_link

    scope :for_source, ->(item) { where(source_id: item.id) }
    scope :for_target, ->(item) { where(target_id: item.id) }
    scope :for_items, ->(source, target) do
      where(source: source, target: target).or(where(source: target, target: source))
    end

    private

    def check_existing_parent_link
      return unless source && target

      existing_relation = WorkItems::ParentLink.for_parents([source, target]).for_children([source, target])
      return if existing_relation.none?

      errors.add(:source, format(_('is a parent or child of this %{item}'), item: self.class.issuable_name))
    end
  end
end

LinkableItem.include_mod_with('LinkableItem::Callbacks')
LinkableItem.prepend_mod_with('LinkableItem')