Skip to content

Compatibility for rails 3.2 #116

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 5 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/indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ def delete_document(options={})
def update_document(options={})
if changed_attributes = self.instance_variable_get(:@__changed_attributes)
attributes = if respond_to?(:as_indexed_json)
changed_attributes.select { |k,v| self.as_indexed_json.keys.include? k }
indexed_symbols = self.as_indexed_json.keys.map(&:to_sym)
changed_attributes.select { |k,v| indexed_symbols.include? k.to_sym }
else
changed_attributes
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,68 @@ def as_indexed_json(options={})
end
end


class ::ArticleWithCustomSerializationMethod < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks

mapping do
indexes :tag_list
end

def tag_list=(val)
attribute_will_change!('tag_list') unless val == @tag_list
@tag_list=val
end

def tag_list
@tag_list || 'foo bar'
end

def as_indexed_json(options={})
as_json(root: false, methods: [:tag_list])
end
end

context "ActiveRecord model with custom JSON serialization including method" do
setup do
ActiveRecord::Schema.define(:version => 1) do
create_table ArticleWithCustomSerializationMethod.table_name do |t|
t.string :title
end
end

ArticleWithCustomSerializationMethod.delete_all
ArticleWithCustomSerializationMethod.__elasticsearch__.create_index! force: true
end

should "index the tag_list method when creating" do
ArticleWithCustomSerializationMethod.create! title: 'Test'

a = ArticleWithCustomSerializationMethod.__elasticsearch__.client.get \
index: 'article_with_custom_serialization_methods',
type: 'article_with_custom_serialization_method',
id: '1'

assert_equal( { 'id'=> 1, 'title'=> 'Test', 'tag_list' => 'foo bar' }, a['_source'] )
end

should "index the updated tag_list attribute when updating" do
ArticleWithCustomSerializationMethod.create! title: 'Test'

article = ArticleWithCustomSerializationMethod.first
article.tag_list='UPDATED'
article.save!

a = ArticleWithCustomSerializationMethod.__elasticsearch__.client.get \
index: 'article_with_custom_serialization_methods',
type: 'article_with_custom_serialization_method',
id: '1'

assert_equal( { 'id'=> 1, 'title'=> 'Test', 'tag_list' => 'UPDATED' }, a['_source'] )
end
end

end
end
end
38 changes: 38 additions & 0 deletions elasticsearch-model/test/unit/indexing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,25 @@ def as_indexed_json(options={})
{ :foo => 'B' }
end
end

class ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJsonMixed
extend Elasticsearch::Model::Indexing::ClassMethods
include Elasticsearch::Model::Indexing::InstanceMethods

def self.before_save(&block)
(@callbacks ||= {})[block.hash] = block
end

def changed_attributes; [:foo, :bar]; end

def changes
{:foo => ['A', 'B'], 'bar' => ['C', 'D']}
end

def as_indexed_json(options={})
{ 'foo' => 'B', :bar => 'D' }
end
end

should "register before_save callback when included" do
::DummyIndexingModelWithCallbacks.expects(:before_save).returns(true)
Expand Down Expand Up @@ -295,6 +314,25 @@ def as_indexed_json(options={})

instance.update_document
end

should "accept strings and symbols for as_indexed_json, because as_json(methods: [:foo, :bar]) in rails 3.2 returns symbols instead of strings" do
client = mock('client')
instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJsonMixed.new

# Set the fake `changes` hash
instance.instance_variable_set(:@__changed_attributes, {foo: 'B', bar: 'D' })

client.expects(:update).with do |payload|
assert_equal({foo: 'B', bar: 'D'}, payload[:body][:doc])
end

instance.expects(:client).returns(client)
instance.expects(:index_name).returns('foo')
instance.expects(:document_type).returns('bar')
instance.expects(:id).returns('1')

instance.update_document
end
end

context "Re-creating the index" do
Expand Down