Skip to content

Instantly share code, notes, and snippets.

@barmstrong
Created September 27, 2012 23:52
Show Gist options
  • Save barmstrong/3797160 to your computer and use it in GitHub Desktop.
Save barmstrong/3797160 to your computer and use it in GitHub Desktop.
require 'sunspot'
require 'mongoid'
require 'sunspot/rails'
class Post
include Mongoid::Document
field :title
include Sunspot::Mongoid
searchable do
text :title
end
end
# lib/sunspot/mongoid.rb
# module Sunspot
module Mongoid
def self.included(base)
base.class_eval do
extend Sunspot::Rails::Searchable::ActsAsMethods
Sunspot::Adapters::DataAccessor.register(DataAccessor, base)
Sunspot::Adapters::InstanceAdapter.register(InstanceAdapter, base)
end
end
class InstanceAdapter < Sunspot::Adapters::InstanceAdapter
def id
@instance.id.to_s
end
end
class DataAccessor < Sunspot::Adapters::DataAccessor
def load(id)
@clazz.criteria.for_ids(Moped::BSON::ObjectId(id))
end
def load_all(ids)
@clazz.criteria.in(:_id => ids.map {|id| Moped::BSON::ObjectId(id)})
end
private
def criteria(id)
@clazz.criteria.id(id)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment