Files
fizzy/app/models/search/query.rb
T
Mike Dalessio fe6df7085f Finish the rollout of account_id to all core data models
Schema:
- add account_id to tables it was missing from
- make account_id a required column everywhere
- add [account_id] indexes, or add `account_id` to existing indices

Models:
- add `belongs_to :account` to all models (default to using a domain
  model's account whenever possible)
- add account_id in all the necessary fixtures
- add account_id to insert_all hashes
- pass account_id to a few initialize calls

Miscellaneous:
- update the import script to set account_id

Note that I'm not adding account_id to the join tables primarily
because I couldn't think of an easy way to populate it without making
it a full Join model, and that was more work than I have time to take
on right now.
2025-11-17 09:12:40 -05:00

49 lines
959 B
Ruby

class Search::Query < ApplicationRecord
belongs_to :account, default: -> { user&.account || Current.account }
belongs_to :user, optional: true
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