Polymorphism to the Rescue
12 Jan 2011
The video processing service we used at spokt got acquired by another company and the API changed. I created a code branch and started hacking away. I have a model "VideoAttachment" which handles sending the encoding request and dealing with the asynchronous completion notification.
It took some time to get all the ducks in a row and just as I was doing some final testing I realized that if I were to deploy my new code all the previously uploaded videos would no longer work. My head started filling with all the conditional logic I'd need to add... like to every method. Skin crawling. I pulled the previous version of "VideoAttachment" out of the master branch to see what would need to happen... it wasn't going to be pretty.
Luckily I had a crazy idea that turned out to be a very good one: use polymorphism to handle the conditional logic. I saved the old video_attachment.rb file as legacy_video_attachment.rb, changed the class name then updated the 'type' attribute in the database to LegacyVideoAttachment. And done!
Polymorphism, I think I love you! (You too STI, most of the time).