Commit d9fa7eea5d97fc126cabdf261cfad315c9e1f4ec

Authored by Andrew Kane
1 parent d55f062a

Mongoid2 updates

.travis.yml
... ... @@ -16,4 +16,4 @@ gemfile:
16 16 - Gemfile
17 17 - gemfiles/mongoid2.gemfile
18 18 - gemfiles/mongoid3.gemfile
19   - - gemfiles/mongoid4.gemfile
20 19 \ No newline at end of file
  20 + - gemfiles/mongoid4.gemfile
... ...
lib/searchkick/results.rb
... ... @@ -27,12 +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) && records.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
32   - elsif records.respond_to?(:all) && records.all && records.all.respond_to?(:for_ids)
  33 + elsif records.respond_to?(:all) and records.all.respond_to?(:for_ids)
33 34 # Mongoid 2
34 35 records.all.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a
35 36 else
  37 + # Mongoid 3+
36 38 records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a
37 39 end
38 40 end
... ...
test/test_helper.rb
... ... @@ -11,15 +11,12 @@ Searchkick.client.transport.logger = Logger.new("elasticsearch.log")
11 11  
12 12 if defined?(Mongoid)
13 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
  14 + def mongoid2?
  15 + Mongoid::VERSION.starts_with?("2.")
19 16 end
20 17  
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)
  18 + if mongoid2?
  19 + # enable comparison of BSON::ObjectIds
23 20 module BSON
24 21 class ObjectId
25 22 def <=>(other)
... ... @@ -30,7 +27,7 @@ if defined?(Mongoid)
30 27 end
31 28  
32 29 Mongoid.configure do |config|
33   - if Mongoid.mongoid2?
  30 + if mongoid2?
34 31 config.master = Mongo::Connection.new.db("searchkick_test")
35 32 else
36 33 config.connect_to "searchkick_test"
... ...