From ce7afbd6dd6e3efa557512cd5fe4dc4f88bcc32d Mon Sep 17 00:00:00 2001 From: treby Date: Tue, 3 Mar 2015 16:58:17 +0900 Subject: [PATCH 1/2] Save pre-saved changed_attributes. --- elasticsearch-model/lib/elasticsearch/model/proxy.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/elasticsearch-model/lib/elasticsearch/model/proxy.rb b/elasticsearch-model/lib/elasticsearch/model/proxy.rb index 90e1a9a44..0188763f0 100644 --- a/elasticsearch-model/lib/elasticsearch/model/proxy.rb +++ b/elasticsearch-model/lib/elasticsearch/model/proxy.rb @@ -59,8 +59,9 @@ def __elasticsearch__ &block # @see http://api.rubyonrails.org/classes/ActiveModel/Dirty.html # before_save do |i| + changed_attr = i.__elasticsearch__.instance_variable_get(:@__changed_attributes) || Hash[] i.__elasticsearch__.instance_variable_set(:@__changed_attributes, - Hash[ i.changes.map { |key, value| [key, value.last] } ]) + changed_attr.merge(Hash[ i.changes.map { |key, value| [key, value.last] } ])) end if respond_to?(:before_save) && instance_methods.include?(:changed_attributes) end end From adf4b9410de16e21c9aaf0a81c2228844571f60d Mon Sep 17 00:00:00 2001 From: treby Date: Sat, 18 Apr 2015 12:28:08 +0900 Subject: [PATCH 2/2] Add test for calling multiple save in transaction. --- .../integration/active_record_basic_test.rb | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/elasticsearch-model/test/integration/active_record_basic_test.rb b/elasticsearch-model/test/integration/active_record_basic_test.rb index d5b51642e..1a3a614e3 100644 --- a/elasticsearch-model/test/integration/active_record_basic_test.rb +++ b/elasticsearch-model/test/integration/active_record_basic_test.rb @@ -11,6 +11,7 @@ class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCas ActiveRecord::Schema.define(:version => 1) do create_table :articles do |t| t.string :title + t.string :body t.datetime :created_at, :default => 'NOW()' end end @@ -22,6 +23,7 @@ class ::Article < ActiveRecord::Base settings index: { number_of_shards: 1, number_of_replicas: 0 } do mapping do indexes :title, type: 'string', analyzer: 'snowball' + indexes :body, type: 'string' indexes :created_at, type: 'date' end end @@ -30,9 +32,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', body: '' + ::Article.create! title: 'Testing Coding', body: '' + ::Article.create! title: 'Coding', body: '' Article.__elasticsearch__.refresh_index! end @@ -146,6 +148,29 @@ class ::Article < ActiveRecord::Base assert_equal 1, response.records.size end + should "update for multiple save in transaction" do + article = Article.first + response = Article.search 'body:dummy' + + assert_equal 0, response.results.size + assert_equal 0, response.records.size + + ActiveRecord::Base.transaction do + article.body = 'dummy' + article.save + + article.title = 'special' + article.save + end + + article.__elasticsearch__.update_document + Article.__elasticsearch__.refresh_index! + + response = Article.search 'body:dummy' + assert_equal 1, response.results.size + assert_equal 1, response.records.size + end + should "return results for a DSL search" do response = Article.search query: { match: { title: { query: 'test' } } }