Module: LcpRuby::BulkUpdater

Defined in:
lib/lcp_ruby/bulk_updater.rb

Class Method Summary collapse

Class Method Details

.tracked_update_all(scope, updates, action: :bulk_update, model_definition:) {|affected_ids, updates, action| ... } ⇒ Integer

Performs update_all on a scope and yields metadata for callers that need to react (e.g., auditing, event dispatch).

Parameters:

  • scope (ActiveRecord::Relation)

    the records to update

  • updates (Hash)

    attribute changes to apply

  • action (Symbol) (defaults to: :bulk_update)

    semantic action name (e.g., :bulk_update, :batch_discard)

  • model_definition (ModelDefinition)

    the model’s metadata

Yields:

  • (affected_ids, updates, action)

    optional block for post-update hooks

Returns:

  • (Integer)

    number of affected rows



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lcp_ruby/bulk_updater.rb', line 12

def self.tracked_update_all(scope, updates, action: :bulk_update, model_definition:)
  if block_given?
    affected_ids = scope.pluck(:id)
    return 0 if affected_ids.empty?

    result = scope.update_all(updates)
    yield(affected_ids, updates, action)
    result
  else
    scope.update_all(updates)
  end
end