Skip to content

Instantly share code, notes, and snippets.

@phongleland
Created February 6, 2018 21:37
Show Gist options
  • Save phongleland/37b5c968be24cae32fcb88aee99c989a to your computer and use it in GitHub Desktop.
Save phongleland/37b5c968be24cae32fcb88aee99c989a to your computer and use it in GitHub Desktop.
Convert ActiveRecord objects into fixture
class FixtureMaker
def initialize(obj)
@obj = obj
end
def attributes
attributes ||= @obj.attributes.delete_if{|k,v| v.blank?}
end
def fixture_path
@fixture_path ||= SupportApi::Engine.root.to_s + '/test/fixtures/support_api/' + self.table_name + '.yml'
end
def generate
File.open(fixture_path, 'a') do |f|
output = {self.table_name+"_#{@obj.id}" => attributes}
f << output.to_yaml.gsub(/^--- \n/,'').gsub(/^---\n/,'') + "\n"
end
end
def obj_class
@obj_class ||= @obj.class
end
def table_name
@table_name ||= obj_class.table_name
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment