class ARLeftOverGuardian::Instance

Instance of left over guardian, which keeps track of all models counts before test and after test, and expects them to be the same

Attributes

counts[R]
models[R]

Public Class Methods

new(models) click to toggle source
# File lib/ar_left_over_guardian.rb, line 20
def initialize(models)
  @models = models.reject(&method(:abstract?))
  @counts = current_counts(false)
  self
end

Public Instance Methods

verify() click to toggle source
# File lib/ar_left_over_guardian.rb, line 29
def verify
  left_overs = get_left_overs
  return if left_overs.empty?
  model_list = left_overs.map { |model| "- #{model}" }.join("\n")
  raise DidntCleanup.new("Following models has left over records:\n#{model_list}")
end

Private Instance Methods

abstract?(model) click to toggle source
# File lib/ar_left_over_guardian.rb, line 58
def abstract?(model)
  model.respond_to?(:abstract_class?) && model.abstract_class?
end
current_counts(reset = true) click to toggle source
# File lib/ar_left_over_guardian.rb, line 51
def current_counts(reset = true)
  models.map { |model|
    reset_model(model, reset).count
  }
end
get_left_overs() click to toggle source
# File lib/ar_left_over_guardian.rb, line 41
def get_left_overs
  current_counts.each_with_index.inject([]) do |acc, (count, index)|
    if count != counts[index]
      acc << models[index]
    end
    acc
  end
end
reset_model(model, reset) click to toggle source
# File lib/ar_left_over_guardian.rb, line 63
def reset_model(model, reset)
  return model unless reset
  RSpec::Mocks.space.proxy_for(model).reset if defined?(RSpec::Mocks)
  model
end