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

object_builder.rb « group « import_export « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 43cc7a78a611a51aaf6cced14f1960a636abac9a (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
# frozen_string_literal: true

module Gitlab
  module ImportExport
    module Group
      # Given a class, it finds or creates a new object at group level.
      #
      # Example:
      #   `Group::ObjectBuilder.build(Label, label_attributes)`
      #    finds or initializes a label with the given attributes.
      class ObjectBuilder < Base::ObjectBuilder
        def initialize(klass, attributes)
          super

          @group = @attributes['group']

          update_description
        end

        private

        attr_reader :group

        # Convert description empty string to nil
        # due to existing object being saved with description: nil
        # Which makes object lookup to fail since nil != ''
        def update_description
          attributes['description'] = nil if attributes['description'] == ''
        end

        def where_clauses
          [
            where_clause_base,
            where_clause_for_title,
            where_clause_for_description,
            where_clause_for_created_at
          ].compact
        end

        # Returns Arel clause `"{table_name}"."group_id" = {group.id}`
        def where_clause_base
          table[:group_id].in(group_and_ancestor_ids)
        end

        def group_and_ancestor_ids
          group.ancestors.map(&:id) << group.id
        end
      end
    end
  end
end