Skip to content

Instantly share code, notes, and snippets.

@banyan
Created November 7, 2012 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banyan/4030456 to your computer and use it in GitHub Desktop.
Save banyan/4030456 to your computer and use it in GitHub Desktop.
ActiveRecord extension to print Fabrication definition!
# This is port of https://gist.github.com/4029778 (ActiveRecord extension to print FactoryGirl definition!)
class ActiveRecord::Base
def to_fabrication
ignores = %w(id created_at updated_at)
array = []
array << "Fabricator(:#{self.class.model_name.underscore}) do"
width = attributes.map{ |a| a[0].size }.max.to_i
attributes.each do |key, value|
next if ignores.include?(key)
if key =~ /_id$/
array << " %s" % key.gsub(/_id$/, '')
else
array << " %-#{width}s %s" % [key, value.inspect]
end
end
array << "end"
array.join("\n")
end
alias_method :to_fb, :to_fabrication
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment