Created
November 4, 2010 15:31
-
-
Save dirkkelly/662624 to your computer and use it in GitHub Desktop.
Radiant (Rails) Exporter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
writing file could be a module method where you pass the file name (specified in the environment)
Radiant::Export should accept an array of constantized and the controller should pass that data (could drop 13-15)
Maybe I'll suggest that.