Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tworgy/active_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dump_to_yaml(dir = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/tworgy/active_record.rb', line 6

def self.dump_to_yaml(dir = nil)
  dir = RAILS_ROOT + (dir || "/db/backup/")
  FileUtils.mkdir_p(dir)
  file = dir + "#{self.table_name}.yml"

  ActiveRecord::Base.transaction do         
    File.open(file, 'w+') do |f| 
      YAML.dump self.find(:all, :order => 'id').collect(&:attributes), f 
    end      
  end
end

.load_from_yaml(dir = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tworgy/active_record.rb', line 18

def self.load_from_yaml(dir = nil)
  dir = RAILS_ROOT + (dir || "/db/backup/")
  file = dir + "#{self.table_name}.yml"

  ActiveRecord::Base.transaction do         
    self.delete_all
    YAML.load_file(file).each do |fixture|
      ActiveRecord::Base.connection.execute "INSERT INTO #{self.table_name} (#{fixture.keys.join(",")}) VALUES (#{fixture.values.collect { |value| ActiveRecord::Base.connection.quote(value) }.join(",")})", 'Fixture Insert'
    end        
  end
end

.to_fixtureObject



30
31
32
33
34
35
# File 'lib/tworgy/active_record.rb', line 30

def self.to_fixture
  write_file(File.expand_path("test/fixtures/#{table_name}.yml", RAILS_ROOT), 
    self.find(:all).inject({}) { |hsh, record| 
      hsh.merge("record_#{record.id}" => record.attributes) 
    }.to_yaml)
end

.write_file(path, content) ⇒ Object



37
38
39
40
41
# File 'lib/tworgy/active_record.rb', line 37

def self.write_file(path, content)
  f = File.new(path, "w+")
  f.puts content
  f.close
end

Instance Method Details

#helpersObject



2
3
4
# File 'lib/tworgy/active_record.rb', line 2

def helpers
  ActionController::Base.helpers
end