Module: ActiveRecord

Defined in:
lib/raicoto/activerecord/inspection.rb

Class Method Summary collapse

Class Method Details

.countsObject



25
26
27
28
29
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
55
56
# File 'lib/raicoto/activerecord/inspection.rb', line 25

def self.counts
  klasses = []
  without_logging do
    tables = ActiveRecord::Base.connection.tables
    col = tables.map(&:length).max
    out =  "Current Record Counts\n"
    out << ("-"*col)+"------\n"
    total = 0
    tables.each do |table|
      next if table.match(/\Aschema_migrations\Z/)
      begin
        klass = table.singularize.camelize.constantize
        klasses << klass
        count = klass.count
        total += count
        out << "#{klass.name.ljust(col)} #{count.to_s.rjust(5)}"
        out << "\n"
      rescue
        sql = "Select count(*) from #{table}"
        result = ActiveRecord::Base.connection.execute(sql)
        count = result.first['count']
        total += count.to_i
        out << ".#{table.ljust(col-1)} #{count.rjust(5)}"
        out << "\n"
      end
    end
    out << ("-"*col)+"------\n"
    puts out
    puts "#{total} records in #{tables.length} tables."
  end
  klasses
end

.tablesObject



13
14
15
# File 'lib/raicoto/activerecord/inspection.rb', line 13

def self.tables
  ActiveRecord::Base.connection.tables
end

.without_loggingObject



17
18
19
20
21
22
23
# File 'lib/raicoto/activerecord/inspection.rb', line 17

def self.without_logging
  old_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil
  result = yield
  ActiveRecord::Base.logger = old_logger
  result
end