-
-
Save tpitale/1291094 to your computer and use it in GitHub Desktop.
DataMapper and Sunspot Integration Extension
This file contains hidden or 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
module Sunspot | |
module DataMapper | |
def self.included(base) | |
base.class_eval do | |
alias new_record? new? | |
end | |
base.extend Sunspot::Rails::Searchable::ActsAsMethods | |
Sunspot::Adapters::DataAccessor.register(DataAccessor, base) | |
Sunspot::Adapters::InstanceAdapter.register(InstanceAdapter, base) | |
def base.before_save(*args, &block) | |
before(:save, *args, &block) | |
end | |
def base.after_save(*args, &block) | |
after(:save, *args, &block) | |
end | |
def base.after_destroy(*args, &block) | |
after(:destroy, *args, &block) | |
end | |
def base.find_in_batches(options = {}) | |
batch_size = options.delete(:batch_size) | |
total = count | |
offset = 0 | |
while offset < total | |
yield all(:offset => offset, :limit => batch_size) | |
offset += batch_size | |
end | |
end | |
def base.logger | |
::DataMapper.logger | |
end | |
end | |
class InstanceAdapter < Sunspot::Adapters::InstanceAdapter | |
def id | |
@instance.id | |
end | |
end | |
class DataAccessor < Sunspot::Adapters::DataAccessor | |
def load(id) | |
@clazz.get(id) | |
end | |
def load_all(ids) | |
@clazz.all(:id => ids) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the helpful example code! In case this helps you or anyone else, I've rolled this gist into a higher-level gem here: https://github.com/brainix/data_mapped