diff --git a/app/assets/javascripts/application/autocomplete.js b/app/assets/javascripts/application/autocomplete.js index 916308ff14e66d624ff82f32d0e6a014686baa1b..22bb933d7d441c4e877c3748939481231ebd3e4d 100644 --- a/app/assets/javascripts/application/autocomplete.js +++ b/app/assets/javascripts/application/autocomplete.js @@ -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); }; }); diff --git a/app/assets/stylesheets/application/search.scss b/app/assets/stylesheets/application/search.scss index 25c956f57d11948fc789e2751467998b794e2666..5e09e993e14533f1740fac3e5b7a2c9f78dffafc 100644 --- a/app/assets/stylesheets/application/search.scss +++ b/app/assets/stylesheets/application/search.scss @@ -3,10 +3,7 @@ } .autocomplete { - width: 56px; - height: 32px; - border: 0; - margin-right: 8px; + margin-right: 5px; } .search-sidebar { diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index c4d440da9d34846f709ee52d9c8e3f84655b7e88..6656f8ff981426bceacccb181d89b1a4d2f8756f 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -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 = {}