Class: ConsistencyFail::Enforcer

Inherits:
Object
  • Object
show all
Defined in:
lib/consistency_fail/enforcer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enforce!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/consistency_fail/enforcer.rb', line 13

def self.enforce!
  models = ConsistencyFail::Models.new($LOAD_PATH)
  models.preload_all

  introspectors = [ConsistencyFail::Introspectors::ValidatesUniquenessOf.new,
                   ConsistencyFail::Introspectors::HasOne.new,
                   ConsistencyFail::Introspectors::Polymorphic.new]

  problem_models_exist = models.all.detect do |model|
    introspectors.any? {|i| !i.missing_indexes(model).empty?}
  end

  if problem_models_exist
    mega_fail!
  end
end

.mega_fail!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/consistency_fail/enforcer.rb', line 30

def self.mega_fail!
  ActiveRecord::Base.class_eval do
    class << self
      def panic
        raise "You've got missing indexes! Run `consistency_fail` to find and fix them."
      end

      def find(*arguments)
        panic
      end

      alias :first :find
      alias :last :find
      alias :count :find
    end

    def save(*arguments)
      self.class.panic
    end

    def save!(*arguments)
      self.class.panic
    end
  end
end

Instance Method Details

#problems(models, introspector) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/consistency_fail/enforcer.rb', line 5

def problems(models, introspector)
  problems = models.map do |m|
    [m, introspector.missing_indexes(m)]
  end.reject do |m, indexes|
    indexes.empty?
  end
end