1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-16 01:24:03 +01:00
fab-manager/config/initializers/active_record_base.rb

27 lines
915 B
Ruby

# frozen_string_literal: true
ActiveRecord::Base.class_eval do
def dump_fixture
fixture_file = Rails.root.join("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
def self.dump_fixtures
fixture_file = Rails.root.join("test/fixtures/#{table_name}.yml")
mode = (File.exist?(fixture_file) ? 'a' : 'w')
File.open(fixture_file, mode) do |f|
if attribute_names.include?('id')
all.each do |instance|
f.puts({ "#{table_name.singularize}_#{instance.id}" => instance.attributes }.to_yaml.sub!(/---\s?/, "\n"))
end
else
all.each_with_index do |instance, i|
f.puts({ "#{table_name.singularize}_#{i}" => instance.attributes }.to_yaml.sub!(/---\s?/, "\n"))
end
end
end
end
end