Skip to content

Instantly share code, notes, and snippets.

@tpitale
Forked from dkubb/dm_sunspot.rb
Created October 16, 2011 16:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpitale/1291094 to your computer and use it in GitHub Desktop.
Save tpitale/1291094 to your computer and use it in GitHub Desktop.
DataMapper and Sunspot Integration Extension
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
@brainix
Copy link

brainix commented Jul 4, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment