Skip to content

Instantly share code, notes, and snippets.

@dirkkelly
Created November 4, 2010 15:31
Show Gist options
  • Save dirkkelly/662624 to your computer and use it in GitHub Desktop.
Save dirkkelly/662624 to your computer and use it in GitHub Desktop.
Radiant (Rails) Exporter
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
@dirkkelly
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment