gojee-sunspot-rails 2.0.3 → 2.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/.rspec +1 -0
- data/History.txt +74 -0
- data/LICENSE +18 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +265 -0
- data/Rakefile +17 -0
- data/TODO +8 -0
- data/dev_tasks/rdoc.rake +24 -0
- data/dev_tasks/release.rake +4 -0
- data/dev_tasks/spec.rake +107 -0
- data/dev_tasks/todo.rake +4 -0
- data/gemfiles/rails-2.3.14 +12 -0
- data/gemfiles/rails-3.0.11 +12 -0
- data/gemfiles/rails-3.1.3 +12 -0
- data/gemfiles/rails-3.2.1 +12 -0
- data/generators/sunspot/sunspot_generator.rb +9 -0
- data/generators/sunspot/templates/sunspot.yml +20 -0
- data/install.rb +1 -0
- data/lib/generators/sunspot_rails/install/install_generator.rb +13 -0
- data/lib/generators/sunspot_rails/install/templates/config/sunspot.yml +19 -0
- data/lib/generators/sunspot_rails.rb +9 -0
- data/lib/sunspot/rails/adapters.rb +83 -0
- data/lib/sunspot/rails/configuration.rb +376 -0
- data/lib/sunspot/rails/init.rb +5 -0
- data/lib/sunspot/rails/log_subscriber.rb +33 -0
- data/lib/sunspot/rails/railtie.rb +36 -0
- data/lib/sunspot/rails/railties/controller_runtime.rb +36 -0
- data/lib/sunspot/rails/request_lifecycle.rb +36 -0
- data/lib/sunspot/rails/searchable.rb +491 -0
- data/lib/sunspot/rails/server.rb +114 -0
- data/lib/sunspot/rails/solr_instrumentation.rb +19 -0
- data/lib/sunspot/rails/solr_logging.rb +62 -0
- data/lib/sunspot/rails/spec_helper.rb +26 -0
- data/lib/sunspot/rails/stub_session_proxy.rb +142 -0
- data/lib/sunspot/rails/tasks.rb +84 -0
- data/lib/sunspot/rails.rb +69 -0
- data/lib/sunspot_rails.rb +12 -0
- data/spec/configuration_spec.rb +209 -0
- data/spec/model_lifecycle_spec.rb +63 -0
- data/spec/model_spec.rb +601 -0
- data/spec/rails_template/app/controllers/application_controller.rb +10 -0
- data/spec/rails_template/app/controllers/posts_controller.rb +6 -0
- data/spec/rails_template/app/models/author.rb +8 -0
- data/spec/rails_template/app/models/blog.rb +12 -0
- data/spec/rails_template/app/models/location.rb +2 -0
- data/spec/rails_template/app/models/photo_post.rb +2 -0
- data/spec/rails_template/app/models/post.rb +11 -0
- data/spec/rails_template/app/models/post_with_auto.rb +10 -0
- data/spec/rails_template/app/models/post_with_default_scope.rb +11 -0
- data/spec/rails_template/config/boot.rb +127 -0
- data/spec/rails_template/config/preinitializer.rb +22 -0
- data/spec/rails_template/config/routes.rb +9 -0
- data/spec/rails_template/config/sunspot.yml +24 -0
- data/spec/rails_template/db/schema.rb +27 -0
- data/spec/request_lifecycle_spec.rb +61 -0
- data/spec/schema.rb +27 -0
- data/spec/searchable_spec.rb +12 -0
- data/spec/server_spec.rb +33 -0
- data/spec/session_spec.rb +57 -0
- data/spec/shared_examples/indexed_after_save.rb +8 -0
- data/spec/shared_examples/not_indexed_after_save.rb +8 -0
- data/spec/spec_helper.rb +48 -0
- data/spec/stub_session_proxy_spec.rb +122 -0
- data/sunspot_rails.gemspec +43 -0
- data/tmp/.gitkeep +0 -0
- metadata +97 -5
data/spec/model_spec.rb
ADDED
@@ -0,0 +1,601 @@
|
|
1
|
+
require File.expand_path('spec_helper', File.dirname(__FILE__))
|
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
|
+
|
19
|
+
it "should not blow up if there's a default scope specifying order" do
|
20
|
+
posts = Array.new(2) { |j| PostWithDefaultScope.create! :title => (10-j).to_s }
|
21
|
+
lambda { PostWithDefaultScope.index(:batch_size => 1) }.should_not raise_error
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'single table inheritence' do
|
26
|
+
before :each do
|
27
|
+
@post = PhotoPost.create!
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should not break auto-indexing' do
|
31
|
+
@post.title = 'Title'
|
32
|
+
lambda { @post.save! }.should_not raise_error
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'index!()' do
|
37
|
+
before :each do
|
38
|
+
@post = Post.create!
|
39
|
+
@post.index!
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should immediately index and commit' do
|
43
|
+
Post.search.results.should == [@post]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'remove_from_index()' do
|
48
|
+
before :each do
|
49
|
+
@post = Post.create!
|
50
|
+
@post.index!
|
51
|
+
@post.remove_from_index
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should not commit immediately' do
|
55
|
+
Post.search.results.should == [@post]
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should remove the model from the index' do
|
59
|
+
Sunspot.commit
|
60
|
+
Post.search.results.should be_empty
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe 'remove_from_index!()' do
|
65
|
+
before :each do
|
66
|
+
@post = Post.create!
|
67
|
+
@post.index!
|
68
|
+
@post.remove_from_index!
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should immediately remove the model and commit' do
|
72
|
+
Post.search.results.should be_empty
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'remove_all_from_index' do
|
77
|
+
before :each do
|
78
|
+
@posts = Array.new(2) { Post.create! }.each { |post| Sunspot.index(post) }
|
79
|
+
Sunspot.commit
|
80
|
+
Post.remove_all_from_index
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should not commit immediately' do
|
84
|
+
Post.search.results.to_set.should == @posts.to_set
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should remove all instances from the index' do
|
88
|
+
Sunspot.commit
|
89
|
+
Post.search.results.should be_empty
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe 'remove_all_from_index!' do
|
94
|
+
before :each do
|
95
|
+
Array.new(2) { Post.create! }.each { |post| Sunspot.index(post) }
|
96
|
+
Sunspot.commit
|
97
|
+
Post.remove_all_from_index!
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'should remove all instances from the index and commit immediately' do
|
101
|
+
Post.search.results.should be_empty
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe 'search()' do
|
106
|
+
before :each do
|
107
|
+
@post = Post.create!(:title => 'Test Post')
|
108
|
+
@post.index!
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'should return results specified by search' do
|
112
|
+
Post.search do
|
113
|
+
with :title, 'Test Post'
|
114
|
+
end.results.should == [@post]
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should not return results excluded by search' do
|
118
|
+
Post.search do
|
119
|
+
with :title, 'Bogus Post'
|
120
|
+
end.results.should be_empty
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'should use the include option on the data accessor when specified' do
|
124
|
+
Post.should_receive(:all).with(hash_including(:include => [:blog])).and_return([@post])
|
125
|
+
Post.search do
|
126
|
+
with :title, 'Test Post'
|
127
|
+
data_accessor_for(Post).include = [:blog]
|
128
|
+
end.results.should == [@post]
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should pass :include option from search call to data accessor' do
|
132
|
+
Post.should_receive(:all).with(hash_including(:include => [:blog])).and_return([@post])
|
133
|
+
Post.search(:include => [:blog]) do
|
134
|
+
with :title, 'Test Post'
|
135
|
+
end.results.should == [@post]
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'should use the select option from search call to data accessor' do
|
139
|
+
Post.should_receive(:all).with(hash_including(:select => 'title, published_at')).and_return([@post])
|
140
|
+
Post.search(:select => 'title, published_at') do
|
141
|
+
with :title, 'Test Post'
|
142
|
+
end.results.should == [@post]
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'should not allow bogus options to search' do
|
146
|
+
lambda { Post.search(:bogus => :option) }.should raise_error(ArgumentError)
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'should use the select option on the data accessor when specified' do
|
150
|
+
Post.should_receive(:all).with(hash_including(:select => 'title, published_at')).and_return([@post])
|
151
|
+
Post.search do
|
152
|
+
with :title, 'Test Post'
|
153
|
+
data_accessor_for(Post).select = [:title, :published_at]
|
154
|
+
end.results.should == [@post]
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'should not use the select option on the data accessor when not specified' do
|
158
|
+
Post.should_receive(:all).with(hash_not_including(:select)).and_return([@post])
|
159
|
+
Post.search do
|
160
|
+
with :title, 'Test Post'
|
161
|
+
end.results.should == [@post]
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'should gracefully handle nonexistent records' do
|
165
|
+
post2 = Post.create!(:title => 'Test Post')
|
166
|
+
post2.index!
|
167
|
+
post2.destroy
|
168
|
+
Post.search do
|
169
|
+
with :title, 'Test Post'
|
170
|
+
end.results.should == [@post]
|
171
|
+
end
|
172
|
+
|
173
|
+
it 'should use an ActiveRecord object for coordinates' do
|
174
|
+
post = Post.new(:title => 'Test Post')
|
175
|
+
post.location = Location.create!(:lat => 40.0, :lng => -70.0)
|
176
|
+
post.save
|
177
|
+
post.index!
|
178
|
+
Post.search { with(:location).near(40.0, -70.0) }.results.should == [post]
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
describe 'search_ids()' do
|
184
|
+
before :each do
|
185
|
+
@posts = Array.new(2) { Post.create! }.each { |post| post.index }
|
186
|
+
Sunspot.commit
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'should return IDs' do
|
190
|
+
Post.search_ids.to_set.should == @posts.map { |post| post.id }.to_set
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
describe 'searchable?()' do
|
195
|
+
it 'should not be true for models that have not been configured for search' do
|
196
|
+
Location.should_not be_searchable
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'should be true for models that have been configured for search' do
|
200
|
+
Post.should be_searchable
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
describe 'index_orphans()' do
|
205
|
+
before :each do
|
206
|
+
@posts = Array.new(2) { Post.create }.each { |post| post.index }
|
207
|
+
Sunspot.commit
|
208
|
+
@posts.first.destroy
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'should return IDs of objects that are in the index but not the database' do
|
212
|
+
Post.index_orphans.should == [@posts.first.id]
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'should find the orphans in batches to improve performance' do
|
216
|
+
Post.should_receive(:find_each).with(hash_including(:batch_size => 10)).and_return([])
|
217
|
+
Post.index_orphans(:batch_size => 10)
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|
221
|
+
|
222
|
+
describe 'clean_index_orphans()' do
|
223
|
+
before :each do
|
224
|
+
@posts = Array.new(2) { Post.create }.each { |post| post.index }
|
225
|
+
Sunspot.commit
|
226
|
+
@posts.first.destroy
|
227
|
+
end
|
228
|
+
|
229
|
+
it 'should remove orphans from the index' do
|
230
|
+
Post.clean_index_orphans
|
231
|
+
Sunspot.commit
|
232
|
+
Post.search.results.should == [@posts.last]
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
describe 'reindex()' do
|
237
|
+
before :each do
|
238
|
+
@posts = Array.new(2) { Post.create }
|
239
|
+
end
|
240
|
+
|
241
|
+
it 'should index all instances' do
|
242
|
+
Post.reindex(:batch_size => nil)
|
243
|
+
Sunspot.commit
|
244
|
+
Post.search.results.to_set.should == @posts.to_set
|
245
|
+
end
|
246
|
+
|
247
|
+
it 'should remove all currently indexed instances' do
|
248
|
+
old_post = Post.create!
|
249
|
+
old_post.index!
|
250
|
+
old_post.destroy
|
251
|
+
Post.reindex
|
252
|
+
Sunspot.commit
|
253
|
+
Post.search.results.to_set.should == @posts.to_set
|
254
|
+
end
|
255
|
+
|
256
|
+
end
|
257
|
+
|
258
|
+
describe 'reindex() with real data' do
|
259
|
+
before :each do
|
260
|
+
@posts = Array.new(2) { Post.create }
|
261
|
+
end
|
262
|
+
|
263
|
+
it 'should index all instances' do
|
264
|
+
Post.reindex(:batch_size => nil)
|
265
|
+
Sunspot.commit
|
266
|
+
Post.search.results.to_set.should == @posts.to_set
|
267
|
+
end
|
268
|
+
|
269
|
+
it 'should remove all currently indexed instances' do
|
270
|
+
old_post = Post.create!
|
271
|
+
old_post.index!
|
272
|
+
old_post.destroy
|
273
|
+
Post.reindex
|
274
|
+
Sunspot.commit
|
275
|
+
Post.search.results.to_set.should == @posts.to_set
|
276
|
+
end
|
277
|
+
|
278
|
+
describe "using batch sizes" do
|
279
|
+
it 'should index with a specified batch size' do
|
280
|
+
Post.reindex(:batch_size => 1)
|
281
|
+
Sunspot.commit
|
282
|
+
Post.search.results.to_set.should == @posts.to_set
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
describe "reindex()" do
|
290
|
+
|
291
|
+
before(:each) do
|
292
|
+
@posts = Array.new(2) { Post.create }
|
293
|
+
end
|
294
|
+
|
295
|
+
describe "when not using batches" do
|
296
|
+
|
297
|
+
it "should select all if the batch_size is nil" do
|
298
|
+
Post.should_receive(:all).with(:include => []).and_return([])
|
299
|
+
Post.reindex(:batch_size => nil)
|
300
|
+
end
|
301
|
+
|
302
|
+
it "should search for models with includes" do
|
303
|
+
Post.should_receive(:all).with(:include => :author).and_return([])
|
304
|
+
Post.reindex(:batch_size => nil, :include => :author)
|
305
|
+
end
|
306
|
+
|
307
|
+
describe ':if constraints' do
|
308
|
+
before do
|
309
|
+
Post.sunspot_options[:if] = proc { |model| model.id != @posts.first.id }
|
310
|
+
end
|
311
|
+
|
312
|
+
after do
|
313
|
+
Post.sunspot_options[:if] = nil
|
314
|
+
end
|
315
|
+
|
316
|
+
it 'should only index those models where :if constraints pass' do
|
317
|
+
Post.reindex(:batch_size => nil)
|
318
|
+
|
319
|
+
Post.search.results.should_not include(@posts.first)
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
end
|
324
|
+
|
325
|
+
describe "when using batches" do
|
326
|
+
it "should commit after indexing each batch" do
|
327
|
+
Sunspot.should_receive(:commit).twice
|
328
|
+
Post.reindex(:batch_size => 1)
|
329
|
+
end
|
330
|
+
|
331
|
+
it "should commit after indexing everything" do
|
332
|
+
Sunspot.should_receive(:commit).once
|
333
|
+
Post.reindex(:batch_commit => false)
|
334
|
+
end
|
335
|
+
|
336
|
+
describe ':if constraints' do
|
337
|
+
before do
|
338
|
+
Post.sunspot_options[:if] = proc { |model| model.id != @posts.first.id }
|
339
|
+
end
|
340
|
+
|
341
|
+
after do
|
342
|
+
Post.sunspot_options[:if] = nil
|
343
|
+
end
|
344
|
+
|
345
|
+
it 'should only index those models where :if constraints pass' do
|
346
|
+
Post.reindex(:batch_size => 50)
|
347
|
+
|
348
|
+
Post.search.results.should_not include(@posts.first)
|
349
|
+
end
|
350
|
+
end
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
describe "more_like_this()" do
|
355
|
+
before(:each) do
|
356
|
+
@posts = [
|
357
|
+
Post.create!(:title => 'Post123', :body => "one two three"),
|
358
|
+
Post.create!(:title => 'Post345', :body => "three four five"),
|
359
|
+
Post.create!(:title => 'Post456', :body => "four five six"),
|
360
|
+
Post.create!(:title => 'Post234', :body => "two three four"),
|
361
|
+
]
|
362
|
+
@posts_with_auto = [
|
363
|
+
PostWithAuto.create!(:body => "one two three"),
|
364
|
+
PostWithAuto.create!(:body => "four five six")
|
365
|
+
]
|
366
|
+
@posts.each { |p| p.index! }
|
367
|
+
end
|
368
|
+
|
369
|
+
it "should return results" do
|
370
|
+
@posts.first.more_like_this.results.should == [@posts[3], @posts[1]]
|
371
|
+
end
|
372
|
+
|
373
|
+
it "should return results for specified classes" do
|
374
|
+
@posts.first.more_like_this(Post, PostWithAuto).results.to_set.should ==
|
375
|
+
Set[@posts_with_auto[0], @posts[1], @posts[3]]
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
379
|
+
describe 'more_like_this_ids()' do
|
380
|
+
before :each do
|
381
|
+
@posts = [
|
382
|
+
Post.create!(:title => 'Post123', :body => "one two three"),
|
383
|
+
Post.create!(:title => 'Post345', :body => "three four five"),
|
384
|
+
Post.create!(:title => 'Post456', :body => "four five six"),
|
385
|
+
Post.create!(:title => 'Post234', :body => "two three four"),
|
386
|
+
]
|
387
|
+
@posts.each { |p| p.index! }
|
388
|
+
end
|
389
|
+
|
390
|
+
it 'should return IDs' do
|
391
|
+
@posts.first.more_like_this_ids.to_set.should == [@posts[3], @posts[1]].map { |post| post.id }.to_set
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
describe ':if constraint' do
|
396
|
+
subject do
|
397
|
+
PostWithAuto.new(:title => 'Post123')
|
398
|
+
end
|
399
|
+
|
400
|
+
after do
|
401
|
+
subject.class.sunspot_options[:if] = nil
|
402
|
+
end
|
403
|
+
|
404
|
+
context 'Symbol' do
|
405
|
+
context 'constraint returns true' do
|
406
|
+
# searchable :if => :returns_true
|
407
|
+
before do
|
408
|
+
subject.should_receive(:returns_true).and_return(true)
|
409
|
+
subject.class.sunspot_options[:if] = :returns_true
|
410
|
+
end
|
411
|
+
|
412
|
+
it_should_behave_like 'indexed after save'
|
413
|
+
end
|
414
|
+
|
415
|
+
context 'constraint returns false' do
|
416
|
+
# searchable :if => :returns_false
|
417
|
+
before do
|
418
|
+
subject.should_receive(:returns_false).and_return(false)
|
419
|
+
subject.class.sunspot_options[:if] = :returns_false
|
420
|
+
end
|
421
|
+
|
422
|
+
it_should_behave_like 'not indexed after save'
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
context 'String' do
|
427
|
+
context 'constraint returns true' do
|
428
|
+
# searchable :if => 'returns_true'
|
429
|
+
before do
|
430
|
+
subject.should_receive(:returns_true).and_return(true)
|
431
|
+
subject.class.sunspot_options[:if] = 'returns_true'
|
432
|
+
end
|
433
|
+
|
434
|
+
it_should_behave_like 'indexed after save'
|
435
|
+
end
|
436
|
+
|
437
|
+
context 'constraint returns false' do
|
438
|
+
# searchable :if => 'returns_false'
|
439
|
+
before do
|
440
|
+
subject.should_receive(:returns_false).and_return(false)
|
441
|
+
subject.class.sunspot_options[:if] = 'returns_false'
|
442
|
+
end
|
443
|
+
|
444
|
+
it_should_behave_like 'not indexed after save'
|
445
|
+
end
|
446
|
+
end
|
447
|
+
|
448
|
+
context 'Proc' do
|
449
|
+
context 'constraint returns true' do
|
450
|
+
# searchable :if => proc { true }
|
451
|
+
before do
|
452
|
+
subject.class.sunspot_options[:if] = proc { true }
|
453
|
+
end
|
454
|
+
|
455
|
+
it_should_behave_like 'indexed after save'
|
456
|
+
end
|
457
|
+
|
458
|
+
context 'constraint returns false' do
|
459
|
+
# searchable :if => proc { false }
|
460
|
+
before do
|
461
|
+
subject.class.sunspot_options[:if] = proc { false }
|
462
|
+
end
|
463
|
+
|
464
|
+
it_should_behave_like 'not indexed after save'
|
465
|
+
end
|
466
|
+
end
|
467
|
+
|
468
|
+
context 'Array' do
|
469
|
+
context 'all constraints returns true' do
|
470
|
+
# searchable :if => [:returns_true_1, :returns_true_2]
|
471
|
+
before do
|
472
|
+
subject.should_receive(:returns_true_1).and_return(true)
|
473
|
+
subject.should_receive(:returns_true_2).and_return(true)
|
474
|
+
subject.class.sunspot_options[:if] = [:returns_true_1, 'returns_true_2']
|
475
|
+
end
|
476
|
+
|
477
|
+
it_should_behave_like 'indexed after save'
|
478
|
+
end
|
479
|
+
|
480
|
+
context 'one constraint returns false' do
|
481
|
+
# searchable :if => [:returns_true, :returns_false]
|
482
|
+
before do
|
483
|
+
subject.should_receive(:returns_true).and_return(true)
|
484
|
+
subject.should_receive(:returns_false).and_return(false)
|
485
|
+
subject.class.sunspot_options[:if] = [:returns_true, 'returns_false']
|
486
|
+
end
|
487
|
+
|
488
|
+
it_should_behave_like 'not indexed after save'
|
489
|
+
end
|
490
|
+
end
|
491
|
+
|
492
|
+
it 'removes the model from the index if the constraint does not match' do
|
493
|
+
subject.save!
|
494
|
+
Sunspot.commit
|
495
|
+
subject.class.search.results.should include(subject)
|
496
|
+
|
497
|
+
subject.class.sunspot_options[:if] = proc { false }
|
498
|
+
subject.save!
|
499
|
+
Sunspot.commit
|
500
|
+
subject.class.search.results.should_not include(subject)
|
501
|
+
end
|
502
|
+
end
|
503
|
+
|
504
|
+
describe ':unless constraint' do
|
505
|
+
subject do
|
506
|
+
PostWithAuto.new(:title => 'Post123')
|
507
|
+
end
|
508
|
+
|
509
|
+
after do
|
510
|
+
subject.class.sunspot_options[:unless] = nil
|
511
|
+
end
|
512
|
+
|
513
|
+
context 'Symbol' do
|
514
|
+
context 'constraint returns true' do
|
515
|
+
# searchable :unless => :returns_true
|
516
|
+
before do
|
517
|
+
subject.should_receive(:returns_true).and_return(true)
|
518
|
+
subject.class.sunspot_options[:unless] = :returns_true
|
519
|
+
end
|
520
|
+
|
521
|
+
it_should_behave_like 'not indexed after save'
|
522
|
+
end
|
523
|
+
|
524
|
+
context 'constraint returns false' do
|
525
|
+
# searchable :unless => :returns_false
|
526
|
+
before do
|
527
|
+
subject.should_receive(:returns_false).and_return(false)
|
528
|
+
subject.class.sunspot_options[:unless] = :returns_false
|
529
|
+
end
|
530
|
+
|
531
|
+
it_should_behave_like 'indexed after save'
|
532
|
+
end
|
533
|
+
end
|
534
|
+
|
535
|
+
context 'String' do
|
536
|
+
context 'constraint returns true' do
|
537
|
+
# searchable :unless => 'returns_true'
|
538
|
+
before do
|
539
|
+
subject.should_receive(:returns_true).and_return(true)
|
540
|
+
subject.class.sunspot_options[:unless] = 'returns_true'
|
541
|
+
end
|
542
|
+
|
543
|
+
it_should_behave_like 'not indexed after save'
|
544
|
+
end
|
545
|
+
|
546
|
+
context 'constraint returns false' do
|
547
|
+
# searchable :unless => 'returns_false'
|
548
|
+
before do
|
549
|
+
subject.should_receive(:returns_false).and_return(false)
|
550
|
+
subject.class.sunspot_options[:unless] = 'returns_false'
|
551
|
+
end
|
552
|
+
|
553
|
+
it_should_behave_like 'indexed after save'
|
554
|
+
end
|
555
|
+
end
|
556
|
+
|
557
|
+
context 'Proc' do
|
558
|
+
context 'constraint returns true' do
|
559
|
+
# searchable :unless => proc { true }
|
560
|
+
before do
|
561
|
+
subject.class.sunspot_options[:unless] = proc { |model| model == subject } # true
|
562
|
+
end
|
563
|
+
|
564
|
+
it_should_behave_like 'not indexed after save'
|
565
|
+
end
|
566
|
+
|
567
|
+
context 'constraint returns false' do
|
568
|
+
# searchable :unless => proc { false }
|
569
|
+
before do
|
570
|
+
subject.class.sunspot_options[:unless] = proc { false }
|
571
|
+
end
|
572
|
+
|
573
|
+
it_should_behave_like 'indexed after save'
|
574
|
+
end
|
575
|
+
end
|
576
|
+
|
577
|
+
context 'Array' do
|
578
|
+
context 'all constraints returns true' do
|
579
|
+
# searchable :unless => [:returns_true_1, :returns_true_2]
|
580
|
+
before do
|
581
|
+
subject.should_receive(:returns_true_1).and_return(true)
|
582
|
+
subject.should_receive(:returns_true_2).and_return(true)
|
583
|
+
subject.class.sunspot_options[:unless] = [:returns_true_1, 'returns_true_2']
|
584
|
+
end
|
585
|
+
|
586
|
+
it_should_behave_like 'not indexed after save'
|
587
|
+
end
|
588
|
+
|
589
|
+
context 'one constraint returns false' do
|
590
|
+
# searchable :unless => [:returns_true, :returns_false]
|
591
|
+
before do
|
592
|
+
subject.should_receive(:returns_true).and_return(true)
|
593
|
+
subject.should_receive(:returns_false).and_return(false)
|
594
|
+
subject.class.sunspot_options[:unless] = [:returns_true, 'returns_false']
|
595
|
+
end
|
596
|
+
|
597
|
+
it_should_behave_like 'indexed after save'
|
598
|
+
end
|
599
|
+
end
|
600
|
+
end
|
601
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Filters added to this controller apply to all controllers in the application.
|
2
|
+
# Likewise, all the methods added will be available for all controllers.
|
3
|
+
|
4
|
+
class ApplicationController < ActionController::Base
|
5
|
+
helper :all # include all helpers, all the time
|
6
|
+
protect_from_forgery # See ActionController::RequestForgeryProtection for details
|
7
|
+
|
8
|
+
# Scrub sensitive parameters from your log
|
9
|
+
# filter_parameter_logging :password
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class Blog < ActiveRecord::Base
|
2
|
+
has_many :posts
|
3
|
+
has_many :comments, :through => :posts
|
4
|
+
|
5
|
+
searchable :include => { :posts => :author } do
|
6
|
+
string :subdomain
|
7
|
+
text :name
|
8
|
+
end
|
9
|
+
|
10
|
+
# Make sure that includes are added to with multiple searchable calls
|
11
|
+
searchable(:include => :comments) {}
|
12
|
+
end
|