Last active
December 18, 2015 17:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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