Commit 54f5f4b0f0dd7652eaeec14f13632a779f9a6640
Exists in
master
and in
21 other branches
Merge branch 'joe1chen-mongoid2-support'
Showing
4 changed files
with
35 additions
and
2 deletions
Show diff stats
.travis.yml
lib/searchkick/results.rb
... | ... | @@ -27,9 +27,14 @@ module Searchkick |
27 | 27 | records = records.includes(options[:includes]) |
28 | 28 | end |
29 | 29 | results[type] = |
30 | - if records.respond_to?(:primary_key) | |
30 | + if records.respond_to?(:primary_key) and records.primary_key | |
31 | + # ActiveRecord | |
31 | 32 | records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a |
33 | + elsif records.respond_to?(:all) and records.all.respond_to?(:for_ids) | |
34 | + # Mongoid 2 | |
35 | + records.all.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a | |
32 | 36 | else |
37 | + # Mongoid 3+ | |
33 | 38 | records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a |
34 | 39 | end |
35 | 40 | end | ... | ... |
test/test_helper.rb
... | ... | @@ -10,8 +10,28 @@ File.delete("elasticsearch.log") if File.exists?("elasticsearch.log") |
10 | 10 | Searchkick.client.transport.logger = Logger.new("elasticsearch.log") |
11 | 11 | |
12 | 12 | if defined?(Mongoid) |
13 | + | |
14 | + def mongoid2? | |
15 | + Mongoid::VERSION.starts_with?("2.") | |
16 | + end | |
17 | + | |
18 | + if mongoid2? | |
19 | + # enable comparison of BSON::ObjectIds | |
20 | + module BSON | |
21 | + class ObjectId | |
22 | + def <=>(other) | |
23 | + self.data <=> other.data | |
24 | + end | |
25 | + end | |
26 | + end | |
27 | + end | |
28 | + | |
13 | 29 | Mongoid.configure do |config| |
14 | - config.connect_to "searchkick_test" | |
30 | + if mongoid2? | |
31 | + config.master = Mongo::Connection.new.db("searchkick_test") | |
32 | + else | |
33 | + config.connect_to "searchkick_test" | |
34 | + end | |
15 | 35 | end |
16 | 36 | |
17 | 37 | class Product | ... | ... |