Skip to content

Commit

Permalink
[MODEL] Prevent callback failures with ActiveRecord 3
Browse files Browse the repository at this point in the history
  • Loading branch information
karmi committed Jan 18, 2014
1 parent ac6f283 commit f7b597b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch-model/lib/elasticsearch/model/serializing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ 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

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
Expand Down

0 comments on commit f7b597b

Please sign in to comment.