Skip to content

Save pre-saved changed_attributes. #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion elasticsearch-model/lib/elasticsearch/model/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 28 additions & 3 deletions elasticsearch-model/test/integration/active_record_basic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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' } } }

Expand Down