Skip to content

Instantly share code, notes, and snippets.

@nickmerwin
Created September 13, 2011 23:16
Show Gist options
  • Save nickmerwin/1215447 to your computer and use it in GitHub Desktop.
Save nickmerwin/1215447 to your computer and use it in GitHub Desktop.
Tandemstock.com Sunspot Session Proxy
module Sunspot
module SessionProxy
class TandemSessionProxy < AbstractSessionProxy
attr_reader :search_session
delegate :new_search, :search, :config,
:new_more_like_this, :more_like_this,
:delete_dirty, :delete_dirty?,
:to => :search_session
def initialize(search_session = Sunspot.session)
@search_session = search_session
end
def rescued_exception(method, e, args, _caller)
HoptoadNotifier.notify(
:error_class => e.class.to_s,
:error_message => e.message,
:parameters => {:args => args.inspect, :method => method},
:backtrace => _caller
)
end
SUPPORTED_METHODS = [
:batch, :commit, :commit_if_dirty, :commit_if_delete_dirty, :dirty?,
:index!, :index, :optimize, :remove!, :remove, :remove_all!, :remove_all,
:remove_by_id!, :remove_by_id
]
SUPPORTED_METHODS.each do |method|
module_eval(<<-RUBY)
def #{method}(*args, &block)
begin
res = search_session.#{method}(*args, &block)
#{"args.each{|obj| obj.reload.update_attribute :indexed_at, Time.now if obj.respond_to?(:indexed_at)}" if %w{index! index}.include?(method.to_s)}
res
rescue Exception => e
self.rescued_exception(:#{method}, e, args, caller)
end
end
RUBY
end
end
end
end
Sunspot.session = Sunspot::SessionProxy::TandemSessionProxy.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment