diff --git a/.travis.yml b/.travis.yml index f9f5806..70d7a27 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/lib/sequel/plugins/elasticsearch.rb b/lib/sequel/plugins/elasticsearch.rb index d28b0aa..163a65c 100644 --- a/lib/sequel/plugins/elasticsearch.rb +++ b/lib/sequel/plugins/elasticsearch.rb @@ -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 diff --git a/spec/sequel/plugins/elasticsearch_spec.rb b/spec/sequel/plugins/elasticsearch_spec.rb index af9d5b8..811e392 100644 --- a/spec/sequel/plugins/elasticsearch_spec.rb +++ b/spec/sequel/plugins/elasticsearch_spec.rb @@ -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+}) @@ -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