Skip to content

Instantly share code, notes, and snippets.

@mguterl
Created October 12, 2011 18:08
Show Gist options
  • Save mguterl/1282035 to your computer and use it in GitHub Desktop.
Save mguterl/1282035 to your computer and use it in GitHub Desktop.
module Sunspot
class ResqueSessionProxy < Sunspot::SessionProxy::AbstractSessionProxy
attr_reader :original_session
delegate :config, :delete_dirty?, :dirty?,
:new_search, :search,
:new_more_like_this, :more_like_this,
:remove, :remove!,
:remove_by_id, :remove_by_id!,
:remove_all, :remove_all!,
:batch, :commit, :commit_if_delete_dirty, :commit_if_dirty,
:index!, :to => :session
def initialize(session)
@original_session = session
end
alias_method :session, :original_session
def index(*objects)
args = []
objects.flatten.compact.each do |object|
args << object.class.name << object.id
end
Resque.enqueue(Sunspot::IndexJob, *args) unless args.empty?
end
end
end
module Sunspot
class IndexJob
@queue = :indexer
def self.perform(*args)
objects = []
args.each_slice(2) do |(clazz, id)|
object = clazz.constantize.find_by_id(id)
# don't blow up if the object no longer exists in the db
if object
objects << object
end
end
Sunspot.session.original_session.index(*objects)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment