Skip to content

Commit

Permalink
Merge pull request #4 from jrgns/feat/available-to-customize-index-va…
Browse files Browse the repository at this point in the history
…lues

Ability to customize the indexed values. Fixes #3
  • Loading branch information
jrgns authored Jun 14, 2018
2 parents eae1cf2 + b763206 commit 3409efa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ before_script:
> ./cc-test-reporter
- chmod +x ./cc-test-reporter
- "./cc-test-reporter before-build"
- export TZ=Africa/Johannesburg
after_script:
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
deploy:
Expand Down
7 changes: 6 additions & 1 deletion lib/sequel/plugins/elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,15 @@ def document_path
# Create or update the document on the Elasticsearch cluster.
def index_document
params = document_path
params[:body] = values.each_key { |k| values[k] = values[k].strftime('%FT%T%:z') if values[k].is_a?(Time) }
params[:body] = indexed_values
es_client.index params
end

# Values to be indexed
def indexed_values
values.each_key { |k| values[k] = values[k].strftime('%FT%T%:z') if values[k].is_a?(Time) }
end

# Remove the document from the Elasticsearch cluster.
def destroy_document
es_client.delete document_path
Expand Down
41 changes: 27 additions & 14 deletions spec/sequel/plugins/elasticsearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@
end
end

context '#indexed_values' do
it 'correctly formats dates and other types' do
doc = simple_doc.new(
title: 'title',
content: 'content',
views: 4,
active: true,
created_at: Time.parse('2018-02-07T22:18:42+02:00')
)
expect(doc.send(:indexed_values)).to include(
title: "title",
content: "content",
views: 4,
active: true,
created_at: "2018-02-07T22:18:42+02:00"
)
end

it 'can be extended' do
doc = simple_doc.new
def doc.indexed_values
{ test: 'this' }
end
expect(doc.send(:indexed_values)).to include(test: 'this')
end
end

context '#document_path' do
it 'returns the document index, type and id for documents' do
stub_request(:put, %r{http://localhost:9200/documents/sync/\d+})
Expand All @@ -205,20 +232,6 @@
expect(WebMock)
.to have_requested(:put, "http://localhost:9200/#{simple_doc.table_name}/sync/#{doc.id}")
end

it 'correctly formats dates and other types' do
stub_request(:put, %r{http://localhost:9200/documents/sync/\d+})
doc = simple_doc.new(
title: 'title',
content: 'content',
views: 4,
active: true,
created_at: Time.parse('2018-02-07T22:18:42+02:00')
).save
expect(WebMock)
.to have_requested(:put, "http://localhost:9200/#{simple_doc.table_name}/sync/#{doc.id}")
.with(body: '{"id":' + doc.id.to_s + ',"title":"title","content":"content","views":4,"active":true,"created_at":"2018-02-07T22:18:42+00:00"}')
end
end

context '#update' do
Expand Down

0 comments on commit 3409efa

Please sign in to comment.