Skip to content

Instantly share code, notes, and snippets.

@iorionda
Created August 20, 2013 01:56
Show Gist options
  • Save iorionda/6276293 to your computer and use it in GitHub Desktop.
Save iorionda/6276293 to your computer and use it in GitHub Desktop.
rails console で [email protected](main)> model.first.to_fg
class ActiveRecord::Base
def to_factory_girl
ignores = %w(id created_at updated_at)
array = []
array << "FactoryGirl.define do"
array << " factory :#{self.class.model_name.underscore} do"
attributes.each do |key, value|
next if ignores.include?(key)
if key =~ /_id$/
array << " association :#{key.gsub(/_id$/, '')}"
else
array << " #{key} #{value.inspect}"
end
end
array << " end"
array << "end"
array.join("\n")
end
alias_method :to_fg, :to_factory_girl
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment