Skip to content

Commit f7b597b

Browse files
committed
[MODEL] Prevent callback failures with ActiveRecord 3
1 parent ac6f283 commit f7b597b

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb

+16-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module ActiveRecord
1010
lambda { |klass| !!defined?(::ActiveRecord::Base) && klass.ancestors.include?(::ActiveRecord::Base) }
1111

1212
module Records
13-
1413
# Returns an `ActiveRecord::Relation` instance
1514
#
1615
def records
@@ -21,7 +20,11 @@ def records
2120
#
2221
sql_records.instance_exec(response['hits']['hits']) do |hits|
2322
define_singleton_method :to_a do
24-
self.load
23+
if ::ActiveRecord.respond_to?(:version) && ::ActiveRecord.version.to_s > '4'
24+
self.load
25+
else
26+
self.__send__(:exec_queries)
27+
end
2528
@records.sort_by { |record| hits.index { |hit| hit['_id'].to_s == record.id.to_s } }
2629
end
2730
end
@@ -43,7 +46,14 @@ def order(*args)
4346
# Redefine the `to_a` method to the original one
4447
#
4548
sql_records.instance_exec do
46-
define_singleton_method(:to_a) { self.load; @records }
49+
define_singleton_method(:to_a) do
50+
if ::ActiveRecord.respond_to?(:version) && ::ActiveRecord.version.to_s > '4'
51+
self.load
52+
else
53+
self.__send__(:exec_queries)
54+
end
55+
@records
56+
end
4757
end
4858

4959
sql_records
@@ -59,9 +69,9 @@ module Callbacks
5969
#
6070
def self.included(base)
6171
base.class_eval do
62-
after_commit lambda { __elasticsearch__.index_document }, on: [:create]
63-
after_commit lambda { __elasticsearch__.update_document }, on: [:update]
64-
after_commit lambda { __elasticsearch__.delete_document }, on: [:destroy]
72+
after_commit lambda { __elasticsearch__.index_document }, on: :create
73+
after_commit lambda { __elasticsearch__.update_document }, on: :update
74+
after_commit lambda { __elasticsearch__.delete_document }, on: :destroy
6575
end
6676
end
6777
end

elasticsearch-model/lib/elasticsearch/model/serializing.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module InstanceMethods
2525
#
2626
def as_indexed_json(options={})
2727
# TODO: Play with the `MyModel.indexes` method -- reject non-mapped attributes, `:as` options, etc
28-
self.as_json(options)
28+
self.as_json(options.merge root: false)
2929
end
3030

3131
end

elasticsearch-model/test/integration/active_record_basic_test.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ class ::Article < ActiveRecord::Base
2626
Article.delete_all
2727
Article.__elasticsearch__.create_index! force: true
2828

29-
Article.create! title: 'Test'
30-
Article.create! title: 'Testing Coding'
31-
Article.create! title: 'Coding'
29+
::Article.create! title: 'Test'
30+
::Article.create! title: 'Testing Coding'
31+
::Article.create! title: 'Coding'
3232

3333
Article.__elasticsearch__.refresh_index!
3434
end
3535

3636
should "index and find a document" do
3737
response = Article.search('title:test')
3838

39-
assert response.any?
39+
assert response.any?, "Response should not be empty: #{response.to_a.inspect}"
4040

4141
assert_equal 2, response.results.size
4242
assert_equal 2, response.records.size

0 commit comments

Comments
 (0)