Class: ActiveVersion::Migrators::Audited

Inherits:
Base
  • Object
show all
Defined in:
lib/active_version/migrators/audited.rb

Overview

Migrator from audited gem

Constant Summary

Constants inherited from Base

Base::AUDIT_STORAGES

Class Method Summary collapse

Methods inherited from Base

create_audit_table, create_revision_table, create_translation_table

Class Method Details

.migrate(model_class, options = {}) ⇒ Integer

Migrate audits from audited gem

Parameters:

  • model_class (Class)

    ActiveRecord model class

  • options (Hash) (defaults to: {})

    Migration options

Options Hash (options):

  • :dry_run (Boolean) — default: false

    Don’t actually migrate

Returns:

  • (Integer)

    Number of records migrated



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
42
# File 'lib/active_version/migrators/audited.rb', line 13

def migrate(model_class, options = {})
  return 0 unless model_class.respond_to?(:audited?)

  audit_class = model_class.audit_class
  return 0 unless audit_class

  old_audits = source_audits(model_class)
  count = 0

  if old_audits.respond_to?(:find_each)
    old_audits.find_each do |old_audit|
      count += 1
      next if options[:dry_run]

      audit_data = convert_audit(old_audit, model_class)
      create_audit(nil, audit_data, audit_class)
    end
  else
    # Handle array case (from mocked source_audits in tests)
    old_audits.each do |old_audit|
      count += 1
      next if options[:dry_run]

      audit_data = convert_audit(old_audit, model_class)
      create_audit(nil, audit_data, audit_class)
    end
  end

  count
end