pixeltrix-thinking-sphinx 1.1.5 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. data/README.textile +147 -0
  2. data/lib/thinking_sphinx/active_record/attribute_updates.rb +48 -0
  3. data/lib/thinking_sphinx/active_record/delta.rb +14 -1
  4. data/lib/thinking_sphinx/active_record/scopes.rb +37 -0
  5. data/lib/thinking_sphinx/active_record.rb +46 -12
  6. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +9 -1
  7. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +3 -2
  8. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +12 -5
  9. data/lib/thinking_sphinx/association.rb +20 -0
  10. data/lib/thinking_sphinx/attribute.rb +187 -116
  11. data/lib/thinking_sphinx/class_facet.rb +15 -0
  12. data/lib/thinking_sphinx/configuration.rb +46 -14
  13. data/lib/thinking_sphinx/core/string.rb +3 -10
  14. data/lib/thinking_sphinx/deltas/datetime_delta.rb +3 -3
  15. data/lib/thinking_sphinx/deltas/default_delta.rb +9 -6
  16. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +1 -1
  17. data/lib/thinking_sphinx/deltas/delayed_delta.rb +4 -2
  18. data/lib/thinking_sphinx/deltas.rb +14 -6
  19. data/lib/thinking_sphinx/deploy/capistrano.rb +98 -0
  20. data/lib/thinking_sphinx/excerpter.rb +22 -0
  21. data/lib/thinking_sphinx/facet.rb +68 -18
  22. data/lib/thinking_sphinx/facet_search.rb +134 -0
  23. data/lib/thinking_sphinx/field.rb +7 -97
  24. data/lib/thinking_sphinx/index/builder.rb +255 -201
  25. data/lib/thinking_sphinx/index.rb +28 -343
  26. data/lib/thinking_sphinx/property.rb +160 -0
  27. data/lib/thinking_sphinx/rails_additions.rb +7 -4
  28. data/lib/thinking_sphinx/search.rb +593 -587
  29. data/lib/thinking_sphinx/search_methods.rb +421 -0
  30. data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
  31. data/lib/thinking_sphinx/source/sql.rb +128 -0
  32. data/lib/thinking_sphinx/source.rb +150 -0
  33. data/lib/thinking_sphinx/tasks.rb +45 -11
  34. data/lib/thinking_sphinx.rb +88 -14
  35. data/rails/init.rb +14 -0
  36. data/spec/{unit → lib}/thinking_sphinx/active_record/delta_spec.rb +7 -7
  37. data/spec/{unit → lib}/thinking_sphinx/active_record/has_many_association_spec.rb +0 -0
  38. data/spec/lib/thinking_sphinx/active_record/scopes_spec.rb +92 -0
  39. data/spec/{unit → lib}/thinking_sphinx/active_record_spec.rb +115 -42
  40. data/spec/{unit → lib}/thinking_sphinx/association_spec.rb +4 -5
  41. data/spec/lib/thinking_sphinx/attribute_spec.rb +465 -0
  42. data/spec/{unit → lib}/thinking_sphinx/configuration_spec.rb +118 -7
  43. data/spec/{unit → lib}/thinking_sphinx/core/string_spec.rb +0 -0
  44. data/spec/lib/thinking_sphinx/excerpter_spec.rb +49 -0
  45. data/spec/lib/thinking_sphinx/facet_search_spec.rb +176 -0
  46. data/spec/lib/thinking_sphinx/facet_spec.rb +302 -0
  47. data/spec/{unit → lib}/thinking_sphinx/field_spec.rb +26 -17
  48. data/spec/lib/thinking_sphinx/index/builder_spec.rb +355 -0
  49. data/spec/{unit → lib}/thinking_sphinx/index/faux_column_spec.rb +0 -0
  50. data/spec/{unit → lib}/thinking_sphinx/index_spec.rb +3 -12
  51. data/spec/lib/thinking_sphinx/rails_additions_spec.rb +191 -0
  52. data/spec/lib/thinking_sphinx/search_methods_spec.rb +152 -0
  53. data/spec/lib/thinking_sphinx/search_spec.rb +887 -0
  54. data/spec/lib/thinking_sphinx/source_spec.rb +217 -0
  55. data/spec/{unit → lib}/thinking_sphinx_spec.rb +30 -8
  56. data/tasks/distribution.rb +20 -1
  57. data/tasks/testing.rb +7 -15
  58. data/vendor/after_commit/init.rb +3 -0
  59. data/vendor/after_commit/lib/after_commit/active_record.rb +27 -4
  60. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +1 -1
  61. data/vendor/after_commit/lib/after_commit.rb +4 -1
  62. data/vendor/riddle/lib/riddle/client/message.rb +4 -3
  63. data/vendor/riddle/lib/riddle/client.rb +3 -0
  64. data/vendor/riddle/lib/riddle/configuration/section.rb +8 -2
  65. data/vendor/riddle/lib/riddle/controller.rb +1 -1
  66. data/vendor/riddle/lib/riddle.rb +1 -1
  67. metadata +75 -39
  68. data/README +0 -107
  69. data/lib/thinking_sphinx/active_record/search.rb +0 -57
  70. data/lib/thinking_sphinx/collection.rb +0 -142
  71. data/lib/thinking_sphinx/facet_collection.rb +0 -44
  72. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +0 -107
  73. data/spec/unit/thinking_sphinx/attribute_spec.rb +0 -212
  74. data/spec/unit/thinking_sphinx/collection_spec.rb +0 -14
  75. data/spec/unit/thinking_sphinx/index/builder_spec.rb +0 -5
  76. data/spec/unit/thinking_sphinx/search_spec.rb +0 -59
@@ -3,7 +3,7 @@ require 'spec/spec_helper'
3
3
  describe "ThinkingSphinx::ActiveRecord" do
4
4
  describe "define_index method" do
5
5
  before :each do
6
- module TestModule
6
+ module ::TestModule
7
7
  class TestModel < ActiveRecord::Base; end
8
8
  end
9
9
 
@@ -14,7 +14,7 @@ describe "ThinkingSphinx::ActiveRecord" do
14
14
  )
15
15
 
16
16
  @index = ThinkingSphinx::Index.stub_instance(:delta? => false)
17
- ThinkingSphinx::Index.stub_method(:new => @index)
17
+ ThinkingSphinx::Index::Builder.stub_method(:generate => @index)
18
18
  end
19
19
 
20
20
  after :each do
@@ -24,17 +24,17 @@ describe "ThinkingSphinx::ActiveRecord" do
24
24
  ThinkingSphinx.indexed_models.delete "TestModule::TestModel"
25
25
  end
26
26
 
27
- it "should return nil and do nothing if indexes are disabled" do
27
+ it "should do nothing if indexes are disabled" do
28
28
  ThinkingSphinx.stub_method(:define_indexes? => false)
29
29
 
30
- TestModule::TestModel.define_index {}.should be_nil
30
+ TestModule::TestModel.define_index {}
31
31
  ThinkingSphinx::Index.should_not have_received(:new)
32
32
 
33
33
  ThinkingSphinx.unstub_method(:define_indexes?)
34
34
  end
35
35
 
36
36
  it "should add a new index to the model" do
37
- TestModule::TestModel.define_index do; end
37
+ TestModule::TestModel.define_index {}
38
38
 
39
39
  TestModule::TestModel.sphinx_indexes.length.should == 1
40
40
  end
@@ -64,15 +64,15 @@ describe "ThinkingSphinx::ActiveRecord" do
64
64
 
65
65
  TestModule::TestModel.define_index do; end
66
66
 
67
- TestModule::TestModel.should have_received(:before_save)
68
- TestModule::TestModel.should have_received(:after_commit)
67
+ TestModule::TestModel.should have_received(:before_save).with(:toggle_delta)
68
+ TestModule::TestModel.should have_received(:after_commit).with(:index_delta)
69
69
  end
70
70
 
71
71
  it "should not add before_save and after_commit hooks to the model if delta indexing is disabled" do
72
72
  TestModule::TestModel.define_index do; end
73
73
 
74
- TestModule::TestModel.should_not have_received(:before_save)
75
- TestModule::TestModel.should_not have_received(:after_commit)
74
+ TestModule::TestModel.should_not have_received(:before_save).with(:toggle_delta)
75
+ TestModule::TestModel.should_not have_received(:after_commit).with(:index_delta)
76
76
  end
77
77
 
78
78
  it "should add an after_destroy hook with delta indexing enabled" do
@@ -92,6 +92,76 @@ describe "ThinkingSphinx::ActiveRecord" do
92
92
  it "should return the new index" do
93
93
  TestModule::TestModel.define_index.should == @index
94
94
  end
95
+
96
+ it "should die quietly if there is a database error" do
97
+ ThinkingSphinx::Index::Builder.stub_method_to_raise(:generate => Mysql::Error)
98
+
99
+ lambda {
100
+ TestModule::TestModel.define_index
101
+ }.should_not raise_error
102
+ end
103
+
104
+ it "should die noisily if there is a non-database error" do
105
+ ThinkingSphinx::Index::Builder.stub_method_to_raise(:generate => StandardError)
106
+
107
+ lambda {
108
+ TestModule::TestModel.define_index
109
+ }.should raise_error
110
+ end
111
+ end
112
+
113
+ describe "index methods" do
114
+ before(:all) do
115
+ @person = Person.find(:first)
116
+ end
117
+
118
+ describe "in_both_indexes?" do
119
+ it "should return true if in core and delta indexes" do
120
+ @person.should_receive(:in_core_index?).and_return(true)
121
+ @person.should_receive(:in_delta_index?).and_return(true)
122
+ @person.in_both_indexes?.should be_true
123
+ end
124
+
125
+ it "should return false if in one index and not the other" do
126
+ @person.should_receive(:in_core_index?).and_return(true)
127
+ @person.should_receive(:in_delta_index?).and_return(false)
128
+ @person.in_both_indexes?.should be_false
129
+ end
130
+ end
131
+
132
+ describe "in_core_index?" do
133
+ it "should call in_index? with core" do
134
+ @person.should_receive(:in_index?).with('core')
135
+ @person.in_core_index?
136
+ end
137
+ end
138
+
139
+ describe "in_delta_index?" do
140
+ it "should call in_index? with delta" do
141
+ @person.should_receive(:in_index?).with('delta')
142
+ @person.in_delta_index?
143
+ end
144
+ end
145
+
146
+ describe "in_index?" do
147
+ it "should return true if in the specified index" do
148
+ @person.should_receive(:sphinx_document_id).and_return(1)
149
+ @person.should_receive(:sphinx_index_name).and_return('person_core')
150
+ Person.should_receive(:search_for_id).with(1, 'person_core').and_return(true)
151
+
152
+ @person.in_index?('core').should be_true
153
+ end
154
+ end
155
+ end
156
+
157
+ describe "source_of_sphinx_index method" do
158
+ it "should return self if model defines an index" do
159
+ Person.source_of_sphinx_index.should == Person
160
+ end
161
+
162
+ it "should return the parent if model inherits an index" do
163
+ Admin::Person.source_of_sphinx_index.should == Person
164
+ end
95
165
  end
96
166
 
97
167
  describe "to_crc32 method" do
@@ -100,6 +170,12 @@ describe "ThinkingSphinx::ActiveRecord" do
100
170
  end
101
171
  end
102
172
 
173
+ describe "to_crc32s method" do
174
+ it "should return an array" do
175
+ Person.to_crc32s.should be_a_kind_of(Array)
176
+ end
177
+ end
178
+
103
179
  describe "toggle_deleted method" do
104
180
  before :each do
105
181
  ThinkingSphinx.stub_method(:sphinx_running? => true)
@@ -109,7 +185,8 @@ describe "ThinkingSphinx::ActiveRecord" do
109
185
  :address => "an address",
110
186
  :port => 123
111
187
  )
112
- @client = Riddle::Client.stub_instance(:update => true)
188
+ @client = Riddle::Client.new
189
+ @client.stub!(:update => true)
113
190
  @person = Person.find(:first)
114
191
 
115
192
  Riddle::Client.stub_method(:new => @client)
@@ -126,87 +203,82 @@ describe "ThinkingSphinx::ActiveRecord" do
126
203
  end
127
204
 
128
205
  it "should update the core index's deleted flag if in core index" do
129
- @person.toggle_deleted
130
-
131
- @client.should have_received(:update).with(
206
+ @client.should_receive(:update).with(
132
207
  "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
133
208
  )
209
+
210
+ @person.toggle_deleted
134
211
  end
135
212
 
136
213
  it "shouldn't update the core index's deleted flag if the record isn't in it" do
137
214
  @person.stub_method(:in_core_index? => false)
138
-
139
- @person.toggle_deleted
140
-
141
- @client.should_not have_received(:update).with(
215
+ @client.should_not_receive(:update).with(
142
216
  "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
143
217
  )
218
+
219
+ @person.toggle_deleted
144
220
  end
145
221
 
146
222
  it "shouldn't attempt to update the deleted flag if sphinx isn't running" do
147
223
  ThinkingSphinx.stub_method(:sphinx_running? => false)
224
+ @client.should_not_receive(:update)
148
225
 
149
226
  @person.toggle_deleted
150
227
 
151
228
  @person.should_not have_received(:in_core_index?)
152
- @client.should_not have_received(:update)
153
229
  end
154
230
 
155
231
  it "should update the delta index's deleted flag if delta indexes are enabled and the instance's delta is true" do
156
232
  ThinkingSphinx.stub_method(:deltas_enabled? => true)
157
233
  Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
158
234
  @person.delta = true
159
-
160
- @person.toggle_deleted
161
-
162
- @client.should have_received(:update).with(
235
+ @client.should_receive(:update).with(
163
236
  "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
164
237
  )
238
+
239
+ @person.toggle_deleted
165
240
  end
166
241
 
167
242
  it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is false" do
168
243
  ThinkingSphinx.stub_method(:deltas_enabled? => true)
169
244
  Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
170
245
  @person.delta = false
171
-
172
- @person.toggle_deleted
173
-
174
- @client.should_not have_received(:update).with(
246
+ @client.should_not_receive(:update).with(
175
247
  "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
176
248
  )
249
+
250
+ @person.toggle_deleted
177
251
  end
178
252
 
179
253
  it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is equivalent to false" do
180
254
  ThinkingSphinx.stub_method(:deltas_enabled? => true)
181
255
  Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
182
256
  @person.delta = 0
183
-
184
- @person.toggle_deleted
185
-
186
- @client.should_not have_received(:update).with(
257
+ @client.should_not_receive(:update).with(
187
258
  "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
188
259
  )
260
+
261
+ @person.toggle_deleted
189
262
  end
190
263
 
191
264
  it "shouldn't update the delta index if delta indexes are disabled" do
192
265
  ThinkingSphinx.stub_method(:deltas_enabled? => true)
193
- @person.toggle_deleted
194
-
195
- @client.should_not have_received(:update).with(
266
+ @client.should_not_receive(:update).with(
196
267
  "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
197
268
  )
269
+
270
+ @person.toggle_deleted
198
271
  end
199
272
 
200
273
  it "should not update the delta index if delta indexing is disabled" do
201
274
  ThinkingSphinx.stub_method(:deltas_enabled? => false)
202
275
  Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
203
276
  @person.delta = true
204
-
205
- @person.toggle_deleted
206
-
207
- @client.should_not have_received(:update).with(
277
+ @client.should_not_receive(:update).with(
208
278
  "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
209
279
  )
280
+
281
+ @person.toggle_deleted
210
282
  end
211
283
 
212
284
  it "should not update either index if updates are disabled" do
@@ -216,10 +288,9 @@ describe "ThinkingSphinx::ActiveRecord" do
216
288
  )
217
289
  Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
218
290
  @person.delta = true
291
+ @client.should_not_receive(:update)
219
292
 
220
293
  @person.toggle_deleted
221
-
222
- @client.should_not have_received(:update)
223
294
  end
224
295
  end
225
296
 
@@ -229,10 +300,12 @@ describe "ThinkingSphinx::ActiveRecord" do
229
300
  end
230
301
 
231
302
  it "should allow associations to other STI models" do
232
- Child.sphinx_indexes.last.link!
233
- sql = Child.sphinx_indexes.last.to_riddle_for_core(0, 0).sql_query
303
+ source = Child.sphinx_indexes.last.sources.first
304
+ sql = source.to_riddle_for_core(0, 0).sql_query
234
305
  sql.gsub!('$start', '0').gsub!('$end', '100')
235
- lambda { Child.connection.execute(sql) }.should_not raise_error(ActiveRecord::StatementInvalid)
306
+ lambda {
307
+ Child.connection.execute(sql)
308
+ }.should_not raise_error(ActiveRecord::StatementInvalid)
236
309
  end
237
310
  end
238
311
 
@@ -79,7 +79,8 @@ describe ThinkingSphinx::Association do
79
79
  before :each do
80
80
  @parent_join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
81
81
  @join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
82
- @parent = ThinkingSphinx::Association.stub_instance(:join_to => true, :join => nil)
82
+ @parent = ThinkingSphinx::Association.new(nil, nil)
83
+ @parent.stub!(:join_to => true, :join => nil)
83
84
  @base_join = Object.stub_instance(:joins => [:a, :b, :c])
84
85
 
85
86
  ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_method(:new => @join)
@@ -87,19 +88,17 @@ describe ThinkingSphinx::Association do
87
88
 
88
89
  it "should call the parent's join_to if parent has no join" do
89
90
  @assoc = ThinkingSphinx::Association.new(@parent, :ref)
91
+ @parent.should_receive(:join_to).with(@base_join)
90
92
 
91
93
  @assoc.join_to(@base_join)
92
-
93
- @parent.should have_received(:join_to).with(@base_join)
94
94
  end
95
95
 
96
96
  it "should not call the parent's join_to if it already has a join" do
97
97
  @assoc = ThinkingSphinx::Association.new(@parent, :ref)
98
98
  @parent.stub_method(:join => @parent_join)
99
+ @parent.should_not_receive(:join_to)
99
100
 
100
101
  @assoc.join_to(@base_join)
101
-
102
- @parent.should_not have_received(:join_to)
103
102
  end
104
103
 
105
104
  it "should define the join association with a JoinAssociation instance" do