Commit 2556919e09031a7df6c5bd06f63b4a1ad83229f4
1 parent
9a9707d4
Exists in
master
and in
21 other branches
Add mongoid2 support and add mongoid2 to travis test matrix.
Showing
4 changed files
with
37 additions
and
3 deletions
Show diff stats
.travis.yml
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 defined?(BSON) && defined?(BSON::ObjectId) | |
23 | + module BSON | |
24 | + class ObjectId | |
25 | + def <=>(other) | |
26 | + self.eql?(other) ? 0 : self.generation_time <=> other.generation_time | |
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 | ... | ... |