Changeset 14
- Timestamp:
- 03/12/06 11:22:45 (3 years ago)
- Files:
-
- trunk/demo/README_DEMO (added)
- trunk/demo/app/models/comment.rb (modified) (2 diffs)
- trunk/demo/config/database.yml (modified) (2 diffs)
- trunk/demo/db/schema.sqlite (added)
- trunk/demo/test/fixtures/comments.yml (modified) (1 diff)
- trunk/demo/test/test_helper.rb (modified) (1 diff)
- trunk/demo/test/unit/comment_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/demo/app/models/comment.rb
r10 r14 1 require 'ferret' 2 1 3 class Comment < ActiveRecord::Base 2 4 belongs_to :parent, :class_name => 'Content' … … 4 6 acts_as_ferret 5 7 #acts_as_ferret :fields => ['author', 'content' ] 8 9 # you can override the default to_doc method 10 # to customize what gets into your index. 11 def to_doc 12 # doc now has all the fields of our model instance, we 13 # just add another field to it: 14 doc = super 15 # add a field containing the current time 16 doc << Ferret::Document::Field.new( 17 'added', Time.now.to_i.to_s, 18 Ferret::Document::Field::Store::YES, 19 Ferret::Document::Field::Index::UNTOKENIZED) 20 return doc 21 end 6 22 end trunk/demo/config/database.yml
r5 r14 6 6 # And be sure to use new-style password hashing: 7 7 # http://dev.mysql.com/doc/refman/5.0/en/old-client.html 8 # 9 # mysql config 8 10 development: 9 11 adapter: mysql … … 21 23 22 24 # since this is a demo, we won't need this one 23 production:24 adapter: mysql25 database: ferret_production26 username: root27 password:28 socket: /var/run/mysqld/mysqld.sock25 #production: 26 # adapter: mysql 27 # database: ferret_production 28 # username: root 29 # password: 30 # socket: /var/run/mysqld/mysqld.sock 29 31 32 # SQLITE 33 #development: 34 # adapter: sqlite3 35 # dbfile: db/dev.db 36 37 #test: 38 # adapter: sqlite3 39 # dbfile: db/test.db 40 trunk/demo/test/fixtures/comments.yml
r5 r14 2 2 first: 3 3 id: 1 4 content: comment from fixture 4 5 another: 5 6 id: 2 7 content: second comment from fixture trunk/demo/test/test_helper.rb
r5 r14 16 16 # don't care one way or the other, switching from MyISAM to InnoDB tables 17 17 # is recommended. 18 self.use_transactional_fixtures = true 18 #self.use_transactional_fixtures = true 19 self.use_transactional_fixtures = false 19 20 20 21 # Instantiated fixtures are slow, but give you @david where otherwise you trunk/demo/test/unit/comment_test.rb
r10 r14 11 11 def test_class_index_dir 12 12 assert_equal "#{RAILS_ROOT}/index/test/Comment", Comment.class_index_dir 13 end 14 15 # tests the automatic building of an index when none exists 16 # delete index/test/* before running rake to make this useful 17 def test_index_rebuild 18 comments_from_ferret = Comment.find_by_contents('"comment from fixture"') 19 assert_equal 2, comments_from_ferret.size 20 assert comments_from_ferret.include?(comments(:first)) 21 assert comments_from_ferret.include?(comments(:another)) 22 end 23 24 # tests the custom to_doc method defined in comment.rb 25 def test_custom_to_doc 26 top_docs = Comment.ferret_index.search('"comment from fixture"') 27 assert_equal 2, top_docs.score_docs.size 28 doc = Comment.ferret_index.doc(top_docs.score_docs[0].doc) 29 # check for the special field added by the custom to_doc method 30 assert_not_nil doc[:added] 31 # still a valid int ? 32 assert doc[:added].to_i > 0 13 33 end 14 34 … … 45 65 # ...unless you connect them by OR 46 66 comments_from_ferret = Comment.find_by_contents('monkey OR comment') 47 assert_equal 1, comments_from_ferret.size 48 assert_equal comment.id, comments_from_ferret.first.id 67 assert_equal 3, comments_from_ferret.size 68 assert comments_from_ferret.include?(comment) 69 assert comments_from_ferret.include?(comments(:first)) 70 assert comments_from_ferret.include?(comments(:another)) 49 71 50 72 # multiple terms, each term has to occur in a document to be found,
