diff --git a/lib/tire/search.rb b/lib/tire/search.rb index 2a5445b4..042e2529 100644 --- a/lib/tire/search.rb +++ b/lib/tire/search.rb @@ -64,8 +64,8 @@ def size(value) self end - def fields(fields=[]) - @fields = fields + def fields(*fields) + @fields = Array(fields.flatten) self end diff --git a/test/integration/results_test.rb b/test/integration/results_test.rb index c78f4978..806ab21b 100644 --- a/test/integration/results_test.rb +++ b/test/integration/results_test.rb @@ -21,6 +21,17 @@ class ResultsIntegrationTest < Test::Unit::TestCase assert_nil s.results.first.tags end + should "allow to retrieve multiple fields" do + q = 'title:one' + s = Tire.search('articles-test') do + query { string q } + fields 'title', 'tags' + end + assert_equal 'One', s.results.first.title + assert_equal 'ruby', s.results.first.tags[0] + assert_nil s.results.first.published_on + end + end end diff --git a/test/unit/search_test.rb b/test/unit/search_test.rb index 62ad1c9f..cd48ad2d 100644 --- a/test/unit/search_test.rb +++ b/test/unit/search_test.rb @@ -254,7 +254,23 @@ def foo; 'bar'; end fields :title end hash = MultiJson.decode( s.to_json ) - assert_equal 'title', hash['fields'] + assert_equal ['title'], hash['fields'] + end + + should "take multiple fields as an Array" do + s = Search::Search.new('index') do + fields [:title, :tags] + end + hash = MultiJson.decode( s.to_json ) + assert_equal ['title', 'tags'], hash['fields'] + end + + should "take multiple fields as splat argument" do + s = Search::Search.new('index') do + fields :title, :tags + end + hash = MultiJson.decode( s.to_json ) + assert_equal ['title', 'tags'], hash['fields'] end end