Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Dump models into fixtures. (See related posts)

This snippet of code will look in app/models for all models, then dump their contents into the corresponding fixture file found in test/fixtures. At the moment, it can not differentiate between regular models and mailer models, so just remove the extension then run the script. Same goes for models you don't want dumped. I'm working on adding only and except as well.

require 'find'

namespace :db do
  namespace :fixtures do
    desc 'Dumps all models into fixtures.'
    task :dump => :environment do
      models = []
      Find.find(RAILS_ROOT + '/app/models') do |path|
        unless File.directory?(path) then models << path.match(/(\w+).rb/)[1] end
      end
  
      puts "Found models: " + models.join(', ')
      
      models.each do |m|
        puts "Dumping model: " + m
        model = m.capitalize.constantize
        entries = model.find(:all, :order => 'id ASC')
        
        formatted, increment, tab = '', 1, '  '
        entries.each do |a|
          formatted += m + '_' + increment.to_s + ':' + "\n"
          increment += 1
          
          a.attributes.each do |column, value|
            formatted += tab
            
            match = value.to_s.match(/\n/)
            if match
              formatted += column + ': |' + "\n"
              
              value.to_a.each do |v|
                formatted += tab + tab + v
              end
            else
              formatted += column + ': ' + value.to_s
            end
            
            formatted += "\n"
          end
                    
          formatted += "\n"
        end
      
        model_file = RAILS_ROOT + '/test/fixtures/' + m.pluralize + '.yml'
        
        File.exists?(model_file) ? File.delete(model_file) : nil
        File.open(model_file, 'w') {|f| f << formatted}
      end
    end
  end
end

Comments on this post

scharfie posts on Aug 27, 2007 at 09:43
How about this:
...
models.each do |m|
puts "Dumping model: " + m
model = m.capitalize.constantize
next if model.superclass == ActionMailer::Base
...
sergecpelletier posts on Nov 05, 2007 at 13:12
How about this for some more weird Model to table names (in my case status to statuses)

table_name = m.pluralize
model = table_name.classify.constantize
digitalronin posts on Jun 18, 2008 at 11:03
I found a couple of problems with this script. The use of 'capitalize' instead of 'camelize' meant that any models with more than one word in their names broke the script. Also, I wrapped the model loop in a begin...rescue...end block, so that it continues after any problems.

I've put those tweaks up on pastie: http://pastie.org/217271

Cheers

David

You need to create an account or log in to post comments to this site.


Click here to browse all 5137 code snippets

Related Posts