如何在 Ruby on Rails 中进行猴子补丁?

发布于 2024-09-25 18:20:43 字数 514 浏览 0 评论 0原文

让我们使用一个现实世界的例子。

我想猴子修补 WillPaginate::LinkRenderer.to_html 方法。

到目前为止,我已经尝试过:

  1. 在文件夹中创建了一个文件:lib/monkeys/will_paginate_nohtml.rb
  2. 在 config/environments.rb 中添加:在文件末尾需要 'monkeys/will_paginate_nohtml'
  3. 在该文件内,这是我的代码:

e

module Monkeys::WillPaginateNohtml
  def to_html
    debugger
    super
  end
end

WillPaginate::LinkRenderer.send(:include, Monkeys::WillPaginateNohtml)

但是不知何故,调试器没有通过。看起来补丁失败了。

任何帮助将不胜感激,谢谢!

Lets use a real world example.

I want to monkey patch WillPaginate::LinkRenderer.to_html method.

So far I have tried:

  1. Created a file in folder: lib/monkeys/will_paginate_nohtml.rb
  2. Added in config/environments.rb: require 'monkeys/will_paginate_nohtml' at the end of the file
  3. Inside that file, this was my code:

e

module Monkeys::WillPaginateNohtml
  def to_html
    debugger
    super
  end
end

WillPaginate::LinkRenderer.send(:include, Monkeys::WillPaginateNohtml)

But somehow, debugger doesn't get passed through. Looks like the patching failed.

Any help would be appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丑丑阿 2024-10-02 18:20:43

那么这个呢:-) @shingana、@kandadaboggu 的解决方案将不起作用,因为这里没有“超级”。您想要调用原始版本而不是超级版本。

module WillPaginate
  class LinkRenderer
    alias_method :to_html_original, :to_html
    def to_html
      debugger
      to_html_original
    end
  end
end

And what about this one :-) Solutions by @shingana, @kandadaboggu will not work as there is no "super" here. You want to call original version not the super version.

module WillPaginate
  class LinkRenderer
    alias_method :to_html_original, :to_html
    def to_html
      debugger
      to_html_original
    end
  end
end
暖风昔人 2024-10-02 18:20:43

你的问题的标题具有误导性。坦率地说,我认为您可能只是想自定义 will_paginate 页面列表结构,这可以以不同的方式完成。

因此,在您的情况下,正确的方法是扩展渲染器。例如,从初始化程序加载以下内容(通过 config/initializers):

class CustomPaginationRenderer < WillPaginate::LinkRenderer

  def to_html
    # Your custom code, debugger etc
  end

end

然后,要让您的应用程序使用此渲染器,请将以下内容添加到您的 config/environment.rb 文件中:

WillPaginate::ViewHelpers.pagination_options[:renderer] = 'CustomPaginationRenderer'

The title of your question is misleading. Frankly, I think you probably just want to customize the will_paginate page list structure, which can be done differently.

So in your case the right way is to extend the renderer. For example load the following from an initializer (via config/initializers):

class CustomPaginationRenderer < WillPaginate::LinkRenderer

  def to_html
    # Your custom code, debugger etc
  end

end

Then, to have your application use this renderer add the following to your config/environment.rb file:

WillPaginate::ViewHelpers.pagination_options[:renderer] = 'CustomPaginationRenderer'
深爱成瘾 2024-10-02 18:20:43

我认为你需要打开该方法

module WillPaginate
  class LinkRenderer
    def to_html
      debugger
      super
    end
  end
end

I think you need open the method

module WillPaginate
  class LinkRenderer
    def to_html
      debugger
      super
    end
  end
end
ゃ人海孤独症 2024-10-02 18:20:43

试试这个:

module LinkRendererWrapper
  def to_html
    debugger
    super
  end
end

WillPaginate::LinkRenderer.prepend(LinkRendererWrapper)

Try this:

module LinkRendererWrapper
  def to_html
    debugger
    super
  end
end

WillPaginate::LinkRenderer.prepend(LinkRendererWrapper)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文