Sha256: 88793e2ce2d1170f0492e866b24b2470cfb2c7a4ce106646bb256337b1a0e61f

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 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

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

  module ClassMethods
    def facets(query = nil, options = {})
      merge_search ThinkingSphinx.facets, query, options
    end

    def search(query = nil, options = {})
      merge_search ThinkingSphinx.search, query, options
    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

    def merge_search(search, query, options)
      merger = ThinkingSphinx::Search::Merger.new 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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
thinking-sphinx-3.1.0 lib/thinking_sphinx/active_record/base.rb
thinking-sphinx-3.0.6 lib/thinking_sphinx/active_record/base.rb
thinking-sphinx-3.0.5 lib/thinking_sphinx/active_record/base.rb
thinking-sphinx-3.0.4 lib/thinking_sphinx/active_record/base.rb