Skip to content

Instantly share code, notes, and snippets.

@kivanio
Forked from RafeHatfield/sunspot_sidekiq.rb
Created April 2, 2014 21:31
Show Gist options
  • Save kivanio/9943596 to your computer and use it in GitHub Desktop.
Save kivanio/9943596 to your computer and use it in GitHub Desktop.
# app/models/post.rb
class Post
searchable :auto_index => false, :auto_remove => false do
text :title
text :body
end
after_commit :sidekiq_solr_update, :if => :persisted?
before_destroy :sidekiq_solr_remove
protected
def sidekiq_solr_update
SolrUpdate.perform_async(self.class.to_s, id)
end
def sidekiq_solr_remove
SolrRemove.perform_async(self.class.to_s, id)
end
end
# app/workers/solr_update.rb
class SolrUpdate
include Sidekiq::Worker
sidekiq_options :queue => :solr
def perform(classname, id)
classname.constantize.find(id).solr_index
end
end
# app/workers/solr_remove.rb
class SolrRemove
include Sidekiq::Worker
sidekiq_options :queue => :solr
def perform(classname, id)
Sunspot.remove_by_id(classname, id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment