Rails 2.3+Rake:rake 任务中使用的类的猴子补丁方法
如何猴子修补 rake 任务中使用的类的方法?特别是我想重新定义 asset:packager 插件的方法 Synthesis::AssetPackage#compress_js
。我尝试将重新定义放在 RAILS_ROOT
的 Rakefile
中,但这不起作用。我不想直接更改插件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在加载 Synthesis 后重新定义它。我猜你的应用程序的 rake 任务 (Rails.root/lib/tasks/*.rake) 在所有插件之后加载,因此你可以尝试将猴子补丁移到那里。
一般来说,无论如何,您都不应该直接修改 Rails 应用程序的 Rakefile;将任何自定义 rake 任务或其他自定义项放入 lib/tasks/*.rake 中。
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.