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
@@ -14,5 +14,6 @@ notifications: | @@ -14,5 +14,6 @@ notifications: | ||
14 | on_failure: change | 14 | on_failure: change |
15 | gemfile: | 15 | gemfile: |
16 | - Gemfile | 16 | - Gemfile |
17 | + - gemfiles/mongoid2.gemfile | ||
17 | - gemfiles/mongoid3.gemfile | 18 | - gemfiles/mongoid3.gemfile |
18 | - gemfiles/mongoid4.gemfile | 19 | - gemfiles/mongoid4.gemfile |
lib/searchkick/results.rb
@@ -27,9 +27,14 @@ module Searchkick | @@ -27,9 +27,14 @@ module Searchkick | ||
27 | records = records.includes(options[:includes]) | 27 | records = records.includes(options[:includes]) |
28 | end | 28 | end |
29 | results[type] = | 29 | results[type] = |
30 | - if records.respond_to?(:primary_key) | 30 | + if records.respond_to?(:primary_key) and records.primary_key |
31 | + # ActiveRecord | ||
31 | records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a | 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 | else | 36 | else |
37 | + # Mongoid 3+ | ||
33 | records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a | 38 | records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a |
34 | end | 39 | end |
35 | end | 40 | end |
test/test_helper.rb
@@ -10,8 +10,28 @@ File.delete("elasticsearch.log") if File.exists?("elasticsearch.log") | @@ -10,8 +10,28 @@ File.delete("elasticsearch.log") if File.exists?("elasticsearch.log") | ||
10 | Searchkick.client.transport.logger = Logger.new("elasticsearch.log") | 10 | Searchkick.client.transport.logger = Logger.new("elasticsearch.log") |
11 | 11 | ||
12 | if defined?(Mongoid) | 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 | Mongoid.configure do |config| | 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 | end | 35 | end |
16 | 36 | ||
17 | class Product | 37 | class Product |