From f7b597b14f30658cb32d51b75db5fa7357382245 Mon Sep 17 00:00:00 2001 From: Karel Minarik Date: Tue, 10 Dec 2013 21:32:02 +0100 Subject: [PATCH] [MODEL] Prevent callback failures with ActiveRecord 3 --- .../model/adapters/active_record.rb | 22 ++++++++++++++----- .../lib/elasticsearch/model/serializing.rb | 2 +- .../integration/active_record_basic_test.rb | 8 +++---- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb b/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb index 1253f5f4f..d8c5e553e 100644 --- a/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb +++ b/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb @@ -10,7 +10,6 @@ module ActiveRecord lambda { |klass| !!defined?(::ActiveRecord::Base) && klass.ancestors.include?(::ActiveRecord::Base) } module Records - # Returns an `ActiveRecord::Relation` instance # def records @@ -21,7 +20,11 @@ def records # sql_records.instance_exec(response['hits']['hits']) do |hits| define_singleton_method :to_a do - self.load + if ::ActiveRecord.respond_to?(:version) && ::ActiveRecord.version.to_s > '4' + self.load + else + self.__send__(:exec_queries) + end @records.sort_by { |record| hits.index { |hit| hit['_id'].to_s == record.id.to_s } } end end @@ -43,7 +46,14 @@ def order(*args) # Redefine the `to_a` method to the original one # sql_records.instance_exec do - define_singleton_method(:to_a) { self.load; @records } + define_singleton_method(:to_a) do + if ::ActiveRecord.respond_to?(:version) && ::ActiveRecord.version.to_s > '4' + self.load + else + self.__send__(:exec_queries) + end + @records + end end sql_records @@ -59,9 +69,9 @@ module Callbacks # def self.included(base) base.class_eval do - after_commit lambda { __elasticsearch__.index_document }, on: [:create] - after_commit lambda { __elasticsearch__.update_document }, on: [:update] - after_commit lambda { __elasticsearch__.delete_document }, on: [:destroy] + after_commit lambda { __elasticsearch__.index_document }, on: :create + after_commit lambda { __elasticsearch__.update_document }, on: :update + after_commit lambda { __elasticsearch__.delete_document }, on: :destroy end end end diff --git a/elasticsearch-model/lib/elasticsearch/model/serializing.rb b/elasticsearch-model/lib/elasticsearch/model/serializing.rb index 2e2395233..659a58bb2 100644 --- a/elasticsearch-model/lib/elasticsearch/model/serializing.rb +++ b/elasticsearch-model/lib/elasticsearch/model/serializing.rb @@ -25,7 +25,7 @@ module InstanceMethods # def as_indexed_json(options={}) # TODO: Play with the `MyModel.indexes` method -- reject non-mapped attributes, `:as` options, etc - self.as_json(options) + self.as_json(options.merge root: false) end end diff --git a/elasticsearch-model/test/integration/active_record_basic_test.rb b/elasticsearch-model/test/integration/active_record_basic_test.rb index 69c78dec4..6ee992b99 100644 --- a/elasticsearch-model/test/integration/active_record_basic_test.rb +++ b/elasticsearch-model/test/integration/active_record_basic_test.rb @@ -26,9 +26,9 @@ class ::Article < ActiveRecord::Base Article.delete_all Article.__elasticsearch__.create_index! force: true - Article.create! title: 'Test' - Article.create! title: 'Testing Coding' - Article.create! title: 'Coding' + ::Article.create! title: 'Test' + ::Article.create! title: 'Testing Coding' + ::Article.create! title: 'Coding' Article.__elasticsearch__.refresh_index! end @@ -36,7 +36,7 @@ class ::Article < ActiveRecord::Base should "index and find a document" do response = Article.search('title:test') - assert response.any? + assert response.any?, "Response should not be empty: #{response.to_a.inspect}" assert_equal 2, response.results.size assert_equal 2, response.records.size