Sha256: 0537441856fa22ef0fa8203fb314a63cb31dc18eff93cdb2c54861332a7b0af7

Contents?: true

Size: 986 Bytes

Versions: 4

Compression:

Stored size: 986 Bytes

Contents

require "logger"
require "pg_search/scope"

module PgSearch
  class Document < ActiveRecord::Base
    include PgSearch
    self.table_name = 'pg_search_documents'
    belongs_to :searchable, :polymorphic => true

    before_validation :update_content

    # The logger might not have loaded yet.
    # https://github.com/Casecommons/pg_search/issues/26
    def self.logger
      super || Logger.new(STDERR)
    end

    pg_search_scope :search, lambda { |*args|
      options = if PgSearch.multisearch_options.respond_to?(:call)
        PgSearch.multisearch_options.call(*args)
      else
        PgSearch.multisearch_options.reverse_merge(:query => args.first)
      end
      options.reverse_merge(:against => :content)
    }

    private

    def update_content
      methods = Array.wrap(searchable.pg_search_multisearchable_options[:against])
      searchable_text = methods.map { |symbol| searchable.send(symbol) }.join(" ")
      self.content = searchable_text
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pg_search-0.5.1 lib/pg_search/document.rb
pg_search-0.5 lib/pg_search/document.rb
pg_search-0.4.2 lib/pg_search/document.rb
pg_search-0.4.1 lib/pg_search/document.rb