summaryrefslogtreecommitdiff
path: root/app/models/bulk_imports/entity.rb
blob: 2d0bba7bccce073ee5e3e10c1745a42bc0108f8b (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
# frozen_string_literal: true

class BulkImports::Entity < ApplicationRecord
  self.table_name = 'bulk_import_entities'

  belongs_to :bulk_import, optional: false
  belongs_to :parent, class_name: 'BulkImports::Entity', optional: true

  belongs_to :project, optional: true
  belongs_to :group, foreign_key: :namespace_id, optional: true

  validates :project, absence: true, if: :group
  validates :group, absence: true, if: :project
  validates :source_type, :source_full_path, :destination_name,
            :destination_namespace, presence: true

  validate :validate_parent_is_a_group, if: :parent
  validate :validate_imported_entity_type

  enum source_type: { group_entity: 0, project_entity: 1 }

  state_machine :status, initial: :created do
    state :created, value: 0
  end

  private

  def validate_parent_is_a_group
    unless parent.group_entity?
      errors.add(:parent, s_('BulkImport|must be a group'))
    end
  end

  def validate_imported_entity_type
    if group.present? && project_entity?
      errors.add(:group, s_('BulkImport|expected an associated Project but has an associated Group'))
    end

    if project.present? && group_entity?
      errors.add(:project, s_('BulkImport|expected an associated Group but has an associated Project'))
    end
  end
end