From 2556919e09031a7df6c5bd06f63b4a1ad83229f4 Mon Sep 17 00:00:00 2001 From: Joseph Chen Date: Wed, 13 Aug 2014 20:29:59 -0700 Subject: [PATCH] Add mongoid2 support and add mongoid2 to travis test matrix. --- .travis.yml | 3 ++- gemfiles/mongoid2.gemfile | 7 +++++++ lib/searchkick/results.rb | 5 ++++- test/test_helper.rb | 25 ++++++++++++++++++++++++- 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 gemfiles/mongoid2.gemfile diff --git a/.travis.yml b/.travis.yml index f674235..b562811 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,5 +14,6 @@ notifications: on_failure: change gemfile: - Gemfile + - gemfiles/mongoid2.gemfile - gemfiles/mongoid3.gemfile - - gemfiles/mongoid4.gemfile + - gemfiles/mongoid4.gemfile \ No newline at end of file diff --git a/gemfiles/mongoid2.gemfile b/gemfiles/mongoid2.gemfile new file mode 100644 index 0000000..fc19022 --- /dev/null +++ b/gemfiles/mongoid2.gemfile @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in searchkick.gemspec +gemspec path: "../" + +gem "mongoid", "~> 2" +gem "bson_ext" diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 8a2efc7..47e7023 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -27,8 +27,11 @@ module Searchkick records = records.includes(options[:includes]) end results[type] = - if records.respond_to?(:primary_key) + if records.respond_to?(:primary_key) && records.primary_key records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a + elsif records.respond_to?(:all) && records.all && records.all.respond_to?(:for_ids) + # Mongoid 2 + records.all.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a else records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a end diff --git a/test/test_helper.rb b/test/test_helper.rb index 14d4a26..0a780cc 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,8 +10,31 @@ File.delete("elasticsearch.log") if File.exists?("elasticsearch.log") Searchkick.client.transport.logger = Logger.new("elasticsearch.log") if defined?(Mongoid) + + # Helpers to determine version of mongoid. + module Mongoid + def self.mongoid2? + ::Mongoid.const_defined? :Contexts # deprecated in Mongoid 3.x + end + end + + # Mongoid2 uses BSON gem. We need to re-define <=> in order for TestSql.test_order_id to pass. + if defined?(BSON) && defined?(BSON::ObjectId) + module BSON + class ObjectId + def <=>(other) + self.eql?(other) ? 0 : self.generation_time <=> other.generation_time + end + end + end + end + Mongoid.configure do |config| - config.connect_to "searchkick_test" + if Mongoid.mongoid2? + config.master = Mongo::Connection.new.db("searchkick_test") + else + config.connect_to "searchkick_test" + end end class Product -- libgit2 0.21.0