Class: ActiveRecord::Base

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

Instance Method Summary collapse

Instance Method Details

#to_fixturesObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fixtures/fixtures.rb', line 12

def to_fixtures
  hash = self.to_hash
  md5 = Digest::MD5.hexdigest(hash.to_s)
  string = "#{md5}:\n"
  hash.each do |key,value|
    value.gsub!("\n","\n"+" "*4) if value.class == String
    string += "  #{key.to_s}: #{value}\n" unless value.nil?
  end
  string += "\n"
  table_name = self.class.table_name
  file_name = table_name + ".yml"
  File.open("test/fixtures/#{file_name}", 'a') do |out|
    out.write(string)
  end
end

#to_hashObject



7
8
9
10
# File 'lib/fixtures/fixtures.rb', line 7

def to_hash
  hash = {}; self.attributes.each { |k,v| hash[k.to_sym] = v }
  return hash
end