Search code examples
ruby-on-railsmethodsrakemonkeypatching

Rails 2.3+Rake: monkey patch method of class used in rake task


How do you monkey patch a method of a class that is used in a rake task? In particular I want to redefine the method Synthesis::AssetPackage#compress_js of the asset:packager plugin. I tried to place the redefinition in the Rakefile in RAILS_ROOT, but that didn't work. I'd rather not change the plugin directly.


Solution

  • You have to redefine it after Synthesis gets loaded. I would guess that your app's rake tasks (Rails.root/lib/tasks/*.rake) get loaded after all the plugins, so you could try moving your monkey-patch into there.

    In general, you shouldn't modify a Rails app's Rakefile directly anyway; put any custom rake tasks or other customizations into lib/tasks/*.rake.