Skip to content

Commit 3d87815

Browse files
author
Christopher Brown
committed
CHEF-106 refactor for attributes in search method (and out of controller)
1 parent 220bb4b commit 3d87815

2 files changed

Lines changed: 26 additions & 35 deletions

File tree

chef-server/lib/chef/search.rb

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,31 @@ def initialize
2727
@index = Ferret::Index::Index.new(:path => Chef::Config[:search_index_path])
2828
end
2929

30-
def search(type, query, &block)
30+
def search(type, query, attributes, &block)
3131
search_query = build_search_query(type, query)
3232
start_time = Time.now
33-
result = Array.new
3433

35-
if Kernel.block_given?
36-
result = @index.search_each(search_query, :limit => :all) do |id, score|
37-
block.call(build_hash(@index.doc(id)))
38-
end
39-
else
40-
@index.search_each(search_query, :limit => :all) do |id, score|
41-
result << build_hash(@index.doc(id))
42-
end
34+
results = @index.search_each(search_query, :limit => :all) do |id, score|
35+
q = build_hash(@index.doc(id))
36+
Kernel.block_given? ? block.call(q) : [q]
4337
end
38+
4439
Chef::Log.debug("Search #{search_query} complete in #{Time.now - start_time} seconds")
45-
result
40+
41+
unless attributes.empty?
42+
results = results.collect do |r|
43+
nr = Hash.new
44+
nr[:index_name] = r[:index_name]
45+
nr[:id] = r[:id]
46+
attributes.each do |attrib|
47+
if r.has_key?(attrib)
48+
nr[attrib] = r[attrib]
49+
end
50+
end
51+
nr
52+
end
53+
end
54+
results
4655
end
4756

4857
def list_indexes

chef-server/lib/controllers/search.rb

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#
1+
#
22
# Author:: Adam Jacob (<adam@opscode.com>)
33
# Copyright:: Copyright (c) 2008 Opscode, Inc.
44
# License:: Apache License, Version 2.0
@@ -31,29 +31,11 @@ def index
3131

3232
def show
3333
@s = Chef::Search.new
34-
@results = nil
35-
if params[:q]
36-
@results = @s.search(params[:id], params[:q] == "" ? "*" : params[:q])
37-
else
38-
@results = @s.search(params[:id], "*")
39-
end
40-
# Boy, this should move to the search function
41-
if params[:a]
42-
attributes = params[:a].split(",").collect { |a| a.to_sym }
43-
unless attributes.length == 0
44-
@results = @results.collect do |r|
45-
nr = Hash.new
46-
nr[:index_name] = r[:index_name]
47-
nr[:id] = r[:id]
48-
attributes.each do |attrib|
49-
if r.has_key?(attrib)
50-
nr[attrib] = r[attrib]
51-
end
52-
end
53-
nr
54-
end
55-
end
56-
end
34+
35+
query = params[:q].nil? ? "*" : (params[:q].empty? ? "*" : params[:q])
36+
attributes = params[:a].nil? ? [] : params[:a].split(",").collect { |a| a.to_sym }
37+
@results = @s.search(params[:id], query, attributes)
38+
5739
display @results
5840
end
5941

0 commit comments

Comments
 (0)