mirror of
https://github.com/neocities/neocities.git
synced 2025-04-24 17:22:35 +02:00
way better autocomplete, ranks by popularity
This commit is contained in:
parent
698c602475
commit
4a4553f676
3 changed files with 11 additions and 11 deletions
4
app.rb
4
app.rb
|
@ -360,8 +360,8 @@ post '/tags/remove' do
|
|||
redirect request.referer
|
||||
end
|
||||
|
||||
get '/tags/suggestions/:name.json' do |name|
|
||||
Tag.suggestions(name).collect {|t| t.name}.to_json
|
||||
get '/tags/autocomplete/:name.json' do |name|
|
||||
Tag.autocomplete(name).collect {|t| t[:name]}.to_json
|
||||
end
|
||||
|
||||
get '/browse/?' do
|
||||
|
|
|
@ -5,21 +5,21 @@ class Tag < Sequel::Model
|
|||
|
||||
def before_create
|
||||
super
|
||||
values[:name].downcase!
|
||||
values[:name].strip!
|
||||
values[:name] = self.class.clean_name values[:name]
|
||||
end
|
||||
|
||||
def self.clean_name(name)
|
||||
name.downcase.strip
|
||||
end
|
||||
|
||||
def self.create_unless_exists(name)
|
||||
name = name.downcase.strip
|
||||
name = clean_name name
|
||||
return nil if name == '' || name.nil?
|
||||
dataset.filter(name: name).first || create(name: name)
|
||||
end
|
||||
|
||||
def self.suggestions(name, limit=3)
|
||||
Tag.filter(name: /^#{name}/i).
|
||||
order(:name).
|
||||
limit(limit).
|
||||
all
|
||||
def self.autocomplete(name, limit=3)
|
||||
DB["select tags.name,count(*) as c from sites_tags inner join tags on tags.id=sites_tags.tag_id where tags.name != '' and tags.name LIKE ? group by tags.name having count(*) > 1 order by c desc LIMIT ?", name+'%', limit].all
|
||||
end
|
||||
|
||||
def self.popular_names(limit=10)
|
||||
|
|
|
@ -63,7 +63,7 @@
|
|||
}, {
|
||||
name: 'tags',
|
||||
source: function(query, callback) {
|
||||
$.get('/tags/suggestions/'+query+'.json', function(data) {
|
||||
$.get('/tags/autocomplete/'+query+'.json', function(data) {
|
||||
console.log(data)
|
||||
|
||||
var suggestions = JSON.parse(data)
|
||||
|
|
Loading…
Add table
Reference in a new issue