Class: Exporter

Inherits:
Object
  • Object
show all
Extended by:
Loader
Defined in:
lib/exporter.rb

Class Method Summary collapse

Methods included from Loader

join_tables_from_database, models_from_database

Class Method Details

.exportObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/exporter.rb', line 7

def self.export
  hash = {'name' => 'Last export', 'description' => "Backup of the database as of #{Time.now.to_s(:rfc822)}"}
  hash['records'] = {}
  self.models.each do |klass|
    recs = klass.find(:all)
    if recs
      hash['records'][klass.name.pluralize] = recs.inject({}) { |h, record| h[record.id.to_s] = record.attributes; h }
    end
  end
  self.join_tables.each do |table|
    recs =  ActiveRecord::Base.connection.select_all("SELECT * FROM #{table}")
    if recs
      i = 0
      hash['records'][table.camelize] = recs.inject({}) { |h, record| h[i += 1] = record; h }
    end
  end
  hash.to_yaml
end