Adam Waite
Adam Waite

Reputation: 18855

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.

I want to add a single class to the ActiveMerchant Billing module:

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:

#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?

Thanks

EDIT

Putting it in the lib folder should work but my code requires some classes from the gem to inherit from, like:

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?

Upvotes: 1

Views: 3335

Answers (2)

house9
house9

Reputation: 20614

Add activemerchant to the Gemfile, bundle install

In config/application.rb make sure lib is included in the autoload paths

# 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)

restart rails

Upvotes: 7

Kashyap
Kashyap

Reputation: 4796

You might want to just fork the project on GitHub and add your changes to it. Even if it is just a single class. And then, in your Gemfile, do this:

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

Upvotes: 1

Related Questions