2020-05-19 11:45:51 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-05 10:08:29 +02:00
|
|
|
ActiveRecord::Base.class_eval do
|
|
|
|
def dump_fixture
|
|
|
|
fixture_file = "#{Rails.root}/test/fixtures/#{self.class.table_name}.yml"
|
2020-05-19 11:45:51 +02:00
|
|
|
File.open(fixture_file, 'a') do |f|
|
2016-04-05 10:08:29 +02:00
|
|
|
f.puts({ "#{self.class.table_name.singularize}_#{id}" => attributes }.
|
|
|
|
to_yaml.sub!(/---\s?/, "\n"))
|
|
|
|
end
|
|
|
|
end
|
2016-04-05 11:02:49 +02:00
|
|
|
|
|
|
|
def self.dump_fixtures
|
2020-05-19 11:45:51 +02:00
|
|
|
fixture_file = "#{Rails.root}/test/fixtures/#{table_name}.yml"
|
|
|
|
mode = (File.exist?(fixture_file) ? 'a' : 'w')
|
2016-04-05 12:05:40 +02:00
|
|
|
File.open(fixture_file, mode) do |f|
|
2016-04-05 12:28:22 +02:00
|
|
|
|
2020-05-19 11:45:51 +02:00
|
|
|
if attribute_names.include?('id')
|
|
|
|
all.each do |instance|
|
|
|
|
f.puts({ "#{table_name.singularize}_#{instance.id}" => instance.attributes }.to_yaml.sub!(/---\s?/, "\n"))
|
2016-04-05 12:28:22 +02:00
|
|
|
end
|
|
|
|
else
|
2020-05-19 11:45:51 +02:00
|
|
|
all.each_with_index do |instance, i|
|
|
|
|
f.puts({ "#{table_name.singularize}_#{i}" => instance.attributes }.to_yaml.sub!(/---\s?/, "\n"))
|
2016-04-05 12:28:22 +02:00
|
|
|
end
|
2016-04-05 11:02:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-05 10:08:29 +02:00
|
|
|
end
|