At the Indy.rb Code Review Session (11-30-11), Dan Doezema< asked if there was an easy way to rename a resource>, or the related model, controller, view directory, tests, and potentially factory or fixture. We had a good laugh after someone mentioned that being one of the features that lure people to try Smalltalk, or, perhaps, the Ruby VM MagLev. The joke is that doing either of those two things is very likely a road to compromised mental health.
A Christmas Miracle!
This renaming issue was on still bouncing around inside my mind as I began reading the first day of this ZSH-focused advent calendar. Hey, look at that! Between day one and day two, one can piece together a nice line for renaming the files related to a resource in Rails.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Santa’s Little Helper
That is only half of the problem, though. In fact, it’s arguably the easier-solved of the two halves, whether you use Bash, ZSH, or something completely different. How are we going to edit the content of the files? After the above command, we’re still stuck with this:
More command-line work to the rescue!
“` $ git ls-files –other –exclude-standard | xargs sed -i ” ‘s/OldName/NewName/’ $ vim app/models/old_name.rb class NewName
Great! Two lines of terminal work and we’ve reached our goal!
This isn’t perfect. You still might have to change other references to the class, such as a
hasmany :olditems line in another class, or something similar. If you have a better method you’ve been employing (short of installing an IDE), I would love to hear it!