Class: SearchDocument

Inherits:
ApplicationRecord
  • Object
show all
Includes:
PgSearch::Model
Defined in:
app/models/search_document.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resultsObject



33
34
35
36
# File 'app/models/search_document.rb', line 33

def results
  select(:searchable_id, :searchable_type, :locale)
    .map(&:localized_searchable)
end

.search(query, locale: nil, trigram: false) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'app/models/search_document.rb', line 38

def search(query, locale: nil, trigram: false)
  locale ||= I18n.locale
  scope = where(locale:).includes(:searchable)
  if trigram
    scope.trigram_search_scope(query, search_configuration(locale))
  else
    scope.full_text_search_scope(query, search_configuration(locale))
  end
end

.search_configuration(locale) ⇒ Object



48
49
50
# File 'app/models/search_document.rb', line 48

def search_configuration(locale)
  search_configurations[locale&.to_sym] || "simple_unaccent"
end

.search_configurationsObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/search_document.rb', line 52

def search_configurations
  # These are the dictionaries PostgreSQL 12 ships with.
  # Also available in PostgreSQL 13: el: "greek_unaccent",
  { ar: "arabic_unaccent", da: "danish_unaccent", nl: "dutch_unaccent",
    en: "english_unaccent", fi: "finnish_unaccent", fr: "french_unaccent",
    de: "german_unaccent", hu: "hungarian_unaccent",
    id: "indonesian_unaccent", ga: "irish_unaccent", it: "italian_unaccent",
    lt: "lithuanian_unaccent", ne: "nepali_unaccent",
    nb: "norwegian_unaccent", pt: "portuguese_unaccent",
    rm: "romanian_unaccent", ru: "russian_unaccent", es: "spanish_unaccent",
    sv: "swedish_unaccent", ta: "tamil_unaccent", tr: "turkish_unaccent" }
end

Instance Method Details

#localized_searchableObject



66
67
68
69
70
# File 'app/models/search_document.rb', line 66

def localized_searchable
  return searchable unless searchable.respond_to?(:localize)

  searchable.localize(locale)
end