Created
November 7, 2012 09:47
-
-
Save banyan/4030456 to your computer and use it in GitHub Desktop.
ActiveRecord extension to print Fabrication definition!
This file contains 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
# 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