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

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

class BulkUpdateIntegrationService
  def initialize(integration, batch)
    @integration = integration
    @batch = batch
  end

  # rubocop: disable CodeReuse/ActiveRecord
  def execute
    Integration.transaction do
      Integration.where(id: batch_ids).update_all(integration_hash)

      if integration.data_fields_present?
        integration.data_fields.class.where(data_fields_foreign_key => batch_ids).update_all(data_fields_hash)
      end
    end
  end
  # rubocop: enable CodeReuse/ActiveRecord

  private

  attr_reader :integration, :batch

  # service_id or integration_id
  def data_fields_foreign_key
    integration.data_fields.class.reflections['integration'].foreign_key
  end

  def integration_hash
    integration.to_integration_hash.tap { |json| json['inherit_from_id'] = integration.inherit_from_id || integration.id }
  end

  def data_fields_hash
    integration.to_data_fields_hash
  end

  def batch_ids
    @batch_ids ||=
      if batch.is_a?(ActiveRecord::Relation)
        batch.select(:id)
      else
        batch.map(&:id)
      end
  end
end