Sha256: 8e50a6e5265e5383df2aa9a6b95f49b9a15899c2b237a57c3fa5adf9af9e9c49

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

require "active_support/core_ext/class/attribute"

module PgSearch
  module Multisearchable
    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 => lambda { PgSearch.multisearch_enabled? }
      end
    end

    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
        unless pg_search_document.present?
          build_pg_search_document.searchable_type = self.class.name
        end
        pg_search_document.save
      else
        pg_search_document.destroy if pg_search_document
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pg_search-0.7.8 lib/pg_search/multisearchable.rb
pg_search-0.7.7 lib/pg_search/multisearchable.rb
pg_search-0.7.6 lib/pg_search/multisearchable.rb
pg_search-0.7.5 lib/pg_search/multisearchable.rb
pg_search-0.7.4 lib/pg_search/multisearchable.rb