Module: Upkeep::Runtime::ChangeEvents

Defined in:
lib/upkeep/runtime.rb

Class Method Summary collapse

Class Method Details

.active_record_commit(record) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/upkeep/runtime.rb', line 351

def active_record_commit(record)
  return active_record_destroy(record) if record.destroyed?

  attribute_changes = previous_changes(record.previous_changes)

  {
    type: created_record?(record, attribute_changes) ? "create" : "update",
    table: record.class.table_name,
    model: record.class.name,
    id: record.id,
    changed_attributes: attribute_changes.keys.sort,
    old_values: attribute_changes.transform_values { |change| change.fetch(:old) },
    new_values: attribute_changes.transform_values { |change| change.fetch(:new) },
    attribute_changes: attribute_changes
  }
end

.active_record_destroy(record) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/upkeep/runtime.rb', line 368

def active_record_destroy(record)
  old_values = record.attributes.transform_keys(&:to_s)

  {
    type: "destroy",
    table: record.class.table_name,
    model: record.class.name,
    id: record.id,
    changed_attributes: old_values.keys.sort,
    old_values: old_values,
    new_values: {},
    attribute_changes: old_values.transform_values { |value| { old: value, new: nil } }
  }
end

.active_record_update_columns(record, changed_attributes:, new_values: {}) ⇒ Object



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/upkeep/runtime.rb', line 383

def active_record_update_columns(record, changed_attributes:, new_values: {})
  changed_attributes = Array(changed_attributes).map(&:to_s).sort
  new_values = new_values.transform_keys(&:to_s)

  {
    type: "update",
    table: record.class.table_name,
    model: record.class.name,
    id: record.class.primary_key && record.public_send(record.class.primary_key),
    changed_attributes: changed_attributes,
    old_values: {},
    new_values: new_values,
    attribute_changes: changed_attributes.to_h do |attribute|
      [attribute, { old: nil, new: new_values[attribute] }]
    end
  }
end

.bulk_delete(table:, model:, changed_attributes:, predicate_sql:, predicate_coverage:, predicate_table_columns:) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/upkeep/runtime.rb', line 421

def bulk_delete(table:, model:, changed_attributes:, predicate_sql:, predicate_coverage:, predicate_table_columns:)
  changed_attributes = Array(changed_attributes).map(&:to_s).sort

  {
    type: "bulk_delete",
    table: table,
    model: model,
    changed_attributes: changed_attributes,
    old_values: {},
    new_values: {},
    attribute_changes: changed_attributes.to_h { |attribute| [attribute, { old: nil, new: nil }] },
    predicate_sql: predicate_sql,
    predicate_coverage: predicate_coverage,
    predicate_table_columns: predicate_table_columns
  }
end

.bulk_update(table:, model:, changed_attributes:, predicate_sql:, predicate_coverage:, predicate_table_columns:, new_values: {}) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/upkeep/runtime.rb', line 401

def bulk_update(table:, model:, changed_attributes:, predicate_sql:, predicate_coverage:, predicate_table_columns:, new_values: {})
  changed_attributes = Array(changed_attributes).map(&:to_s).sort
  new_values = new_values.transform_keys(&:to_s)

  {
    type: "bulk_update",
    table: table,
    model: model,
    changed_attributes: changed_attributes,
    old_values: {},
    new_values: new_values,
    attribute_changes: changed_attributes.to_h do |attribute|
      [attribute, { old: nil, new: new_values[attribute] }]
    end,
    predicate_sql: predicate_sql,
    predicate_coverage: predicate_coverage,
    predicate_table_columns: predicate_table_columns
  }
end

.created_record?(record, attribute_changes) ⇒ Boolean

Returns:

  • (Boolean)


444
445
446
447
448
449
450
# File 'lib/upkeep/runtime.rb', line 444

def created_record?(record, attribute_changes)
  primary_key = record.class.primary_key
  return false unless primary_key

  primary_key_change = attribute_changes[primary_key.to_s]
  primary_key_change && primary_key_change.fetch(:old).nil? && !primary_key_change.fetch(:new).nil?
end

.previous_changes(changes) ⇒ Object



438
439
440
441
442
# File 'lib/upkeep/runtime.rb', line 438

def previous_changes(changes)
  changes.to_h.transform_keys(&:to_s).transform_values do |(old_value, new_value)|
    { old: old_value, new: new_value }
  end
end