Last Updated: February 25, 2016
·
1.67K
· allolex

Kickstart a FactoryGirl upgrade from old to new syntax

Upgrading from the old FactoryGirl syntax to the new syntax is a right pain. I searched for pre-existing solutions on the web, but couldn't find anything to help with the upgrade (perhaps for shame at the kludgy implementation such things tend to have).

This won't fix everything, but it will certainly get you started.

$ find spec/factories/ -name "*.rb" -exec perl -i -pe's!Factory\.define[\(\s](:\w+)[\)]?(.*?)do \|f\|!FactoryGirl.define do\n\n factory $1$2 do!;s/\bf\.//' {} \;

I very much enjoyed using Perl to "parse" the Ruby factories. Probably a little too much. It saves a few strokes.

The 'f.' substitution is specific to our implementation, although many do things exactly the same way.