Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- desc "Export specific models to yaml"
- task :export => :environment do
- require 'fileutils'
- begin
- # Returns only the specified models, unless they're not defined
- models = ENV['MODELS'].split(',').map { |m| m.pluralize.classify.constantize }
- rescue
- # Returns all models minus the subclasses of Page
- models = (ActiveRecord::Base.send(:subclasses) - Page.send(:subclasses)).map { |m| m.name.constantize }
- end
- hash = {}
- models.each do |model|
- hash[model.name.pluralize] = model.find(:all).inject({}) { |h, record| h[record.id.to_i] = record.attributes; h }
- end
- mkdir_p("#{RAILS_ROOT}/db/templates/", :verbose => false)
- target = File.new("#{RAILS_ROOT}/db/templates/export.yml", "w")
- target.write(hash.to_yaml)
- target.close
- end
Add Comment
Please, Sign In to add comment