kristopher-sunspot_rails 0.10.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/LICENSE +18 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +233 -0
  4. data/Rakefile +19 -0
  5. data/VERSION.yml +4 -0
  6. data/dev_tasks/gemspec.rake +29 -0
  7. data/dev_tasks/rdoc.rake +7 -0
  8. data/dev_tasks/release.rake +4 -0
  9. data/dev_tasks/todo.rake +4 -0
  10. data/generators/sunspot/sunspot_generator.rb +9 -0
  11. data/generators/sunspot/templates/sunspot.yml +19 -0
  12. data/install.rb +1 -0
  13. data/lib/sunspot/rails.rb +13 -0
  14. data/lib/sunspot/rails/adapters.rb +54 -0
  15. data/lib/sunspot/rails/configuration.rb +169 -0
  16. data/lib/sunspot/rails/request_lifecycle.rb +18 -0
  17. data/lib/sunspot/rails/searchable.rb +305 -0
  18. data/lib/sunspot/rails/tasks.rb +47 -0
  19. data/rails/init.rb +10 -0
  20. data/spec/configuration_spec.rb +53 -0
  21. data/spec/mock_app/app/controllers/application.rb +10 -0
  22. data/spec/mock_app/app/controllers/application_controller.rb +10 -0
  23. data/spec/mock_app/app/controllers/posts_controller.rb +6 -0
  24. data/spec/mock_app/app/models/blog.rb +2 -0
  25. data/spec/mock_app/app/models/post.rb +5 -0
  26. data/spec/mock_app/app/models/post_with_auto.rb +9 -0
  27. data/spec/mock_app/config/boot.rb +110 -0
  28. data/spec/mock_app/config/database.yml +4 -0
  29. data/spec/mock_app/config/environment.rb +41 -0
  30. data/spec/mock_app/config/environments/development.rb +27 -0
  31. data/spec/mock_app/config/environments/test.rb +27 -0
  32. data/spec/mock_app/config/initializers/new_rails_defaults.rb +19 -0
  33. data/spec/mock_app/config/initializers/session_store.rb +15 -0
  34. data/spec/mock_app/config/routes.rb +43 -0
  35. data/spec/mock_app/config/sunspot.yml +14 -0
  36. data/spec/model_lifecycle_spec.rb +38 -0
  37. data/spec/model_spec.rb +278 -0
  38. data/spec/request_lifecycle_spec.rb +30 -0
  39. data/spec/schema.rb +14 -0
  40. data/spec/spec_helper.rb +32 -0
  41. metadata +184 -0
@@ -0,0 +1,14 @@
1
+ test:
2
+ solr:
3
+ hostname: localhost
4
+ port: 8980
5
+ development:
6
+ solr:
7
+ hostname: localhost
8
+ port: 8981
9
+ config_test:
10
+ solr:
11
+ hostname: some.host
12
+ port: 1234
13
+ path: /solr/idx
14
+ auto_commit_after_request: false
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'searchable with lifecycle' do
4
+ describe 'on create' do
5
+ before :each do
6
+ @post = PostWithAuto.create
7
+ Sunspot.commit
8
+ end
9
+
10
+ it 'should automatically index' do
11
+ PostWithAuto.search.results.should == [@post]
12
+ end
13
+ end
14
+
15
+ describe 'on update' do
16
+ before :each do
17
+ @post = PostWithAuto.create
18
+ @post.update_attributes(:title => 'Test 1')
19
+ Sunspot.commit
20
+ end
21
+
22
+ it 'should automatically update index' do
23
+ PostWithAuto.search { with :title, 'Test 1' }.results.should == [@post]
24
+ end
25
+ end
26
+
27
+ describe 'on destroy' do
28
+ before :each do
29
+ @post = PostWithAuto.create
30
+ @post.destroy
31
+ Sunspot.commit
32
+ end
33
+
34
+ it 'should automatically remove it from the index' do
35
+ PostWithAuto.search_ids.should be_empty
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,278 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'ActiveRecord mixin' do
4
+ describe 'index()' do
5
+ before :each do
6
+ @post = Post.create!
7
+ @post.index
8
+ end
9
+
10
+ it 'should not commit the model' do
11
+ Post.search.results.should be_empty
12
+ end
13
+
14
+ it 'should index the model' do
15
+ Sunspot.commit
16
+ Post.search.results.should == [@post]
17
+ end
18
+ end
19
+
20
+ describe 'index!()' do
21
+ before :each do
22
+ @post = Post.create!
23
+ @post.index!
24
+ end
25
+
26
+ it 'should immediately index and commit' do
27
+ Post.search.results.should == [@post]
28
+ end
29
+ end
30
+
31
+ describe 'remove_from_index()' do
32
+ before :each do
33
+ @post = Post.create!
34
+ @post.index!
35
+ @post.remove_from_index
36
+ end
37
+
38
+ it 'should not commit immediately' do
39
+ Post.search.results.should == [@post]
40
+ end
41
+
42
+ it 'should remove the model from the index' do
43
+ Sunspot.commit
44
+ Post.search.results.should be_empty
45
+ end
46
+ end
47
+
48
+ describe 'remove_from_index!()' do
49
+ before :each do
50
+ @post = Post.create!
51
+ @post.index!
52
+ @post.remove_from_index!
53
+ end
54
+
55
+ it 'should immediately remove the model and commit' do
56
+ Post.search.results.should be_empty
57
+ end
58
+ end
59
+
60
+ describe 'remove_all_from_index' do
61
+ before :each do
62
+ @posts = Array.new(10) { Post.create! }.each { |post| Sunspot.index(post) }
63
+ Sunspot.commit
64
+ Post.remove_all_from_index
65
+ end
66
+
67
+ it 'should not commit immediately' do
68
+ Post.search.results.to_set.should == @posts.to_set
69
+ end
70
+
71
+ it 'should remove all instances from the index' do
72
+ Sunspot.commit
73
+ Post.search.results.should be_empty
74
+ end
75
+ end
76
+
77
+ describe 'remove_all_from_index!' do
78
+ before :each do
79
+ Array.new(10) { Post.create! }.each { |post| Sunspot.index(post) }
80
+ Sunspot.commit
81
+ Post.remove_all_from_index!
82
+ end
83
+
84
+ it 'should remove all instances from the index and commit immediately' do
85
+ Post.search.results.should be_empty
86
+ end
87
+ end
88
+
89
+ describe 'search()' do
90
+ before :each do
91
+ @post = Post.create!(:title => 'Test Post')
92
+ @post.index!
93
+ end
94
+
95
+ it 'should return results specified by search' do
96
+ Post.search do
97
+ with :title, 'Test Post'
98
+ end.results.should == [@post]
99
+ end
100
+
101
+ it 'should not return results excluded by search' do
102
+ Post.search do
103
+ with :title, 'Bogus Post'
104
+ end.results.should be_empty
105
+ end
106
+
107
+ it 'should find ActiveRecord objects with an integer, not a string' do
108
+ Post.should_receive(:find).with([@post.id.to_i]).and_return([@post])
109
+ Post.search do
110
+ with :title, 'Test Post'
111
+ end.results.should == [@post]
112
+ end
113
+ end
114
+
115
+ describe 'search_ids()' do
116
+ before :each do
117
+ @posts = Array.new(2) { Post.create! }.each { |post| post.index }
118
+ Sunspot.commit
119
+ end
120
+
121
+ it 'should return IDs' do
122
+ Post.search_ids.to_set.should == @posts.map { |post| post.id }.to_set
123
+ end
124
+ end
125
+
126
+ describe 'searchable?()' do
127
+ it 'should not be true for models that have not been configured for search' do
128
+ Blog.should_not be_searchable
129
+ end
130
+
131
+ it 'should be true for models that have been configured for search' do
132
+ Post.should be_searchable
133
+ end
134
+ end
135
+
136
+ describe 'index_orphans()' do
137
+ before :each do
138
+ @posts = Array.new(2) { Post.create }.each { |post| post.index }
139
+ Sunspot.commit
140
+ @posts.first.destroy
141
+ end
142
+
143
+ it 'should return IDs of objects that are in the index but not the database' do
144
+ Post.index_orphans.should == [@posts.first.id]
145
+ end
146
+ end
147
+
148
+ describe 'clean_index_orphans()' do
149
+ before :each do
150
+ @posts = Array.new(2) { Post.create }.each { |post| post.index }
151
+ Sunspot.commit
152
+ @posts.first.destroy
153
+ end
154
+
155
+ it 'should remove orphans from the index' do
156
+ Post.clean_index_orphans
157
+ Sunspot.commit
158
+ Post.search.results.should == [@posts.last]
159
+ end
160
+ end
161
+
162
+ describe 'reindex()' do
163
+ before :each do
164
+ @posts = Array.new(2) { Post.create }
165
+ end
166
+
167
+ it 'should index all instances' do
168
+ Post.reindex(:batch_size => nil)
169
+ Sunspot.commit
170
+ Post.search.results.to_set.should == @posts.to_set
171
+ end
172
+
173
+ it 'should remove all currently indexed instances' do
174
+ old_post = Post.create!
175
+ old_post.index!
176
+ old_post.destroy
177
+ Post.reindex
178
+ Sunspot.commit
179
+ Post.search.results.to_set.should == @posts.to_set
180
+ end
181
+
182
+ end
183
+
184
+ describe 'reindex() with real data' do
185
+ before :each do
186
+ @posts = Array.new(2) { Post.create }
187
+ end
188
+
189
+ it 'should index all instances' do
190
+ Post.reindex(:batch_size => nil)
191
+ Sunspot.commit
192
+ Post.search.results.to_set.should == @posts.to_set
193
+ end
194
+
195
+ it 'should remove all currently indexed instances' do
196
+ old_post = Post.create!
197
+ old_post.index!
198
+ old_post.destroy
199
+ Post.reindex
200
+ Sunspot.commit
201
+ Post.search.results.to_set.should == @posts.to_set
202
+ end
203
+
204
+ describe "using batch sizes" do
205
+ it 'should index with a specified batch size' do
206
+ Post.reindex(:batch_size => 1)
207
+ Sunspot.commit
208
+ Post.search.results.to_set.should == @posts.to_set
209
+ end
210
+ end
211
+
212
+ end
213
+
214
+ describe "reindex()" do
215
+
216
+ before(:each) do
217
+ @posts = Array.new(10) { Post.create }
218
+ end
219
+
220
+ describe "when not using batches" do
221
+
222
+ it "should select all if the batch_size is nil" do
223
+ Post.should_receive(:all).with(:include => []).and_return([])
224
+ Post.reindex(:batch_size => nil)
225
+ end
226
+
227
+ it "should search for models with includes" do
228
+ Post.should_receive(:all).with(:include => :author).and_return([])
229
+ Post.reindex(:batch_size => nil, :include => :author)
230
+ end
231
+
232
+ end
233
+
234
+ describe "when using batches" do
235
+
236
+ it "should use the default options" do
237
+ Post.should_receive(:all).with do |params|
238
+ params[:limit].should == 500
239
+ params[:include].should == []
240
+ end.and_return(@posts)
241
+ Post.reindex
242
+ end
243
+
244
+ it "should count the number of records to index" do
245
+ Post.should_receive(:count).and_return(10)
246
+ Post.reindex
247
+ end
248
+
249
+ it "should override the batch_size" do
250
+ Post.should_receive(:all).with do |params|
251
+ params[:limit].should == 20
252
+ @posts
253
+ end.and_return(@posts)
254
+ Post.reindex(:batch_size => 20)
255
+ end
256
+
257
+ it "should set the include option" do
258
+ Post.should_receive(:all).with do |params|
259
+ params[:include].should == [{:author => :address}]
260
+ @posts
261
+ end.and_return(@posts)
262
+ Post.reindex(:include => [{:author => :address}])
263
+ end
264
+
265
+ it "should commit after indexing each batch" do
266
+ Sunspot.should_receive(:commit).twice
267
+ Post.reindex(:batch_size => 5)
268
+ end
269
+
270
+ it "should commit after indexing everything" do
271
+ Sunspot.should_receive(:commit).once
272
+ Post.reindex(:batch_commit => false)
273
+ end
274
+
275
+ end
276
+ end
277
+
278
+ end
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'request lifecycle', :type => :controller do
4
+ before(:each) do
5
+ Sunspot::Rails.configuration = @configuration = Sunspot::Rails::Configuration.new
6
+ end
7
+
8
+ after(:each) do
9
+ Sunspot::Rails.configuration = nil
10
+ end
11
+ controller_name :posts
12
+
13
+ it 'should automatically commit after each action if specified' do
14
+ @configuration.user_configuration = { 'auto_commit_after_request' => true }
15
+ Sunspot.should_receive(:commit_if_dirty)
16
+ post :create, :post => { :title => 'Test 1' }
17
+ end
18
+
19
+ it 'should not commit, if configuration is set to false' do
20
+ @configuration.user_configuration = { 'auto_commit_after_request' => false }
21
+ Sunspot.should_not_receive(:commit_if_dirty)
22
+ post :create, :post => { :title => 'Test 1' }
23
+ end
24
+
25
+ it 'should commit if configuration is not specified' do
26
+ @configuration.user_configuration = {}
27
+ Sunspot.should_receive(:commit_if_dirty)
28
+ post :create, :post => { :title => 'Test 1' }
29
+ end
30
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,14 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :posts, :force => true do |t|
3
+ t.string :title
4
+ t.text :body
5
+ t.references :blog
6
+ t.timestamps
7
+ end
8
+
9
+ create_table :blogs, :force => true do |t|
10
+ t.string :name
11
+ t.string :subdomain
12
+ t.timestamps
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+ ENV['RAILS_ROOT'] ||= File.join(File.dirname(__FILE__), 'mock_app')
3
+
4
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config', 'environment.rb'))
5
+
6
+ require 'spec'
7
+ require 'spec/rails'
8
+ require 'rake'
9
+ require 'ruby-debug' unless RUBY_VERSION =~ /1\.9/
10
+ require 'sunspot/rails/tasks'
11
+
12
+ def load_schema
13
+ stdout = $stdout
14
+ $stdout = StringIO.new # suppress output while building the schema
15
+ load File.join(File.dirname(__FILE__), 'schema.rb')
16
+ $stdout = stdout
17
+ end
18
+
19
+ def silence_stderr(&block)
20
+ stderr = $stderr
21
+ $stderr = StringIO.new
22
+ yield
23
+ $stderr = stderr
24
+ end
25
+
26
+ Spec::Runner.configure do |config|
27
+ config.before(:each) do
28
+ Sunspot.remove_all
29
+ Sunspot.commit
30
+ load_schema
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kristopher-sunspot_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.10.6
5
+ platform: ruby
6
+ authors:
7
+ - Mat Brown
8
+ - Peer Allan
9
+ - Michael Moen
10
+ - Benjamin Krause
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+
15
+ date: 2009-09-01 00:00:00 -07:00
16
+ default_executable:
17
+ dependencies:
18
+ - !ruby/object:Gem::Dependency
19
+ name: rails
20
+ type: :runtime
21
+ version_requirement:
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: "2.1"
27
+ version:
28
+ - !ruby/object:Gem::Dependency
29
+ name: escape
30
+ type: :runtime
31
+ version_requirement:
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 0.0.4
37
+ version:
38
+ - !ruby/object:Gem::Dependency
39
+ name: kristopher-sunspot
40
+ type: :runtime
41
+ version_requirement:
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.9.8
47
+ version:
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ type: :development
51
+ version_requirement:
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: "1.2"
57
+ version:
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec-rails
60
+ type: :development
61
+ version_requirement:
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: "1.2"
67
+ version:
68
+ - !ruby/object:Gem::Dependency
69
+ name: ruby-debug
70
+ type: :development
71
+ version_requirement:
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: "0.10"
77
+ version:
78
+ - !ruby/object:Gem::Dependency
79
+ name: technicalpickles-jeweler
80
+ type: :development
81
+ version_requirement:
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: "1.0"
87
+ version:
88
+ description: Rails integration for the Sunspot Solr search library
89
+ email: mat@patch.com
90
+ executables: []
91
+
92
+ extensions: []
93
+
94
+ extra_rdoc_files:
95
+ - LICENSE
96
+ - README.rdoc
97
+ files:
98
+ - LICENSE
99
+ - MIT-LICENSE
100
+ - README.rdoc
101
+ - Rakefile
102
+ - VERSION.yml
103
+ - dev_tasks/gemspec.rake
104
+ - dev_tasks/rdoc.rake
105
+ - dev_tasks/release.rake
106
+ - dev_tasks/todo.rake
107
+ - generators/sunspot/sunspot_generator.rb
108
+ - generators/sunspot/templates/sunspot.yml
109
+ - install.rb
110
+ - lib/sunspot/rails.rb
111
+ - lib/sunspot/rails/adapters.rb
112
+ - lib/sunspot/rails/configuration.rb
113
+ - lib/sunspot/rails/request_lifecycle.rb
114
+ - lib/sunspot/rails/searchable.rb
115
+ - lib/sunspot/rails/tasks.rb
116
+ - rails/init.rb
117
+ - spec/configuration_spec.rb
118
+ - spec/mock_app/app/controllers/application.rb
119
+ - spec/mock_app/app/controllers/application_controller.rb
120
+ - spec/mock_app/app/controllers/posts_controller.rb
121
+ - spec/mock_app/app/models/blog.rb
122
+ - spec/mock_app/app/models/post.rb
123
+ - spec/mock_app/app/models/post_with_auto.rb
124
+ - spec/mock_app/config/boot.rb
125
+ - spec/mock_app/config/database.yml
126
+ - spec/mock_app/config/environment.rb
127
+ - spec/mock_app/config/environments/development.rb
128
+ - spec/mock_app/config/environments/test.rb
129
+ - spec/mock_app/config/initializers/new_rails_defaults.rb
130
+ - spec/mock_app/config/initializers/session_store.rb
131
+ - spec/mock_app/config/routes.rb
132
+ - spec/mock_app/config/sunspot.yml
133
+ - spec/model_lifecycle_spec.rb
134
+ - spec/model_spec.rb
135
+ - spec/request_lifecycle_spec.rb
136
+ - spec/schema.rb
137
+ - spec/spec_helper.rb
138
+ has_rdoc: false
139
+ homepage: http://github.com/outoftime/sunspot_rails
140
+ licenses:
141
+ post_install_message:
142
+ rdoc_options:
143
+ - --charset=UTF-8
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: "0"
151
+ version:
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: "0"
157
+ version:
158
+ requirements: []
159
+
160
+ rubyforge_project: sunspot
161
+ rubygems_version: 1.3.5
162
+ signing_key:
163
+ specification_version: 3
164
+ summary: Rails integration for the Sunspot Solr search library
165
+ test_files:
166
+ - spec/configuration_spec.rb
167
+ - spec/mock_app/app/controllers/application.rb
168
+ - spec/mock_app/app/controllers/application_controller.rb
169
+ - spec/mock_app/app/controllers/posts_controller.rb
170
+ - spec/mock_app/app/models/blog.rb
171
+ - spec/mock_app/app/models/post.rb
172
+ - spec/mock_app/app/models/post_with_auto.rb
173
+ - spec/mock_app/config/boot.rb
174
+ - spec/mock_app/config/environment.rb
175
+ - spec/mock_app/config/environments/development.rb
176
+ - spec/mock_app/config/environments/test.rb
177
+ - spec/mock_app/config/initializers/new_rails_defaults.rb
178
+ - spec/mock_app/config/initializers/session_store.rb
179
+ - spec/mock_app/config/routes.rb
180
+ - spec/model_lifecycle_spec.rb
181
+ - spec/model_spec.rb
182
+ - spec/request_lifecycle_spec.rb
183
+ - spec/schema.rb
184
+ - spec/spec_helper.rb