Created
February 6, 2018 21:37
-
-
Save phongleland/37b5c968be24cae32fcb88aee99c989a to your computer and use it in GitHub Desktop.
Convert ActiveRecord objects into fixture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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