acts_as_searchable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,153 @@
1
+ require File.join(File.dirname(__FILE__), 'abstract_unit')
2
+ require File.join(File.dirname(__FILE__), 'fixtures/article')
3
+ require File.join(File.dirname(__FILE__), 'fixtures/comment')
4
+
5
+ require 'breakpoint'
6
+ require 'digest/sha1'
7
+
8
+ class ActsAsSearchableTest < Test::Unit::TestCase
9
+ fixtures :articles, :comments
10
+ @@indexed = false
11
+
12
+ def test_defaults
13
+ assert_equal 'test', Comment.estraier_node
14
+ assert_equal 'localhost', Comment.estraier_host
15
+ assert_equal 1978, Comment.estraier_port
16
+ assert_equal 'admin', Comment.estraier_user
17
+ assert_equal 'admin', Comment.estraier_password
18
+ assert_equal [ :body ], Comment.searchable_fields
19
+ end
20
+
21
+ def test_hooks_presence
22
+ assert Article.after_update.include?(:update_index)
23
+ assert Article.after_create.include?(:add_to_index)
24
+ assert Article.after_destroy.include?(:remove_from_index)
25
+ end
26
+
27
+ def test_connection
28
+ assert_kind_of EstraierPure::Node, Article.estraier_connection
29
+ end
30
+
31
+ def test_reindex!
32
+ Article.clear_index!
33
+ @@indexed = false
34
+ assert_equal 0, Article.estraier_index.size
35
+ reindex!
36
+ assert_equal Article.count, Article.estraier_index.size
37
+ end
38
+
39
+ def test_clear_index!
40
+ Article.clear_index!
41
+ assert_equal 0, Article.estraier_index.size
42
+ @@indexed = false
43
+ end
44
+
45
+ def test_after_update_hook
46
+ articles(:first).update_attribute :body, "updated via tests"
47
+ doc = articles(:first).estraier_doc
48
+ assert_equal articles(:first).id.to_s, doc.attr('db_id')
49
+ assert_equal articles(:first).class.to_s, doc.attr('type')
50
+ assert Article.estraier_connection.get_doc(doc.attr('@id')).texts.include?(articles(:first).body)
51
+ end
52
+
53
+ def test_after_create_hook
54
+ a = Article.create :title => "title created via tests", :body => "body created via tests", :tags => "ruby weblog"
55
+ doc = a.estraier_doc
56
+ assert_equal a.id.to_s, doc.attr('db_id')
57
+ assert_equal a.class.to_s, doc.attr('type')
58
+ assert_equal a.tags, doc.attr('custom_attribute')
59
+ assert_equal a.title, doc.attr('@title')
60
+ assert_equal Article.estraier_connection.get_doc(doc.attr('@id')).texts, [ a.title, a.body ]
61
+ end
62
+
63
+ def test_after_destroy_hook
64
+ articles(:first).destroy
65
+ assert articles(:first).estraier_doc.blank?
66
+ end
67
+
68
+ def test_fulltext_search
69
+ reindex!
70
+ assert_equal 1, Article.fulltext_search('mauris').size
71
+ end
72
+
73
+ def test_fulltext_search_with_attributes
74
+ reindex!
75
+ assert_equal [articles(:third), articles(:second)], Article.fulltext_search('', :attributes => "custom_attribute STRINC rails")
76
+ end
77
+
78
+ def test_fulltext_search_with_attributes_array
79
+ reindex!
80
+ assert_equal [articles(:third)], Article.fulltext_search('', :attributes => ["custom_attribute STRINC rails", "@title STRBW lorem"])
81
+ end
82
+
83
+ def test_fulltext_search_with_number_attribute
84
+ reindex!
85
+ assert_equal [articles(:first)], Article.fulltext_search('', :attributes => "comments_count NUMGE 1")
86
+ end
87
+
88
+ def test_fulltext_search_with_date_attribute
89
+ reindex!
90
+ assert_equal [articles(:third)], Article.fulltext_search('ipsum', :attributes => "@cdate NUMLE #{1.year.from_now.xmlschema}")
91
+ end
92
+
93
+ def test_fulltext_search_with_ordering
94
+ reindex!
95
+ assert_equal %w(1 2 3), Article.fulltext_search('', :order => 'db_id NUMA', :raw_matches => true).collect { |d| d.attr('db_id') }
96
+ assert_equal %w(3 2 1), Article.fulltext_search('', :order => 'db_id NUMD', :raw_matches => true).collect { |d| d.attr('db_id') }
97
+ end
98
+
99
+ def test_fulltext_search_with_pagination
100
+ reindex!
101
+ assert_equal %w(1 2), Article.fulltext_search('', :order => 'db_id NUMA', :raw_matches => true, :limit => 2).collect { |d| d.attr('db_id') }
102
+ assert_equal %w(3 2), Article.fulltext_search('', :order => 'db_id NUMD', :raw_matches => true, :limit => 2).collect { |d| d.attr('db_id') }
103
+ assert_equal %w(2 3), Article.fulltext_search('', :order => 'db_id NUMA', :raw_matches => true, :limit => 2, :offset => 1).collect { |d| d.attr('db_id') }
104
+ assert_equal %w(2 1), Article.fulltext_search('', :order => 'db_id NUMD', :raw_matches => true, :limit => 2, :offset => 1).collect { |d| d.attr('db_id') }
105
+ end
106
+
107
+ def test_fulltext_search_with_no_results
108
+ reindex!
109
+ result = Article.fulltext_search('i do not exist')
110
+ assert_kind_of Array, result
111
+ assert_equal 0, result.size
112
+ end
113
+
114
+ def test_fulltext_search_with_find
115
+ reindex!
116
+ assert_equal %w(1 3), Article.fulltext_search('', :find => { :order => "title ASC" }).collect { |a| a.id.to_s }.first(2)
117
+ assert_equal %w(2 3), Article.fulltext_search('', :find => { :order => "title DESC"}).collect { |a| a.id.to_s }.first(2)
118
+ end
119
+
120
+ def test_fulltext_with_invalid_find_parameters
121
+ reindex!
122
+ assert_nothing_raised { Article.fulltext_search('', :limit => 3, :find => { :limit => 1 } ) }
123
+ end
124
+
125
+ def test_act_if_changed
126
+ assert ! comments(:first).changed?
127
+ comments(:first).article_id = 123
128
+ assert comments(:first).changed?
129
+ end
130
+
131
+ def test_act_changed_attributes
132
+ assert ! articles(:first).changed?
133
+ articles(:first).tags = "123" # Covers :attributes
134
+ assert articles(:first).changed?
135
+
136
+ assert ! articles(:second).changed?
137
+ articles(:second).body = "123" # Covers :searchable_fields
138
+ assert articles(:second).changed?
139
+
140
+ assert articles(:second).save
141
+ assert ! articles(:second).changed?
142
+ end
143
+
144
+ protected
145
+
146
+ def reindex!
147
+ unless @@indexed
148
+ Article.reindex!
149
+ @@indexed = true
150
+ sleep 5
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,3 @@
1
+ sqlite:
2
+ :adapter: sqlite
3
+ :dbfile: acts_as_searchable_plugin.sqlite.db
@@ -0,0 +1,5 @@
1
+ class Article < ActiveRecord::Base
2
+ acts_as_searchable :searchable_fields => [ :title, :body ],
3
+ :attributes => { :title => nil, :custom_attribute => :tags, :cdate => :created_at, :comments_count => nil }
4
+ has_many :comments
5
+ end
@@ -0,0 +1,23 @@
1
+ first:
2
+ id: 1
3
+ title: first article
4
+ body: Mauris a quam. Cras ornare lectus quis justo. Duis laoreet facilisis dui. Nunc dui. Proin consectetuer.
5
+ tags: ruby
6
+ comments_count: 1
7
+ created_at: <%= 5.months.ago.to_s :db %>
8
+
9
+ second:
10
+ id: 2
11
+ title: second article
12
+ body: Cras mollis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
13
+ tags: ruby rails
14
+ comments_count: 0
15
+ created_at: <%= 5.days.ago.to_s :db %>
16
+
17
+ third:
18
+ id: 3
19
+ title: lorem ipsum
20
+ body: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin vestibulum vestibulum ipsum.
21
+ tags: ruby rails javascript
22
+ comments_count: 0
23
+ created_at: <%= 5.minutes.ago.to_s :db %>
@@ -0,0 +1,4 @@
1
+ class Comment < ActiveRecord::Base
2
+ acts_as_searchable :if_changed => [ :article_id ]
3
+ belongs_to :article, :counter_cache => true
4
+ end
@@ -0,0 +1,4 @@
1
+ first:
2
+ id: 1
3
+ article_id: 1
4
+ body: article comment
@@ -0,0 +1,14 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :articles, :force => true do |t|
3
+ t.column "title", :string
4
+ t.column "body", :string
5
+ t.column "tags", :string
6
+ t.column "created_at", :datetime
7
+ t.column "comments_count", :integer, :default => 0
8
+ end
9
+
10
+ create_table :comments, :force => true do |t|
11
+ t.column "body", :string
12
+ t.column "article_id", :integer
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: acts_as_searchable
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-04-06 00:00:00 +02:00
8
+ summary: acts_as_searchable adds fulltext searching capabilities based on Hyper Estraier to an ActiveRecord module.
9
+ require_paths:
10
+ - lib
11
+ email: patrick@lenz.sh
12
+ homepage: http://trac.poocs.net/projects/plugins
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: acts_as_searchable
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Patrick Lenz
30
+ files:
31
+ - CHANGELOG
32
+ - html
33
+ - init.rb
34
+ - install.rb
35
+ - lib
36
+ - MIT-LICENSE
37
+ - Rakefile
38
+ - rdoc
39
+ - README
40
+ - tasks
41
+ - test
42
+ - TODO
43
+ - lib/acts_as_searchable.rb
44
+ - lib/vendor
45
+ - lib/vendor/estraierpure.rb
46
+ - lib/vendor/overview
47
+ - rdoc/classes
48
+ - rdoc/created.rid
49
+ - rdoc/files
50
+ - rdoc/fr_class_index.html
51
+ - rdoc/fr_file_index.html
52
+ - rdoc/fr_method_index.html
53
+ - rdoc/index.html
54
+ - rdoc/rdoc-style.css
55
+ - rdoc/classes/ActiveRecord
56
+ - rdoc/classes/EstraierPure
57
+ - rdoc/classes/ActiveRecord/Acts
58
+ - rdoc/classes/ActiveRecord/Acts/Searchable
59
+ - rdoc/classes/ActiveRecord/Acts/Searchable.html
60
+ - rdoc/classes/ActiveRecord/Acts/Searchable/ActMethods.html
61
+ - rdoc/classes/ActiveRecord/Acts/Searchable/ClassMethods.html
62
+ - rdoc/classes/EstraierPure/Condition.html
63
+ - rdoc/classes/EstraierPure/Document.html
64
+ - rdoc/classes/EstraierPure/Node.html
65
+ - rdoc/classes/EstraierPure/NodeResult.html
66
+ - rdoc/classes/EstraierPure/ResultDocument.html
67
+ - rdoc/files/lib
68
+ - rdoc/files/README.html
69
+ - rdoc/files/lib/acts_as_searchable_rb.html
70
+ - rdoc/files/lib/vendor
71
+ - rdoc/files/lib/vendor/estraierpure_rb.html
72
+ - tasks/acts_as_searchable_tasks.rake
73
+ - test/abstract_unit.rb
74
+ - test/acts_as_searchable_test.rb
75
+ - test/database.yml
76
+ - test/fixtures
77
+ - test/mocks
78
+ - test/schema.rb
79
+ - test/fixtures/article.rb
80
+ - test/fixtures/articles.yml
81
+ - test/fixtures/comment.rb
82
+ - test/fixtures/comments.yml
83
+ test_files:
84
+ - test/acts_as_searchable_test.rb
85
+ rdoc_options: []
86
+
87
+ extra_rdoc_files: []
88
+
89
+ executables: []
90
+
91
+ extensions: []
92
+
93
+ requirements: []
94
+
95
+ dependencies: []
96
+