How to access rails's data in gem?

249 views Asked by At

I use faye and private pub as push server. I need to update user's status to online when he subscribes to channel.

I have forked private_pub gem and changed some code, my application sends user_id, but I don't know how to find user and update his status, because subscription event fires in the gem.

module PrivatePub
  # This class is an extension for the Faye::RackAdapter.
  # It is used inside of PrivatePub.faye_app.
  class FayeExtension

    def incoming(message, callback)
      p "user_id = #{message['ext']['user_id']}"
      if message["channel"] == "/meta/subscribe"
        authenticate_subscribe(message)
        # user = User.find(message['ext']['user_id'])
        # user.status = 'online'
        # user.save
      elsif message["channel"] !~ %r{^/meta/}
        authenticate_publish(message)
      end
      callback.call(message)
    end
  end
end

I can get user_id but I can't use User model in a gem. Which is the best way to do it?

1

There are 1 answers

0
Avanes On BEST ANSWER

Try to add environment to private_pub.rb. For ex.

require File.expand_path("../config/environment", __FILE__)