Class: LcpRuby::BatchActions::DestroyService

Inherits:
BaseService
  • Object
show all
Defined in:
lib/lcp_ruby/batch_actions/destroy_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#batch_operation, #current_user, #evaluator, #model_definition, #progress, #records

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Constructor Details

This class inherits a constructor from LcpRuby::BatchActions::BaseService

Instance Method Details

#callObject



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
# File 'lib/lcp_ruby/batch_actions/destroy_service.rb', line 4

def call
  destroyed = 0
  denied = 0

  each_record do |record, index|
    progress&.call(index)

    if evaluator.can_for_record?(:destroy, record)
      if model_definition.soft_delete?
        record.discard!(by: current_user)
      else
        record.destroy!
      end
      log_item(record, "deleted")
      destroyed += 1
    else
      log_item(record, "skipped", error_details: { reason: "permission_denied" })
      denied += 1
    end
  end

  finalize_batch_operation(destroyed, denied, 0)

  Actions::Result.new(
    success: true,
    message: build_message("destroyed", destroyed, denied),
    redirect_to: nil,
    data: batch_operation ? { batch_operation_id: batch_operation.id } : nil,
    errors: []
  )
end