Sha256: c3de84e7a92ef128d1bf4ad93ce026ba24300f4c782238bf9e1d7cda7fd524e4

Contents?: true

Size: 993 Bytes

Versions: 10

Compression:

Stored size: 993 Bytes

Contents

require "active_support/concern"
require "active_support/core_ext/class/attribute"

module PgSearch
  module Multisearchable
    extend ActiveSupport::Concern

    included 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

    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

10 entries across 10 versions & 1 rubygems

Version Path
pg_search-0.7.2 lib/pg_search/multisearchable.rb
pg_search-0.7.1 lib/pg_search/multisearchable.rb
pg_search-0.7.0 lib/pg_search/multisearchable.rb
pg_search-0.6.4 lib/pg_search/multisearchable.rb
pg_search-0.6.3 lib/pg_search/multisearchable.rb
pg_search-0.6.2 lib/pg_search/multisearchable.rb
pg_search-0.6.1 lib/pg_search/multisearchable.rb
pg_search-0.6.0 lib/pg_search/multisearchable.rb
pg_search-0.5.7 lib/pg_search/multisearchable.rb
pg_search-0.5.6 lib/pg_search/multisearchable.rb