Commit d55f062ae77481045249fafe6f85cce8b89a430b

Authored by Andrew Kane
2 parents 084f7dce 0f4ca3a0

Merge branch 'mongoid2-support' of https://github.com/joe1chen/searchkick into j…

…oe1chen-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
  20 + - gemfiles/mongoid4.gemfile
19 21 \ No newline at end of file
... ...
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,8 +27,11 @@ 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) && records.primary_key
31 31 records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a
  32 + elsif records.respond_to?(:all) && records.all && records.all.respond_to?(:for_ids)
  33 + # Mongoid 2
  34 + records.all.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a
32 35 else
33 36 records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a
34 37 end
... ...
test/test_helper.rb
... ... @@ -10,8 +10,31 @@ 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 + # Helpers to determine version of mongoid.
  15 + module Mongoid
  16 + def self.mongoid2?
  17 + ::Mongoid.const_defined? :Contexts # deprecated in Mongoid 3.x
  18 + end
  19 + end
  20 +
  21 + # Mongoid2 uses BSON gem. We need to re-define <=> in order for TestSql.test_order_id to pass.
  22 + if Mongoid.mongoid2? && defined?(BSON) && defined?(BSON::ObjectId)
  23 + module BSON
  24 + class ObjectId
  25 + def <=>(other)
  26 + self.data <=> other.data
  27 + end
  28 + end
  29 + end
  30 + end
  31 +
13 32 Mongoid.configure do |config|
14   - config.connect_to "searchkick_test"
  33 + if Mongoid.mongoid2?
  34 + config.master = Mongo::Connection.new.db("searchkick_test")
  35 + else
  36 + config.connect_to "searchkick_test"
  37 + end
15 38 end
16 39  
17 40 class Product
... ...