Files
fizzy/app/models/search/query.rb
T
Donal McBreen fc56ad9d7c Combine uuid-old and uuid branch changes
- Switch to binary 16 for UUID keys
- Remove AccountScopedRecord base class, all model use binary uuids now
- Fix the search sql to serialize uuids properly
- Patch the MySQL schema dumper to output binary lengths
2025-11-17 09:12:34 -05:00

46 lines
851 B
Ruby

class Search::Query < ApplicationRecord
validates :terms, presence: true
before_validation :sanitize_terms
class << self
def wrap(query)
if query.is_a?(self)
query
else
self.new(terms: query)
end
end
end
def to_s
Search::Stemmer.stem(terms.to_s)
end
private
def sanitize_terms
self.terms = sanitize(terms)
end
def sanitize(terms)
if terms.present?
terms = remove_invalid_search_characters(self.terms)
terms = remove_unbalanced_quotes(terms)
terms.presence
else
terms
end
end
def remove_invalid_search_characters(terms)
terms.gsub(/[^\w"]/, " ")
end
def remove_unbalanced_quotes(terms)
if terms.count("\"").even?
terms
else
terms.gsub("\"", " ")
end
end
end