Class: LcpRuby::BackgroundJobs::Declarative::RunScopeAction

Inherits:
BaseAction show all
Defined in:
lib/lcp_ruby/background_jobs/declarative/run_scope_action.rb

Constant Summary collapse

ALLOWED_OPERATIONS =
%w[destroy_all delete_all update_all count].freeze

Instance Attribute Summary

Attributes inherited from BaseHandler

#definition, #execution

Instance Method Summary collapse

Methods inherited from BaseAction

#config

Methods inherited from BaseHandler

#attach_result!, #check_cancellation!, #flush_log!, #initialize, #log!, #params, #set_result_url!, #target_record, #triggered_by, #update_progress!

Constructor Details

This class inherits a constructor from LcpRuby::BackgroundJobs::BaseHandler

Instance Method Details

#performObject



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
# File 'lib/lcp_ruby/background_jobs/declarative/run_scope_action.rb', line 7

def perform
  model_name = config["model"]
  scope_name = config["scope"]
  operation = config["operation"] || "count"
  batch_size = (config["batch_size"] || 1000).to_i

  unless ALLOWED_OPERATIONS.include?(operation)
    raise JobError, "Invalid operation '#{operation}'. Allowed: #{ALLOWED_OPERATIONS.join(', ')}"
  end

  model_class = LcpRuby.registry.model_for(model_name)

  scope = if scope_name.present?
            scope_params = config["scope_params"] || {}
            scope_params = scope_params.transform_keys(&:to_sym) if scope_params.is_a?(Hash)
            if scope_params.any?
              model_class.send(scope_name, **scope_params)
            else
              model_class.send(scope_name)
            end
  else
            model_class.all
  end

  case operation
  when "destroy_all"
    perform_destroy_all(scope, batch_size)
  when "delete_all"
    perform_delete_all(scope, batch_size)
  when "update_all"
    perform_update_all(scope, batch_size)
  when "count"
    perform_count(scope)
  end
end