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
@@ -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 |
20 | + - gemfiles/mongoid4.gemfile | ||
19 | \ No newline at end of file | 21 | \ No newline at end of file |
lib/searchkick/results.rb
@@ -27,8 +27,11 @@ module Searchkick | @@ -27,8 +27,11 @@ 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) && records.primary_key |
31 | records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a | 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 | else | 35 | else |
33 | records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a | 36 | records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a |
34 | end | 37 | end |
test/test_helper.rb
@@ -10,8 +10,31 @@ File.delete("elasticsearch.log") if File.exists?("elasticsearch.log") | @@ -10,8 +10,31 @@ 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 | + # 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 | Mongoid.configure do |config| | 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 | end | 38 | end |
16 | 39 | ||
17 | class Product | 40 | class Product |