Skip to content

Instantly share code, notes, and snippets.

@jonathandean
Created July 12, 2021 21:10
Show Gist options
  • Save jonathandean/585df53a181dbe983b2885d8a7dd00a0 to your computer and use it in GitHub Desktop.
Save jonathandean/585df53a181dbe983b2885d8a7dd00a0 to your computer and use it in GitHub Desktop.
Adds dump_all_fixtures and dump_fixture to ApplicationRecord
class ApplicationRecord < ActiveRecord::Base
# ...
def self.dump_all_fixtures
self.all.map(&:dump_fixture)
end
def dump_fixture
fixture_file = "#{Rails.root}/spec/fixtures/#{self.class.table_name}.yml"
File.open(fixture_file, "a+") do |f|
f.puts({ "#{self.class.table_name.singularize}_#{id}" => attributes.reject{|key| %w(id created_at updated_at).include?(key)} }.
to_yaml.sub!(/---\s?/, "\n"))
end
end
# ...
end
# https://stackoverflow.com/questions/6591722/how-to-generate-fixtures-based-on-my-development-database
# https://stackoverflow.com/questions/49234751/how-to-read-ruby-object-in-yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment