Skip to content

Instantly share code, notes, and snippets.

@DimaD
Created February 14, 2011 15:21
Show Gist options
  • Save DimaD/826024 to your computer and use it in GitHub Desktop.
Save DimaD/826024 to your computer and use it in GitHub Desktop.
module Sunspot #:nodoc:
module Rails #:nodoc:
#
# This module provides Sunspot Adapter implementations for Mongoid
# models.
#
module Adapters
class MongoidDocumentInstanceAdapter < Sunspot::Adapters::InstanceAdapter
#
# Return the primary key for the adapted instance
#
# ==== Returns
#
# Integer:: Database ID of model
#
def id
@instance.id
end
end
class MongoidDocumentsDataAccessor < Sunspot::Adapters::DataAccessor
# options for the find
attr_accessor :include, :select
#
# Set the fields to select from the database. This will be passed
# to ActiveRecord.
#
# ==== Parameters
#
# value<Mixed>:: String of comma-separated columns or array of columns
#
def select=(value)
value = value.join(', ') if value.respond_to?(:join)
@select = value
end
#
# Get one ActiveRecord instance out of the database by ID
#
# ==== Parameters
#
# id<String>:: Database ID of model to retreive
#
# ==== Returns
#
# ActiveRecord::Base:: ActiveRecord model
#
def load(id)
@clazz.find(id)
end
#
# Get a collection of ActiveRecord instances out of the database by ID
#
# ==== Parameters
#
# ids<Array>:: Database IDs of models to retrieve
#
# ==== Returns
#
# Array:: Collection of ActiveRecord models
#
def load_all(ids)
@clazz.where(:_id.in => ids)
end
end
end
end
end
Sunspot::Adapters::InstanceAdapter.register(Sunspot::Rails::Adapters::MongoidDocumentInstanceAdapter, Mongoid::Document)
Sunspot::Adapters::DataAccessor.register(Sunspot::Rails::Adapters::MongoidDocumentsDataAccessor, Mongoid::Document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment