Skip to content

Commit

Permalink
Making search find calls somewhat generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
pat committed Dec 7, 2009
1 parent 7102009 commit 7dd6213
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
10 changes: 10 additions & 0 deletions lib/thinking_sphinx/active_record.rb
Expand Up @@ -29,6 +29,16 @@ def sphinx_tailor_for(source)
ThinkingSphinx::ActiveRecord::Tailor.new source
end

def find_for_sphinx(ids, options, index_options)
find(:all,
:joins => options[:joins],
:conditions => {primary_key_for_sphinx.to_sym => ids},
:include => (options[:include] || index_options[:include]),
:select => (options[:select] || index_options[:select]),
:order => (options[:sql_order] || index_options[:sql_order])
)
end

private

def add_initial_sphinx_callbacks
Expand Down
8 changes: 8 additions & 0 deletions lib/thinking_sphinx/data_mapper.rb
Expand Up @@ -6,10 +6,18 @@ def self.included(base)
end

module ClassMethods
def primary_key_for_sphinx
:id
end

def sphinx_tailor_for(source)
ThinkingSphinx::DataMapper::Tailor.new source
end

def find_for_sphinx(ids, options, index_options)
all(primary_key_for_sphinx.to_sym => ids)
end

private

def add_initial_sphinx_callbacks
Expand Down
9 changes: 1 addition & 8 deletions lib/thinking_sphinx/search.rb
Expand Up @@ -635,14 +635,7 @@ def instances_from_class(klass, matches)
index_options = klass.sphinx_index_options

ids = matches.collect { |match| match[:attributes]["sphinx_internal_id"] }
instances = ids.length > 0 ? klass.find(
:all,
:joins => options[:joins],
:conditions => {klass.primary_key_for_sphinx.to_sym => ids},
:include => (options[:include] || index_options[:include]),
:select => (options[:select] || index_options[:select]),
:order => (options[:sql_order] || index_options[:sql_order])
) : []
instances = ids.length > 0 ? klass.find_for_sphinx(ids, options, index_options) : []

# Raise an exception if we find records in Sphinx but not in the DB, so
# the search method can retry without them. See
Expand Down
2 changes: 1 addition & 1 deletion thinking-sphinx.gemspec
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Pat Allan"]
s.date = %q{2009-12-05}
s.date = %q{2009-12-07}
s.description = %q{A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching.}
s.email = %q{pat@freelancing-gods.com}
s.extra_rdoc_files = [
Expand Down

0 comments on commit 7dd6213

Please sign in to comment.