Class: ActiveRecord::Base
Class Method Summary collapse
- .dump_to_yaml(dir = nil) ⇒ Object
- .load_from_yaml(dir = nil) ⇒ Object
- .to_fixture ⇒ Object
- .write_file(path, content) ⇒ Object
Instance Method Summary collapse
Class Method Details
.dump_to_yaml(dir = nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/tworgy/rails_ext.rb', line 26 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
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tworgy/rails_ext.rb', line 38 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_fixture ⇒ Object
50 51 52 53 54 55 |
# File 'lib/tworgy/rails_ext.rb', line 50 def self.to_fixture write_file(File.("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
57 58 59 60 61 |
# File 'lib/tworgy/rails_ext.rb', line 57 def self.write_file(path, content) f = File.new(path, "w+") f.puts content f.close end |
Instance Method Details
#helpers ⇒ Object
22 23 24 |
# File 'lib/tworgy/rails_ext.rb', line 22 def helpers ActionController::Base.helpers end |