Last active
September 18, 2016 14:58
-
-
Save ivanilves/55e86e22b550730d1abc9650e6c6945d to your computer and use it in GitHub Desktop.
Dump fixtures from the current environment's database
This file contains hidden or 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
# | |
# Usage: | |
# * rake db:fixtures:dump to dump all models. | |
# * rake db:fixtures:dump MODELS="user billing_account" to dump only User and BillingAccount models. | |
# | |
namespace :db do | |
namespace :fixtures do | |
def all_models | |
ActiveRecord::Base.subclasses | |
end | |
def selected_models | |
return nil unless ENV['MODELS'] | |
selected_model_names = ENV['MODELS'].split(/\s+|,/).map(&:camelcase) | |
all_models.select { |m| selected_model_names.include?(m.name) } | |
end | |
desc "Dump fixtures from the current environment's database" | |
task dump: :environment do | |
Rails.application.eager_load! | |
models = selected_models || all_models | |
models.each do |model| | |
file_name = "#{Rails.root}/test/fixtures/#{model.name.downcase.pluralize}.yml" | |
puts "Dumping: #{model.name} => #{file_name}" | |
File.open(file_name, 'w') do |file| | |
data = {} | |
model.all.map(&:attributes).each do |record| | |
data["#{model.name.downcase}_#{record['id'].to_s}"] = record | |
end | |
file.write data.to_yaml | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! Better version available here:
https://gist.github.com/dimidd/1fee6c364d1bdcf435bf79f2011a3da8