Created
September 27, 2012 23:52
-
-
Save barmstrong/3797160 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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