如何覆盖rails应用程序中的gem的类方法? [英] How to override a class method of the gem in rails Application?

查看:206
本文介绍了如何覆盖rails应用程序中的gem的类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

覆盖rails应用程序中gem的类方法的最佳实践? 。我需要重写gem的find方法的行为。



以下是宝石中的代码



<$
class显示
attr_accessor:base
def find(id,options = {})
detailed = convert_to_number(options.delete (:详细))
选项[:详细] =详细除非detailed.nil?
base.send:get,/ get_youtube,options.merge(:youtube_id => id)
结束$ b $结束
结束

如何在我的YoutubeSearch控制器的Rails应用程序中重写上述查找方法?

  def find(id,options = {})
// Code here
end


解决方案

config /使用以下代码初始化目录:

  Youtube :: display.class_eval do 
def find(id,options = {})
//代码
end
end


Best practice to Override a class method of the gem in rails Application ? . I need to override the behaviour of the find method of a gem.

following is the code in the gem

module Youtube
  class display
    attr_accessor :base
      def find(id, options = {})
        detailed = convert_to_number(options.delete(:detailed))
        options[:detailed] = detailed unless detailed.nil?
        base.send :get, "/get_youtube", options.merge(:youtube_id => id)
     end
  end
end

How do i override the above find method in my YoutubeSearch Controller of Rails Application ?

   def find(id, options = {})
    //Code here     
   end

解决方案

Create a .rb file in config/initializers directory with the following code:

Youtube::display.class_eval do
   def find(id, options = {})
    //Code here     
   end
end

这篇关于如何覆盖rails应用程序中的gem的类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆