Rails gem class override implementation load order

239 Views Asked by At

I want to replace a particular ruby file in a gem I am using.

The gem I am using is private_pub : https://github.com/ryanb/private_pub and the file I am looking to replace is : https://github.com/ryanb/private_pub/blob/master/lib/private_pub/faye_extension.rb

So what I decided to do was, in my rails project add a new file in config/initializers called faye_extension.rb which contains the following code:

PrivatePub::FayeExtension
puts "Overide loaded"
module PrivatePub
  # This class is an extension for the Faye::RackAdapter.
  # It is used inside of PrivatePub.faye_app.
  class FayeExtension
    # Callback to handle incoming Faye messages. This authenticates both
    # subscribe and publish calls.
    def incoming(message, callback)
    puts "MY MEGA CHANGE"
    logger.debug "inside incoming override"
      if message["channel"] == "/meta/subscribe"
           puts "subscription request received"
        authenticate_subscribe(message)
      elsif message["channel"] !~ %r{^/meta/}
        puts "publish request received"
        authenticate_publish(message)
      end
      callback.call(message)
    end

  private

    # Ensure the subscription signature is correct and that it has not expired.
    def authenticate_subscribe(message)
      subscription = PrivatePub.subscription(:channel => message["subscription"], :timestamp => message["ext"]["private_pub_timestamp"])
      if message["ext"]["private_pub_signature"] != subscription[:signature]
        message["error"] = "Incorrect signature."
      elsif PrivatePub.signature_expired? message["ext"]["private_pub_timestamp"].to_i
        message["error"] = "Signature has expired."
      end
    end

    # Ensures the secret token is correct before publishing.
    def authenticate_publish(message)
      if PrivatePub.config[:secret_token].nil?
        raise Error, "No secret_token config set, ensure private_pub.yml is loaded properly."
      elsif message["ext"]["private_pub_token"] != PrivatePub.config[:secret_token]
        message["error"] = "Incorrect token."
      else
        message["ext"]["private_pub_token"] = nil
      end
    end
  end
end

The reason I include the 'PrivatePub::FayeExtension' at the top was to force autoload, so that FayeExtension class would override the gem's implementation. However when I execute the code path which would drive this logic,none of my added puts statements are showing in the logs. It is important to mention I do see 'Overide loaded' however I believe this happens, and then after, somehow, the gem's implementation is used. This leads me to believe that the gem's faye_extension.rb is being used instead of my version.

How can I enforce my version being used instead of the gem's version?

0

There are 0 best solutions below