Guest User

Untitled

a guest
Jun 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. desc "Export specific models to yaml"
  2. task :export => :environment do
  3. require 'fileutils'
  4. begin
  5. # Returns only the specified models, unless they're not defined
  6. models = ENV['MODELS'].split(',').map { |m| m.pluralize.classify.constantize }
  7. rescue
  8. # Returns all models minus the subclasses of Page
  9. models = (ActiveRecord::Base.send(:subclasses) - Page.send(:subclasses)).map { |m| m.name.constantize }
  10. end
  11.  
  12. hash = {}
  13. models.each do |model|
  14. hash[model.name.pluralize] = model.find(:all).inject({}) { |h, record| h[record.id.to_i] = record.attributes; h }
  15. end
  16.  
  17. mkdir_p("#{RAILS_ROOT}/db/templates/", :verbose => false)
  18. target = File.new("#{RAILS_ROOT}/db/templates/export.yml", "w")
  19. target.write(hash.to_yaml)
  20. target.close
  21. end
Add Comment
Please, Sign In to add comment