Skip to content

Commit

Permalink
[webui] refactor webui search module a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianschroeter committed Aug 8, 2013
1 parent 3d15b64 commit f1f00cf
Showing 1 changed file with 69 additions and 62 deletions.
131 changes: 69 additions & 62 deletions src/webui/app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,53 @@ def index
end

def owner
search
# If the search is too short, return
if (!@search_text or @search_text.length < 2) && !@search_attrib_type_id && !@search_issue
flash[:error] = "Search string must contain at least two characters."
return
end

r = []
collection = find_cached(Owner, :binary => "#{@search_text}", :limit => "#{@owner_limit}", :devel => "#{@owner_devel}", :expires_in => 5.minutes)
collection.send("each_owner") do |result|
users = []
groups = []
if result.to_hash['person']
if result.to_hash['person'].class != Array
blah = []
blah << result.to_hash['person']
users = blah
else
result.to_hash['person'].each do |p|
users << p
end
end
end
if result.to_hash['group']
if result.to_hash['group'].class != Array
blah = []
blah << result.to_hash['group']
groups = blah
else
result.to_hash['group'].each do |g|
groups << g
end
end
end
project = find_cached(Project, result.project)
if result.package
package = find_cached(Package, result.package, :project => project)
end
r << {:type => "owner", :data => result,
:users => users, :groups => groups,
:project => project, :package => package}
end
@results.concat(r)
@per_page = nil

validate_result
end

# The search method does the search and renders the results
# if there is something to search for. If not then it just
# renders a search bar.
Expand Down Expand Up @@ -48,68 +93,19 @@ def search
return
end

# The owner form
if @search_what == ["owner"]
r = []
collection = find_cached(Owner, :binary => "#{@search_text}", :limit => "#{@owner_limit}", :devel => "#{@owner_devel}", :expires_in => 5.minutes)
collection.send("each_owner") do |result|
users = []
groups = []
if result.to_hash['person']
if result.to_hash['person'].class != Array
blah = []
blah << result.to_hash['person']
users = blah
else
result.to_hash['person'].each do |p|
users << p
end
end
end
if result.to_hash['group']
if result.to_hash['group'].class != Array
blah = []
blah << result.to_hash['group']
groups = blah
else
result.to_hash['group'].each do |g|
groups << g
end
end
end
project = find_cached(Project, result.project)
if result.package
package = find_cached(Package, result.package, :project => project)
end
r << {:type => "owner", :data => result,
:users => users, :groups => groups,
:project => project, :package => package}
end
@results.concat(r)
@per_page = nil
# Full text search
else
@per_page = 50
search = ApiDetails.create(:searches, :page => params[:page], :per_page => @per_page, :search => {
text: @search_text,
classes: @search_what,
attrib_type_id: @search_attrib_type_id,
fields: @search_where,
issue_name: @search_issue,
issue_tracker_name: @search_tracker})
@results = search["result"].map {|i| i.symbolize_keys}
@results = Kaminari.paginate_array(@results, total_count: search["total_entries"])
@results = @results.page(params[:page]).per(@per_page)
end
@per_page = 50
search = ApiDetails.create(:searches, :page => params[:page], :per_page => @per_page, :search => {
text: @search_text,
classes: @search_what,
attrib_type_id: @search_attrib_type_id,
fields: @search_where,
issue_name: @search_issue,
issue_tracker_name: @search_tracker})
@results = search["result"].map {|i| i.symbolize_keys}
@results = Kaminari.paginate_array(@results, total_count: search["total_entries"])
@results = @results.page(params[:page]).per(@per_page)

logger.debug "Found #{@results.length} search results: #{@results.inspect}"
if @results.length < 1
flash[:notice] = "Your search did not return any results."
end
if @results.length > 200
@results = @results[0..199]
flash[:notice] = "Your search returned more than 200 results. Please be more precise."
end
validate_result
end

# This method handles obs:// disturls
Expand Down Expand Up @@ -205,4 +201,15 @@ def set_tracker_list
@issue_tracker_list.sort_by! {|a| a.first.downcase }
end

def validate_result
logger.debug "Found #{@results.length} search results: #{@results.inspect}"
if @results.length < 1
flash[:notice] = "Your search did not return any results."
end
if @results.length > 200
@results = @results[0..199]
flash[:notice] = "Your search returned more than 200 results. Please be more precise."
end
end

end

0 comments on commit f1f00cf

Please sign in to comment.