简体   繁体   English

Rails-扩展红宝石宝石

[英]Rails - Extend a Ruby Gem

I want to add some functionality to the ActiveMerchant gem in order to test the PayPal Express gateway, a pull request has been attempted for this but was turned down on Github. 我想向ActiveMerchant gem添加一些功能以测试PayPal Express网关,对此已经尝试了拉取请求,但在Github上被拒绝了。

I want to add a single class to the ActiveMerchant Billing module: 我想向ActiveMerchant计费模块添加一个类:

module ActiveMerchant #:nodoc:
  module Billing #:nodoc:
    class PaypalBogusGateway < BogusGateway

      # some codes here

    end
  end
end

I have done this successfully by downloading and pulling the gem into my project locally and trhowing my new file in there: 我已经成功地做到了这一点,方法是下载gem并将其放入本地项目,然后在其中查找新文件:

#Gemfile
gem 'activemerchant', '1.34.1', path: "vendor/gems/activemerchant-1.34.1", require: 'active_merchant'

But of course, that's not the best idea because I'll have to manually pull any updates if I want them. 但这当然不是最好的主意,因为如果需要,我将不得不手动拉出任何更新。

Is there any way I can add the class to their module using their gem that's been pulled from the RubyGems source? 有什么办法可以使用从RubyGems源中提取的gem将类添加到其模块中?

Thanks 谢谢

EDIT 编辑

Putting it in the lib folder should work but my code requires some classes from the gem to inherit from, like: 将其放在lib文件夹中应该可以工作,但是我的代码需要gem继承一些类,例如:

require File.dirname(__FILE__) + '/paypal/paypal_common_api'
require File.dirname(__FILE__) + '/paypal/paypal_express_response'
require File.dirname(__FILE__) + '/paypal_express_common'

replacing File.dirname( FILE ) with wherever the gem is installed... This will be different across server environments right? 在安装了gem的任何地方替换File.dirname( FILE )...这在服务器环境中会有所不同,对吗?

Add activemerchant to the Gemfile, bundle install 将activemerchant添加到Gemfile,捆绑安装

In config/application.rb make sure lib is included in the autoload paths config/application.rb确保lib包含在自动加载路径中

# need to uncomment or add this to the configuration
config.autoload_paths += %W(#{config.root}/lib)

place your class in a file using nested directories to match the modules 使用嵌套目录将您的课程放在文件中以匹配模块

# lib/active_merchant/billing/paypal_bogus_gateway.rb

do NOT include any require statements in your bogus gateway, rails (via bundler should require everything from the Gemfile) 不要在伪造的网关,rails中包含任何require语句(通过捆绑程序应要求Gemfile中的所有内容)

restart rails 重新启动滑轨

You might want to just fork the project on GitHub and add your changes to it. 您可能只想在GitHub上创建项目,然后将更改添加到它。 Even if it is just a single class. 即使只是一个类。 And then, in your Gemfile , do this: 然后,在您的Gemfile ,执行以下操作:

gem "active_merchant", :git => "git://github.com/<your-user-name-here>/active_merchant.git"

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

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