debian-mirror-gitlab/app/services/bulk_update_integration_service.rb

44 lines
988 B
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
class BulkUpdateIntegrationService
2022-07-23 23:45:48 +05:30
include Integrations::BulkOperationHashes
2021-01-03 14:25:43 +05:30
def initialize(integration, batch)
@integration = integration
@batch = batch
end
# rubocop: disable CodeReuse/ActiveRecord
def execute
2021-06-08 01:23:25 +05:30
Integration.transaction do
2022-07-23 23:45:48 +05:30
Integration.where(id: batch_ids).update_all(integration_hash(:update))
2021-01-03 14:25:43 +05:30
if integration.data_fields_present?
2022-07-23 23:45:48 +05:30
integration.data_fields.class.where(data_fields_foreign_key => batch_ids)
.update_all(
data_fields_hash(:update)
)
2021-01-03 14:25:43 +05:30
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
private
attr_reader :integration, :batch
2021-12-11 22:18:48 +05:30
# service_id or integration_id
def data_fields_foreign_key
integration.data_fields.class.reflections['integration'].foreign_key
end
2021-10-27 15:23:28 +05:30
def batch_ids
@batch_ids ||=
if batch.is_a?(ActiveRecord::Relation)
batch.select(:id)
else
batch.map(&:id)
end
end
2021-01-03 14:25:43 +05:30
end