diff --git a/README.rdoc b/README.rdoc index 7625e98..ecfde7b 100644 --- a/README.rdoc +++ b/README.rdoc @@ -149,6 +149,13 @@ Example showing changing the min word size: A full rundown of the available configuration options can be found in lib/acts_as_indexed/configuration.rb +=== Multi Model Support + +The module now has a method find_with_index, that takes an array of the Models and the query, e.g.: + + results = ActsAsIndexed.find_with_index([Source, Post], 'try') + + === Heroku Support Acts As Indexed supports Heroku out-of-the-box. The index is created in the diff --git a/lib/acts_as_indexed.rb b/lib/acts_as_indexed.rb index 4f341f8..017f83b 100644 --- a/lib/acts_as_indexed.rb +++ b/lib/acts_as_indexed.rb @@ -25,6 +25,8 @@ module ActsAsIndexed #:nodoc: @configuration = Configuration.new + attr_accessor :search_score + # Returns the current configuration for acts_as_indexed. def self.configuration @@ -48,7 +50,15 @@ def self.configure def self.included(mod) mod.extend(ClassMethods) end - + + def self.find_with_index(models=[], query='', find_options = {}, options = {}) + matches = [] + models.each do |m| + matches << m.find_with_index(query, find_options={}, options={}) + end + matches.flatten.sort { |a,b| a.search_score <=> b.search_score } + end + end # reopen ActiveRecord and include all the above to make diff --git a/lib/acts_as_indexed/class_methods.rb b/lib/acts_as_indexed/class_methods.rb index 97b43f0..3898391 100644 --- a/lib/acts_as_indexed/class_methods.rb +++ b/lib/acts_as_indexed/class_methods.rb @@ -149,7 +149,10 @@ def search_index(query, find_options={}, options={}) ranked_records[r] = @query_cache[query][r.id] end - sort(ranked_records.to_a).map{ |r| r.first } + sort(ranked_records.to_a).map { |r| + r.first.search_score = r.last + r.first + } end end @@ -194,4 +197,4 @@ def new_index end -end \ No newline at end of file +end diff --git a/test/fixtures/source.rb b/test/fixtures/source.rb new file mode 100644 index 0000000..683deba --- /dev/null +++ b/test/fixtures/source.rb @@ -0,0 +1,3 @@ +class Source < ActiveRecord::Base + acts_as_indexed :fields => [:name, :description] +end \ No newline at end of file diff --git a/test/fixtures/sources.yml b/test/fixtures/sources.yml new file mode 100644 index 0000000..671ca31 --- /dev/null +++ b/test/fixtures/sources.yml @@ -0,0 +1,5 @@ +# Just here to provide Multimodel test support +just_a_placeholder_1: + id: 1 + name: Place Holder + description: Just here to provide Multimodel test support \ No newline at end of file diff --git a/test/integration/acts_as_indexed_test.rb b/test/integration/acts_as_indexed_test.rb index 39825ed..d42f673 100644 --- a/test/integration/acts_as_indexed_test.rb +++ b/test/integration/acts_as_indexed_test.rb @@ -1,7 +1,7 @@ require 'integration_test_helper.rb' class ActsAsIndexedTest < ActiveSupport::TestCase - fixtures :posts + fixtures :posts, :sources def teardown # need to do this to work with the :if Proc tests. @@ -288,6 +288,22 @@ def test_records_with_underscores assert_equal [post_with_underscores], Post.find_with_index('try') end + def test_records_with_scores + post_with_score = Post.create(:title => 'Test try it', :body => 'Any old thing') + + assert_not_nil Post.find_with_index('try').first.search_score + end + + def test_multiple_model_support + post = Post.create(:title => 'Test try it', :body => 'Any old thing') + source = Source.create(:name => 'Test try it again!', :description => 'Any old thing but a chicken wing') + + results = ActsAsIndexed.find_with_index([Source, Post], 'try') + + assert results.include? post + assert results.include? source + end + private def run_queries(queries)