Sha256: 9bd5660bfa95186a4b4233520d368fc1581fcb4793274a07a1bd08371f965971

Contents?: true

Size: 981 Bytes

Versions: 7

Compression:

Stored size: 981 Bytes

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
        pg_search_document ? pg_search_document.save : create_pg_search_document
      else
        pg_search_document.destroy if pg_search_document
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pg_search-1.0.4 lib/pg_search/multisearchable.rb
pg_search-1.0.3 lib/pg_search/multisearchable.rb
pg_search-1.0.2 lib/pg_search/multisearchable.rb
pg_search-1.0.1 lib/pg_search/multisearchable.rb
pg_search-1.0.0 lib/pg_search/multisearchable.rb
pg_search-0.7.9 lib/pg_search/multisearchable.rb
pg_search-0.7.3 lib/pg_search/multisearchable.rb