How to make `destroy_without_callbacks` method available in a rack application under rails 2.3?

Monday, June 30, 2014

I'm using faye to build a realtime chat system in a rails 2.3 project . The faye server is packed in a rack application which starts with rails. It worked perfectly for weeks, then a new feature was added: notifying offline user when a new message was sent to him.


My solution is to add a server extension, when it receives a subscribe event, it marks the relative user as Online status and saves his faye clientId in DB. Then observing unsubscribe and disconnect event to mark the user which found by faye clientId as Offline.


So I need to use ActiveRecord model in the rack application , all the models in this project use a gem named acts_as_paranoid. However this gem is located in /vendor/plugins/acts_as_paranoid.


After a bunch of 'require' I managed to make it work in my local developing machine (thin and webrick for Development env and passenger+apache for Production env). However when the src was deplolyed to server, the faye server could not be started. Here's the full error message


/home/college-dev/rails/vendor/plugins/acts_as_paranoid/lib/caboose/acts/paranoid.rb:56:in alias_method': undefined methoddestroy_without_callbacks' for class UserChatTeam' (NameError) from /home/college-dev/rails/vendor/plugins/acts_as_paranoid/lib/caboose/acts/paranoid.rb:56:inacts_as_paranoid' from /home/college-dev/rails/app/models/user_chat_team.rb:2 from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require' from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:inrequire' from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in require' from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:inload_dependency' from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:640:in new_constants_in' from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:inload_dependency' from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in require' from faye.ru:14 from /usr/lib/ruby/gems/1.8/gems/rack-1.3.6/lib/rack/builder.rb:51:ininstance_eval' from /usr/lib/ruby/gems/1.8/gems/rack-1.3.6/lib/rack/builder.rb:51:in initialize' from faye.ru:1:innew' from faye.ru:1


And here is my raye.ru file:


require 'faye' require File.expand_path('../config/initializers/faye_token.rb', FILE) require 'active_record' require 'mysql' require 'yaml'



# using 'acts_as_paranoid' as a plugin in 'vendor/plugins'
RAILS_ENV = ENV['RACK_ENV']
load 'active_record/associations.rb'
require File.expand_path('../vendor/plugins/acts_as_paranoid/lib/caboose/acts/paranoid.rb', __FILE__)
require File.expand_path('../vendor/plugins/acts_as_paranoid/lib/caboose/acts/belongs_to_with_deleted_association.rb', __FILE__)
require File.expand_path('../vendor/plugins/acts_as_paranoid/lib/caboose/acts/has_many_through_without_deleted_association.rb', __FILE__)
require File.expand_path('../vendor/plugins/acts_as_paranoid/init.rb', __FILE__)
require File.expand_path('../app/models/user_chat_team.rb', __FILE__)

environment = ENV['RACK_ENV'] || 'production'
dbconfig = YAML.load(File.read('config/database.yml'))
ActiveRecord::Base.establish_connection(dbconfig[environment])

class ServerAuth
def incoming(message, callback)

if message['channel'] !~ %r{^/meta/}
if message['ext']['auth_token'] != FAYE_TOKEN
message['error'] = 'Invalid authentication token.'
else
message['ext'].delete('auth_token')
end
end

callback.call(message)
end
end

class MarkOnline
def incoming(message, callback)

if message['channel'] == '/meta/subscribe'
UserChatTeam.mark_online( message['data']['user_id'],
message['data']['chat_team_id'],
message['clientId'])
end

callback.call(message)
end

end


faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
faye_server.add_extension(ServerAuth.new)
faye_server.add_extension(MarkOnline.new)

faye_server.on('unsubscribe') do |client_id, channel|
UserChatTeam.mark_offline(client_id)
end

faye_server.on('disconnect') do |client_id|
UserChatTeam.mark_offline(client_id)
end

run faye_server






http://ift.tt/1qd7LQx