简体   繁体   English

alias_method_chain已弃用 - Rails 5升级

[英]alias_method_chain is deprecated - Rails 5 upgrade

I'm updating my rails app and I need to refactor a method that is using alias_method_chain because it is deprecated. 我正在更新我的rails应用程序,我需要重构一个使用alias_method_chain的方法,因为它已被弃用。 The message says to use module#prepend as recommended by Rails 5. Here is the helper that I'm trying to refactor: 消息说使用Rails 5推荐的模块#prepend。这是我正在尝试重构的帮助器:

 module ActiveSupport
  module NumberHelper
    def number_to_delimited_with_unicode_infinity(number, options = {})
      result = number_to_delimited_without_unicode_infinity(number, options)
      result.sub(/^Infinity$/, "∞")
    end
    alias_method_chain :number_to_delimited, :unicode_infinity
  end
end

If anyone know how I can refactor with super or some other way let me know thank you! 如果有人知道我怎么能用super或其他方式重构让我知道谢谢!

This works for me. 这适合我。 I don't know why they used alias_method_chain to begin with but this gets rid of the deprecation warning with the same functionality. 我不知道为什么他们开始使用alias_method_chain ,但这消除了具有相同功能的弃用警告。

module ActiveSupport
  module NumberHelper
    def number_to_delimited(number, options = {})
      number.to_s.sub(/^Infinity$/, "∞")
    end
  end
end

In your case this solution seems to be fine. 在你的情况下,这个解决方案似乎很好。 If you have to have a monkey patch with reference to original method then you can do it creating an alias before patching: 如果你必须有一个参考原始方法的猴子补丁,那么你可以在修补之前创建一个别名:

module ActiveSupport
  module NumberHelper

    # create alias to original method
    alias :original_number_to_delimited :number_to_delimited

    def number_to_delimited(number, options = {})
      result = original_number_to_delimited(number, options)
      result.sub(/^Infinity$/, "∞")
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM