about summary refs log tree commit diff
path: root/app/models/tag.rb
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2019-08-05 17:58:39 +0200
committerGitHub <noreply@github.com>2019-08-05 17:58:39 +0200
commit0aee74d78a801285ac33647791da9cb1ede9ddf4 (patch)
tree0cf032b9cecdfc4058f59dfaa5c6197e463a5ffe /app/models/tag.rb
parentff0ceb28b3f1b19a6851a482f8203e434e50f167 (diff)
parent68eb58b8058579f551b8aa94e800283cd0f93f7f (diff)
Merge pull request #1187 from ThibG/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/models/tag.rb')
-rw-r--r--app/models/tag.rb9
1 files changed, 6 insertions, 3 deletions
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 46e3a3ec0..c7f0af86d 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -7,6 +7,7 @@
 #  name       :string           default(""), not null
 #  created_at :datetime         not null
 #  updated_at :datetime         not null
+#  score      :integer
 #
 
 class Tag < ApplicationRecord
@@ -75,10 +76,12 @@ class Tag < ApplicationRecord
     end
 
     def search_for(term, limit = 5, offset = 0)
-      pattern = sanitize_sql_like(normalize(term.strip)) + '%'
+      normalized_term = normalize(term.strip).mb_chars.downcase.to_s
+      pattern         = sanitize_sql_like(normalized_term) + '%'
 
-      Tag.where(arel_table[:name].lower.matches(pattern.mb_chars.downcase.to_s))
-         .order(:name)
+      Tag.where(arel_table[:name].lower.matches(pattern))
+         .where(arel_table[:score].gt(0).or(arel_table[:name].lower.eq(normalized_term)))
+         .order(Arel.sql('length(name) ASC, score DESC, name ASC'))
          .limit(limit)
          .offset(offset)
     end