Module: DataJanitor

Defined in:
lib/data_janitor.rb,
lib/data_janitor/version.rb,
lib/data_janitor/data_janitor.rb,
lib/data_janitor/audit_validatable.rb,
lib/data_janitor/universal_validator.rb

Defined Under Namespace

Modules: AuditValidatable, UniversalValidator Classes: MyRailtie

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.audit(output_file, verbose, unscoped, options) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/data_janitor/data_janitor.rb', line 2

def self.audit(output_file, verbose, unscoped, options)
  output = {}
  all_models.each do |ar_model|
    begin
      audit_model ar_model, output, verbose, unscoped, options
    rescue ActiveRecord::StatementInvalid # used to catch HABTM and schema migration. Only care about real Models
      puts "skipping #{ar_model}"
    end
  end

  File.write(output_file, output.to_json)

  puts "Wrote results to #{output_file}"
end

.audit_model(model, output = {}, verbose = false, unscoped = false, options = 'no-type-check') ⇒ Object



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
43
44
45
# File 'lib/data_janitor/data_janitor.rb', line 17

def self.audit_model(model, output = {}, verbose = false, unscoped = false, options = 'no-type-check')
  total = 0
  failed = 0
  puts "Validating: #{model.name}"
  output[model.name] = {}

  model = model.unscoped if unscoped
  model.include(DataJanitor::UniversalValidator)
  model.validate do |record|
    record.validate_field_values options
  end

  model.find_each do |rec|
    if rec.invalid?(:dj_audit)
      rec.errors.to_h.each_pair do |attribute, error_message|
        output[model.name][attribute] ||= {}
        output[model.name][attribute][error_message] ||= []
        output[model.name][attribute][error_message] << rec.id
      end

      failed += 1
    end

    total += 1
  end

  puts output.to_json if verbose
  puts "Completed #{total} records with #{failed} failures"
end

.cleanseObject



47
48
49
50
51
# File 'lib/data_janitor/data_janitor.rb', line 47

def self.cleanse
  all_models.each do |ar_model|
    cleanse_model! ar_model
  end
end

.cleanse_model(model) ⇒ Object



53
54
55
# File 'lib/data_janitor/data_janitor.rb', line 53

def self.cleanse_model(model)
  cleanse_model! model.constantize
end