search autocomplete

This commit is contained in:
Hipster Hitman
2011-04-10 10:17:06 +04:00
parent 47ee517b27
commit 424906baff
6 changed files with 46 additions and 3 deletions
+15
View File
@@ -9,5 +9,20 @@ class ArtistController < ApplicationController
end
@artist = Artist.getByName(name)
end
def autocomplete
autocomplete = Artist.getLastFmAutocomplete(params[:query])
return render :nothing => true if autocomplete.nil?
suggestions = []
pp autocomplete["response"]
autocomplete["response"]["docs"].each do |doc|
suggestions << doc["artist"] unless suggestions.include?(doc["artist"])
end
render :json => {
:query => params[:query],
:suggestions => suggestions
}
end
end
+12 -1
View File
@@ -1,8 +1,19 @@
class Artist < ActiveRecord::Base
set_table_name 'musicbrainz.bh_artist'
has_many :albums, :conditions => "release_type = 1", :order => 'year ASC, id ASC'
def self.getByName(name)
Artist.first(:conditions => ['name = ? AND id=credit_id', name], :order => 'rating DESC')
end
def self.getLastFmAutocomplete(query)
return nil if query.nil? or query.strip.empty?
json = ActiveSupport::JSON.decode(open(
'http://www.last.fm/search/autocomplete' <<
'?q=' << URI.escape(query)
).read)
return nil if json.empty? else return json
end
end
+14
View File
@@ -1,3 +1,17 @@
<script type="text/javascript">
jQuery(function(){
var ac = $('#name').autocomplete({
serviceUrl: 'search/autocomplete', // Страница для обработки запросов автозаполнения
minChars: 2, // Минимальная длина запроса для срабатывания автозаполнения
delimiter: /(,|;)\s*/, // Разделитель для нескольких запросов, символ или регулярное выражение
maxHeight: 400, // Максимальная высота списка подсказок, в пикселях
width: 300, // Ширина списка
zIndex: 9999, // z-index списка
deferRequestBy: 150, // Задержка запроса (мсек)
});
});
</script>
<div class="search">
<h1>SEARCH</h1>
<%= form_tag('/artist', :method => 'post') %>