Commit 54f5f4b0f0dd7652eaeec14f13632a779f9a6640

Authored by Andrew Kane
2 parents 084f7dce d9fa7eea

Merge branch 'joe1chen-mongoid2-support'

.travis.yml
... ... @@ -14,5 +14,6 @@ notifications:
14 14 on_failure: change
15 15 gemfile:
16 16 - Gemfile
  17 + - gemfiles/mongoid2.gemfile
17 18 - gemfiles/mongoid3.gemfile
18 19 - gemfiles/mongoid4.gemfile
... ...
gemfiles/mongoid2.gemfile 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +source 'https://rubygems.org'
  2 +
  3 +# Specify your gem's dependencies in searchkick.gemspec
  4 +gemspec path: "../"
  5 +
  6 +gem "mongoid", "~> 2"
  7 +gem "bson_ext"
... ...
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
... ...