简体   繁体   English

如何将特定于应用程序的代码添加到Rails的插件中?

[英]How do I add application specific code to Rails' plugin?

I am using facebooker in one of my applications. 我在其中一个应用程序中使用了facebooker。 I want to add some application specific methods to various classes in it? 我想在各种类中添加一些特定于应用程序的方法吗? eg 例如

In facebooker/lib/facebooker/models/user.rb facebooker/lib/facebooker/models/user.rb

module Facebooker
  class User
    # I want to add my methods here. for e.g my_method
  end
end

I can not directly put my_method in the plugin itself since I also want to access my models from there. 我不能直接将my_method放在插件本身中,因为我也想从那里访问我的模型。 Not defining the methods in plugin code itself will also be helpful when I upgrade. 升级时,未在插件代码本身中定义方法也将很有帮助。

Somewhere in the file you wish to use my_method, type in, include, or require the following code: 在文件中您希望使用my_method的某个位置,键入,包含或要求以下代码:

module Facebooker
  class User
    def my_method
        #whatever
    end
  end
end

Ruby will then add my_method to Facebooker's User class before you need to call it. 然后,在需要调用Ruby之前,Ruby将my_method添加到Facebooker的User类。

尝试在您正在使用的模型内部猴子贴上facebooker插件,这样您的编辑只会影响您希望编辑在其中运行的模型内部的代码

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

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