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



27
28
29
30
# File 'app/models/search_document.rb', line 27

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

.search(query, locale: nil) ⇒ Object



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

def search(query, locale: nil)
  locale ||= I18n.locale
  where(locale: locale)
    .includes(:searchable)
    .full_text_search_scope(query, search_configuration(locale))
end

.search_configuration(locale) ⇒ Object



39
40
41
# File 'app/models/search_document.rb', line 39

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

.search_configurationsObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/search_document.rb', line 43

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



57
58
59
60
61
# File 'app/models/search_document.rb', line 57

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

  searchable.localize(locale)
end