Skip to content

Commit cc53850

Browse files
committed
[MODEL] Minor improvements and retouches to the query methods (result.foo?) implementation and tests
Closes #elastic#70 Related: elastic#41, elastic#64
1 parent 2aec359 commit cc53850

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

elasticsearch-model/lib/elasticsearch/model/response/result.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ def initialize(attributes={})
1919

2020
# Delegate methods to `@result` or `@result._source`
2121
#
22-
def method_missing(method_name, *arguments)
22+
def method_missing(name, *arguments)
2323
case
24-
when method_name.to_s.end_with?('?')
25-
delegate_to_source(method_name, *arguments)
26-
when @result.respond_to?(method_name)
27-
@result.__send__ method_name, *arguments
28-
when @result._source && @result._source.respond_to?(method_name)
29-
delegate_to_source(method_name, *arguments)
24+
when name.to_s.end_with?('?')
25+
@result.__send__(name, *arguments) || ( @result._source && @result._source.__send__(name, *arguments) )
26+
when @result.respond_to?(name)
27+
@result.__send__ name, *arguments
28+
when @result._source && @result._source.respond_to?(name)
29+
@result._source.__send__ name, *arguments
3030
else
3131
super
3232
end
@@ -45,13 +45,6 @@ def as_json(options={})
4545
end
4646

4747
# TODO: #to_s, #inspect, with support for Pry
48-
49-
private
50-
51-
def delegate_to_source(method, *args)
52-
@result._source.__send__ method, *args
53-
end
54-
5548
end
5649
end
5750
end

elasticsearch-model/test/integration/active_record_basic_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ class ::Article < ActiveRecord::Base
5252
assert_equal 'Test', response.records.first.title
5353
end
5454

55+
should "provide access to result" do
56+
response = Article.search query: { match: { title: 'test' } }, highlight: { fields: { title: {} } }
57+
58+
assert_equal 'Test', response.results.first.title
59+
60+
assert_equal true, response.results.first.title?
61+
assert_equal false, response.results.first.boo?
62+
63+
assert_equal true, response.results.first.highlight?
64+
assert_equal true, response.results.first.highlight.title?
65+
assert_equal false, response.results.first.highlight.boo?
66+
end
67+
5568
should "iterate over results" do
5669
response = Article.search('title:test')
5770

elasticsearch-model/test/unit/response_result_test.rb

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,43 @@ class Elasticsearch::Model::ResultTest < Test::Unit::TestCase
2828
end
2929

3030
should "delegate existence method calls to `_source`" do
31-
result = Elasticsearch::Model::Response::Result.new foo: 'bar', _source: { bar: {baz: 'blargh'} }
31+
result = Elasticsearch::Model::Response::Result.new foo: 'bar', _source: { bar: { bam: 'baz' } }
3232

33-
assert_respond_to result, :bar?
3433
assert_respond_to result._source, :bar?
34+
assert_respond_to result, :bar?
3535

36-
assert_equal true, result._source.bar?
37-
assert_equal true, result.bar?
38-
assert_equal false, result.baz?
36+
assert_equal true, result._source.bar?
37+
assert_equal true, result.bar?
38+
assert_equal false, result.boo?
3939

40-
assert_equal true, result.bar.baz?
40+
assert_equal true, result.bar.bam?
4141
assert_equal false, result.bar.boo?
4242
end
4343

4444
should "delegate methods to @result" do
4545
result = Elasticsearch::Model::Response::Result.new foo: 'bar'
4646

47-
assert_equal 'bar', result.foo
48-
assert_equal 'bar', result.fetch('foo')
49-
assert_equal 'moo', result.fetch('NOT_EXIST', 'moo')
47+
assert_equal 'bar', result.foo
48+
assert_equal 'bar', result.fetch('foo')
49+
assert_equal 'moo', result.fetch('NOT_EXIST', 'moo')
50+
assert_equal ['foo'], result.keys
5051

5152
assert_respond_to result, :to_hash
5253
assert_equal({'foo' => 'bar'}, result.to_hash)
5354

5455
assert_raise(NoMethodError) { result.does_not_exist }
5556
end
5657

58+
should "delegate existence method calls to @result" do
59+
result = Elasticsearch::Model::Response::Result.new foo: 'bar', _source: { bar: 'bam' }
60+
assert_respond_to result, :foo?
61+
62+
assert_equal true, result.foo?
63+
assert_equal false, result.boo?
64+
assert_equal false, result._source.foo?
65+
assert_equal false, result._source.boo?
66+
end
67+
5768
should "delegate as_json to @result even when ActiveSupport changed half of Ruby" do
5869
require 'active_support/json/encoding'
5970
result = Elasticsearch::Model::Response::Result.new foo: 'bar'

0 commit comments

Comments
 (0)