-
Notifications
You must be signed in to change notification settings - Fork 59
/
mongoid.rb
56 lines (46 loc) · 1.53 KB
/
mongoid.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'mongoid'
require 'sunspot/rails/searchable'
# This code is modified for mongoid from the follow,
# http://github.com/outoftime/sunspot/blob/master/sunspot_rails/lib/sunspot/rails/searchable.rb
#
# Sunspot::Rails is distributed under the MIT License, copyright © 2009 Mat Brown
module Sunspot
module Mongoid
def self.included(base)
base.class_eval do
Sunspot::Adapters::DataAccessor.register(DataAccessor, base)
Sunspot::Adapters::InstanceAdapter.register(InstanceAdapter, base)
extend Sunspot::Rails::Searchable::ClassMethods
include Sunspot::Rails::Searchable::InstanceMethods
def self.searchable(options = {}, &block)
Sunspot.setup(self, &block)
class_inheritable_hash :sunspot_options
unless options[:auto_index] == false
before_save :maybe_mark_for_auto_indexing
after_save :maybe_auto_index
end
unless options[:auto_remove] == false
after_destroy do |searchable|
searchable.remove_from_index
end
end
options[:include] = Sunspot::Util::Array(options[:include])
self.sunspot_options = options
end
end
end
class InstanceAdapter < Sunspot::Adapters::InstanceAdapter
def id
@instance.id
end
end
class DataAccessor < Sunspot::Adapters::DataAccessor
def load(id)
@clazz.find(id) rescue nil
end
def load_all(ids)
@clazz.criteria.in(:_id => ids)
end
end
end
end