module PgSearch::Multisearchable

Public Class Methods

included(mod) click to toggle source
# File lib/pg_search/multisearchable.rb, line 7
def self.included(mod)
  mod.class_eval do
    has_one :pg_search_document,
      as: :searchable,
      class_name: "PgSearch::Document",
      dependent: :delete

    after_save :update_pg_search_document,
      if: -> { PgSearch.multisearch_enabled? }
  end
end

Public Instance Methods

create_or_update_pg_search_document() click to toggle source
# File lib/pg_search/multisearchable.rb, line 57
def create_or_update_pg_search_document
  if !pg_search_document
    create_pg_search_document(pg_search_document_attrs)
  elsif should_update_pg_search_document?
    pg_search_document.update(pg_search_document_attrs) # standard:disable Rails/SaveBang
  end
end
pg_search_document_attrs() click to toggle source
# File lib/pg_search/multisearchable.rb, line 25
def pg_search_document_attrs
  {
    content: searchable_text
  }.tap do |h|
    if (attrs = pg_search_multisearchable_options[:additional_attributes])
      h.merge! attrs.to_proc.call(self)
    end
  end
end
searchable_text() click to toggle source
# File lib/pg_search/multisearchable.rb, line 19
def searchable_text
  Array(pg_search_multisearchable_options[:against])
    .map { |symbol| send(symbol) }
    .join(" ")
end
should_update_pg_search_document?() click to toggle source
# File lib/pg_search/multisearchable.rb, line 35
def should_update_pg_search_document?
  return false if pg_search_document.destroyed?

  conditions = Array(pg_search_multisearchable_options[:update_if])
  conditions.all? { |condition| condition.to_proc.call(self) }
end
update_pg_search_document() click to toggle source
# File lib/pg_search/multisearchable.rb, line 42
def update_pg_search_document
  if_conditions = Array(pg_search_multisearchable_options[:if])
  unless_conditions = Array(pg_search_multisearchable_options[:unless])

  should_have_document =
    if_conditions.all? { |condition| condition.to_proc.call(self) } &&
    unless_conditions.all? { |condition| !condition.to_proc.call(self) }

  if should_have_document
    create_or_update_pg_search_document
  else
    pg_search_document&.destroy # standard:disable Rails/SaveBang
  end
end