class DatabasePlumber::LeakFinder

Constants

IGNORED_AR_INTERNALS

Public Class Methods

inspect(options = {}) click to toggle source
# File lib/database_plumber/leak_finder.rb, line 8
def self.inspect(options = {})
  new(options).inspect
end
new(options) click to toggle source
# File lib/database_plumber/leak_finder.rb, line 12
def initialize(options)
  @ignored_models = (options[:ignored_models] || []) + IGNORED_AR_INTERNALS
  @ignored_adapters = options[:ignored_adapters] || []
  @model_thresholds = options[:model_thresholds] || {}
end

Public Instance Methods

inspect() click to toggle source
# File lib/database_plumber/leak_finder.rb, line 18
def inspect
  filtered_models.each_with_object({}) do |model, results|
    records = count_for(model)
    if records > threshold_for(model)
      results[model.to_s] = records
      mop_up(model)
    end
    results
  end
end

Private Instance Methods

count_for(model) click to toggle source
# File lib/database_plumber/leak_finder.rb, line 31
def count_for(model)
  return 0 if no_table?(model)
  model.count
rescue ActiveRecord::StatementInvalid
  raise InvalidModelError, "#{model} does not have a valid table definition"
end
excluded_model?(model) click to toggle source
# File lib/database_plumber/leak_finder.rb, line 54
def excluded_model?(model)
  @ignored_models.include?(model) || model.name =~ /^HABTM_/
end
filtered_models() click to toggle source
# File lib/database_plumber/leak_finder.rb, line 50
def filtered_models
  model_space.reject(&method(:excluded_model?))
end
model_space() click to toggle source
# File lib/database_plumber/leak_finder.rb, line 58
def model_space
  ActiveRecord::Base.descendants
end
mop_up(model) click to toggle source
# File lib/database_plumber/leak_finder.rb, line 42
def mop_up(model)
  model.destroy_all
end
no_table?(model) click to toggle source
# File lib/database_plumber/leak_finder.rb, line 46
def no_table?(model)
  model.abstract_class? || @ignored_adapters.include?(model.connection.adapter_name.downcase.to_sym)
end
threshold_for(model) click to toggle source
# File lib/database_plumber/leak_finder.rb, line 38
def threshold_for(model)
  @model_thresholds.key?(model) ? @model_thresholds[model].to_i : 0
end