Skip to content

Instantly share code, notes, and snippets.

@mmrwoods
Last active December 18, 2015 17:49
Show Gist options
  • Save mmrwoods/5820789 to your computer and use it in GitHub Desktop.
Save mmrwoods/5820789 to your computer and use it in GitHub Desktop.
Adds metadata to pgsearch documents, allowing search results to be rendered with just the document data.
module PgSearch
module ClassMethods
def rebuild_pg_search_documents
self.find_each {|record| record.update_pg_search_document}
end
end
class Document
before_validation do
self.meta = generate_meta
end
def meta
@meta_hash ||= HashWithIndifferentAccess.new(
ActiveSupport::JSON.decode(self[:meta])
)
end
def meta=(data)
self[:meta] = ActiveSupport::JSON.encode(data)
end
def generate_meta
against = searchable.pg_search_multisearchable_options[:against]
against.inject({}) do |memo, method|
memo.merge({method => searchable.send(method)})
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment