这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ Example showing changing the min word size:
A full rundown of the available configuration options can be found in
<tt>lib/acts_as_indexed/configuration.rb</tt>

=== 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
Expand Down
12 changes: 11 additions & 1 deletion lib/acts_as_indexed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lib/acts_as_indexed/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -194,4 +197,4 @@ def new_index

end

end
end
3 changes: 3 additions & 0 deletions test/fixtures/source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Source < ActiveRecord::Base
acts_as_indexed :fields => [:name, :description]
end
5 changes: 5 additions & 0 deletions test/fixtures/sources.yml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 17 additions & 1 deletion test/integration/acts_as_indexed_test.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
Expand Down