Skip to content
Snippets Groups Projects
Commit 8a25eb33 authored by Israel Barreto Sant'Anna's avatar Israel Barreto Sant'Anna
Browse files

Fixed thumbnails in autocomplete and refactored autocomplete code in SearchController

parent 5f916ab4
No related branches found
No related tags found
No related merge requests found
...@@ -17,8 +17,8 @@ $(document).ready(function() { ...@@ -17,8 +17,8 @@ $(document).ready(function() {
} }
}).data("uiAutocomplete")._renderItem = function(ul, item) { }).data("uiAutocomplete")._renderItem = function(ul, item) {
return $("<li></li>") return $("<li></li>")
.append("<img class='autocomplete' src='" + item.thumbnail + "'>") .append(item.thumbnail)
.append("<a>"+ item.name +"</a>" ) .append(item.name)
.appendTo(ul); .appendTo(ul);
}; };
}); });
...@@ -3,10 +3,7 @@ ...@@ -3,10 +3,7 @@
} }
.autocomplete { .autocomplete {
width: 56px; margin-right: 5px;
height: 32px;
border: 0;
margin-right: 8px;
} }
.search-sidebar { .search-sidebar {
......
...@@ -31,56 +31,45 @@ class SearchController < ApplicationController ...@@ -31,56 +31,45 @@ class SearchController < ApplicationController
end end
def autocomplete def autocomplete
response = [] params_hash = {}
get_thumbnail = nil
case params[:search_class] case params[:search_class]
when "LearningObject" when "LearningObject"
los = LearningObject.search(params[:query], { params_hash = { fields: ['name^10', 'description', 'author'] }
fields: ['name^5', 'description', 'author'], get_thumbnail = Proc.new { |obj| image_tag(learning_object_thumbnail(obj)) }
limit: 10,
misspellings: { below: 5 }
})
los.each do |lo|
hash = {}
hash["name"] = lo.name
hash["thumbnail"] = "/assets/"+learning_object_thumbnail(lo)
hash["url"] = url_for([lo, :only_path => false])
response << hash
end
when "Collection" when "Collection"
cols = Collection.search(params[:query], { params_hash = { where: {privacy: "public"},
where: {privacy: "public"}, fields: ['name^10', 'description', 'owner'] }
fields: ['name^5', 'description', 'owner'], get_thumbnail = Proc.new { |obj| image_tag("/assets/icons/collection") }
limit: 10,
misspellings: { below: 5 }
})
cols.each do |col|
hash = {}
hash["name"] = col.name
hash["thumbnail"] = "/assets/icons/collection"
hash["url"] = url_for([col, :only_path => false])
response << hash
end
when "User" when "User"
users = User.search(params[:query], { params_hash = { fields: ['name'] }
fields: ['name'], get_thumbnail = Proc.new { |obj| image_tag(obj.avatar.url(:thumb), 32) }
limit: 10,
misspellings: { below: 5 }
})
users.each do |user|
hash = {}
hash["name"] = user.name
hash["thumbnail"] = user.avatar.url(:thumb)
hash["url"] = url_for([user, :only_path => false])
response << hash
end
else else
raise "Wrong search class parameter" raise "Wrong search class parameter"
end end
render json: response render json: autocomplete_search(Object.const_get(params[:search_class]), params_hash, get_thumbnail)
end end
private private
def autocomplete_search(search_class, params_hash={}, get_thumbnail)
response = []
search_params = { limit: 10, misspellings: { below: 5 } }
objs = search_class.search(params[:query],search_params.merge(params_hash))
objs.each do |obj|
hash = {}
hash["name"] = obj.name
hash["thumbnail"] = get_thumbnail.call(obj)
hash["url"] = url_for([obj, :only_path => false])
response << hash
end
response
end
def image_tag(image, width=56, height=32)
ActionController::Base.helpers.image_tag image, width: width, height: height, class: "autocomplete"
end
def where_hash(params) def where_hash(params)
hash = {} hash = {}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment