From 6bcc03834a58f869e986d5ae2c13a698654f57a4 Mon Sep 17 00:00:00 2001 From: Nicolas Florentin Date: Tue, 5 Apr 2016 10:08:29 +0200 Subject: [PATCH] adds a method to ActiveRecord::Base to dump a model to fixture --- config/initializers/active_record_base.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 config/initializers/active_record_base.rb diff --git a/config/initializers/active_record_base.rb b/config/initializers/active_record_base.rb new file mode 100644 index 000000000..f54d93090 --- /dev/null +++ b/config/initializers/active_record_base.rb @@ -0,0 +1,9 @@ +ActiveRecord::Base.class_eval do + def dump_fixture + fixture_file = "#{Rails.root}/test/fixtures/#{self.class.table_name}.yml" + File.open(fixture_file, "a") do |f| + f.puts({ "#{self.class.table_name.singularize}_#{id}" => attributes }. + to_yaml.sub!(/---\s?/, "\n")) + end + end +end