use Sqlite FTS5 for search, assume correct language codes
This commit is contained in:
@@ -9,4 +9,36 @@ class Entry < ApplicationRecord
|
||||
enum :category, %i[word phrase proper_name title reference other]
|
||||
|
||||
validates :category, presence: true
|
||||
|
||||
scope :with_category, ->(cat) { cat.present? ? where(category: cat) : all }
|
||||
|
||||
def self.search(query, language_code: nil)
|
||||
return all if query.blank?
|
||||
|
||||
prefix = valid_lang?(language_code) ? "#{language_code}:" : ""
|
||||
fts_query = "#{prefix}\"#{query.to_s.gsub('"', '""')}\""
|
||||
|
||||
joins("JOIN entries_fts ON entries_fts.rowid = entries.id")
|
||||
.where("entries_fts MATCH ?", fts_query)
|
||||
end
|
||||
|
||||
def self.starts_with(prefix, language_code:)
|
||||
return none unless valid_lang?(language_code)
|
||||
return all if prefix.blank?
|
||||
|
||||
where("#{language_code} LIKE ?", "#{sanitize_sql_like(prefix)}%")
|
||||
end
|
||||
|
||||
def self.alphabetical_for(language_code)
|
||||
return none unless valid_lang?(language_code)
|
||||
|
||||
where.not(language_code => [ nil, "" ])
|
||||
.order(Arel.sql("#{language_code} ASC"))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.valid_lang?(code)
|
||||
SupportedLanguage.valid_codes.include?(code.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user