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() {
}
}).data("uiAutocomplete")._renderItem = function(ul, item) {
return $("<li></li>")
.append("<img class='autocomplete' src='" + item.thumbnail + "'>")
.append("<a>"+ item.name +"</a>" )
.append(item.thumbnail)
.append(item.name)
.appendTo(ul);
};
});
......@@ -3,10 +3,7 @@
}
.autocomplete {
width: 56px;
height: 32px;
border: 0;
margin-right: 8px;
margin-right: 5px;
}
.search-sidebar {
......
......@@ -31,56 +31,45 @@ class SearchController < ApplicationController
end
def autocomplete
response = []
params_hash = {}
get_thumbnail = nil
case params[:search_class]
when "LearningObject"
los = LearningObject.search(params[:query], {
fields: ['name^5', 'description', 'author'],
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
params_hash = { fields: ['name^10', 'description', 'author'] }
get_thumbnail = Proc.new { |obj| image_tag(learning_object_thumbnail(obj)) }
when "Collection"
cols = Collection.search(params[:query], {
where: {privacy: "public"},
fields: ['name^5', 'description', 'owner'],
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
params_hash = { where: {privacy: "public"},
fields: ['name^10', 'description', 'owner'] }
get_thumbnail = Proc.new { |obj| image_tag("/assets/icons/collection") }
when "User"
users = User.search(params[:query], {
fields: ['name'],
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
params_hash = { fields: ['name'] }
get_thumbnail = Proc.new { |obj| image_tag(obj.avatar.url(:thumb), 32) }
else
raise "Wrong search class parameter"
end
render json: response
render json: autocomplete_search(Object.const_get(params[:search_class]), params_hash, get_thumbnail)
end
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)
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