Sha256: 87c1d6a91894093f5113371e22debd068fe5fc4056f97b0b8975c336b6d64f2b

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

module ThinkingSphinx::ActiveRecord::Base
  extend ActiveSupport::Concern

  included do
    after_destroy ThinkingSphinx::ActiveRecord::Callbacks::DeleteCallbacks
    before_save   ThinkingSphinx::ActiveRecord::Callbacks::DeltaCallbacks
    after_update  ThinkingSphinx::ActiveRecord::Callbacks::UpdateCallbacks
    after_commit  ThinkingSphinx::ActiveRecord::Callbacks::DeltaCallbacks

    after_save    ThinkingSphinx::RealTime::Callbacks::RealTimeCallbacks

    ::ActiveRecord::Associations::CollectionProxy.send :include,
      ThinkingSphinx::ActiveRecord::AssociationProxy
  end

  module ClassMethods
    def facets(query = nil, options = {})
      search = ThinkingSphinx.facets query, options
      ThinkingSphinx::Search::Merger.new(search).merge! nil, :classes => [self]
    end

    def search(query = nil, options = {})
      merger = ThinkingSphinx::Search::Merger.new ThinkingSphinx.search

      merger.merge! *default_sphinx_scope_response if default_sphinx_scope?
      merger.merge! query, options

      if current_scope && !merger.search.options[:ignore_scopes]
        raise ThinkingSphinx::MixedScopesError,
          'You cannot search with Sphinx through ActiveRecord scopes'
      end

      merger.merge! nil, :classes => [self]
    end

    def search_count(query = nil, options = {})
      search(query, options).total_entries
    end

    def search_for_ids(query = nil, options = {})
      search = search query, options
      ThinkingSphinx::Search::Merger.new(search).merge! nil, :ids_only => true
    end

    private

    def default_sphinx_scope?
      respond_to?(:default_sphinx_scope) && default_sphinx_scope
    end

    def default_sphinx_scope_response
      [sphinx_scopes[default_sphinx_scope].call].flatten
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thinking-sphinx-3.0.3 lib/thinking_sphinx/active_record/base.rb