ebeigarts-thinking-sphinx 1.1.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/LICENCE +20 -0
  2. data/README.textile +143 -0
  3. data/lib/thinking_sphinx.rb +217 -0
  4. data/lib/thinking_sphinx/active_record.rb +278 -0
  5. data/lib/thinking_sphinx/active_record/attribute_updates.rb +48 -0
  6. data/lib/thinking_sphinx/active_record/delta.rb +87 -0
  7. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  8. data/lib/thinking_sphinx/active_record/search.rb +57 -0
  9. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +53 -0
  10. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +54 -0
  11. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +135 -0
  12. data/lib/thinking_sphinx/association.rb +164 -0
  13. data/lib/thinking_sphinx/attribute.rb +269 -0
  14. data/lib/thinking_sphinx/class_facet.rb +15 -0
  15. data/lib/thinking_sphinx/collection.rb +148 -0
  16. data/lib/thinking_sphinx/configuration.rb +275 -0
  17. data/lib/thinking_sphinx/core/string.rb +15 -0
  18. data/lib/thinking_sphinx/deltas.rb +30 -0
  19. data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
  20. data/lib/thinking_sphinx/deltas/default_delta.rb +68 -0
  21. data/lib/thinking_sphinx/deltas/delayed_delta.rb +27 -0
  22. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
  23. data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
  24. data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
  25. data/lib/thinking_sphinx/deploy/capistrano.rb +82 -0
  26. data/lib/thinking_sphinx/facet.rb +108 -0
  27. data/lib/thinking_sphinx/facet_collection.rb +59 -0
  28. data/lib/thinking_sphinx/field.rb +82 -0
  29. data/lib/thinking_sphinx/index.rb +99 -0
  30. data/lib/thinking_sphinx/index/builder.rb +287 -0
  31. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  32. data/lib/thinking_sphinx/property.rb +160 -0
  33. data/lib/thinking_sphinx/rails_additions.rb +136 -0
  34. data/lib/thinking_sphinx/search.rb +727 -0
  35. data/lib/thinking_sphinx/search/facets.rb +104 -0
  36. data/lib/thinking_sphinx/source.rb +175 -0
  37. data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
  38. data/lib/thinking_sphinx/source/sql.rb +126 -0
  39. data/lib/thinking_sphinx/tasks.rb +245 -0
  40. data/rails/init.rb +14 -0
  41. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +136 -0
  42. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  43. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
  44. data/spec/unit/thinking_sphinx/active_record_spec.rb +329 -0
  45. data/spec/unit/thinking_sphinx/association_spec.rb +246 -0
  46. data/spec/unit/thinking_sphinx/attribute_spec.rb +338 -0
  47. data/spec/unit/thinking_sphinx/collection_spec.rb +15 -0
  48. data/spec/unit/thinking_sphinx/configuration_spec.rb +222 -0
  49. data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
  50. data/spec/unit/thinking_sphinx/facet_collection_spec.rb +64 -0
  51. data/spec/unit/thinking_sphinx/facet_spec.rb +302 -0
  52. data/spec/unit/thinking_sphinx/field_spec.rb +154 -0
  53. data/spec/unit/thinking_sphinx/index/builder_spec.rb +355 -0
  54. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +30 -0
  55. data/spec/unit/thinking_sphinx/index_spec.rb +45 -0
  56. data/spec/unit/thinking_sphinx/rails_additions_spec.rb +191 -0
  57. data/spec/unit/thinking_sphinx/search_spec.rb +228 -0
  58. data/spec/unit/thinking_sphinx/source_spec.rb +217 -0
  59. data/spec/unit/thinking_sphinx_spec.rb +151 -0
  60. data/tasks/distribution.rb +67 -0
  61. data/tasks/rails.rake +1 -0
  62. data/tasks/testing.rb +100 -0
  63. data/vendor/after_commit/LICENSE +20 -0
  64. data/vendor/after_commit/README +16 -0
  65. data/vendor/after_commit/Rakefile +22 -0
  66. data/vendor/after_commit/init.rb +8 -0
  67. data/vendor/after_commit/lib/after_commit.rb +45 -0
  68. data/vendor/after_commit/lib/after_commit/active_record.rb +114 -0
  69. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
  70. data/vendor/after_commit/test/after_commit_test.rb +53 -0
  71. data/vendor/delayed_job/lib/delayed/job.rb +251 -0
  72. data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
  73. data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
  74. data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
  75. data/vendor/riddle/lib/riddle.rb +30 -0
  76. data/vendor/riddle/lib/riddle/client.rb +619 -0
  77. data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
  78. data/vendor/riddle/lib/riddle/client/message.rb +65 -0
  79. data/vendor/riddle/lib/riddle/client/response.rb +84 -0
  80. data/vendor/riddle/lib/riddle/configuration.rb +33 -0
  81. data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
  82. data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
  83. data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
  84. data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
  85. data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
  86. data/vendor/riddle/lib/riddle/configuration/section.rb +43 -0
  87. data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
  88. data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
  89. data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
  90. data/vendor/riddle/lib/riddle/controller.rb +44 -0
  91. metadata +191 -0
@@ -0,0 +1,107 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe "ThinkingSphinx::ActiveRecord::Search" do
4
+ it "should add search_for_ids to ActiveRecord::Base" do
5
+ ActiveRecord::Base.should respond_to("search_for_ids")
6
+ end
7
+
8
+ it "should add search_for_ids to ActiveRecord::Base" do
9
+ ActiveRecord::Base.should respond_to("search")
10
+ end
11
+
12
+ it "should add search_count to ActiveRecord::Base" do
13
+ ActiveRecord::Base.should respond_to("search_count")
14
+ end
15
+
16
+ it "should add search_for_id to ActiveRecord::Base" do
17
+ ActiveRecord::Base.should respond_to("search_for_id")
18
+ end
19
+
20
+ describe "search_for_ids method" do
21
+ before :each do
22
+ ThinkingSphinx::Search.stub_method(:search_for_ids => true)
23
+ end
24
+
25
+ it "should call ThinkingSphinx::Search#search_for_ids with the class option set" do
26
+ Person.search_for_ids("search")
27
+
28
+ ThinkingSphinx::Search.should have_received(:search_for_ids).with(
29
+ "search", :class => Person
30
+ )
31
+ end
32
+
33
+ it "should override the class option" do
34
+ Person.search_for_ids("search", :class => Friendship)
35
+
36
+ ThinkingSphinx::Search.should have_received(:search_for_ids).with(
37
+ "search", :class => Person
38
+ )
39
+ end
40
+ end
41
+
42
+ describe "search method" do
43
+ before :each do
44
+ ThinkingSphinx::Search.stub_method(:search => true)
45
+ end
46
+
47
+ it "should call ThinkingSphinx::Search#search with the class option set" do
48
+ Person.search("search")
49
+
50
+ ThinkingSphinx::Search.should have_received(:search).with(
51
+ "search", :class => Person
52
+ )
53
+ end
54
+
55
+ it "should override the class option" do
56
+ Person.search("search", :class => Friendship)
57
+
58
+ ThinkingSphinx::Search.should have_received(:search).with(
59
+ "search", :class => Person
60
+ )
61
+ end
62
+ end
63
+
64
+ describe "search_for_id method" do
65
+ before :each do
66
+ ThinkingSphinx::Search.stub_method(:search_for_id => true)
67
+ end
68
+
69
+ it "should call ThinkingSphinx::Search#search with the class option set" do
70
+ Person.search_for_id(10)
71
+
72
+ ThinkingSphinx::Search.should have_received(:search_for_id).with(
73
+ 10, :class => Person
74
+ )
75
+ end
76
+
77
+ it "should override the class option" do
78
+ Person.search_for_id(10, :class => Friendship)
79
+
80
+ ThinkingSphinx::Search.should have_received(:search_for_id).with(
81
+ 10, :class => Person
82
+ )
83
+ end
84
+ end
85
+
86
+ describe "search_count method" do
87
+ before :each do
88
+ ThinkingSphinx::Search.stub_method(:count => true)
89
+ end
90
+
91
+ it "should call ThinkingSphinx::Search#search with the class option set" do
92
+ Person.search_count("search")
93
+
94
+ ThinkingSphinx::Search.should have_received(:count).with(
95
+ "search", :class => Person
96
+ )
97
+ end
98
+
99
+ it "should override the class option" do
100
+ Person.search_count("search", :class => Friendship)
101
+
102
+ ThinkingSphinx::Search.should have_received(:count).with(
103
+ "search", :class => Person
104
+ )
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,329 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe "ThinkingSphinx::ActiveRecord" do
4
+ describe "define_index method" do
5
+ before :each do
6
+ module ::TestModule
7
+ class TestModel < ActiveRecord::Base; end
8
+ end
9
+
10
+ TestModule::TestModel.stub_methods(
11
+ :before_save => true,
12
+ :after_commit => true,
13
+ :after_destroy => true
14
+ )
15
+
16
+ @index = ThinkingSphinx::Index.stub_instance(:delta? => false)
17
+ ThinkingSphinx::Index::Builder.stub_method(:generate => @index)
18
+ end
19
+
20
+ after :each do
21
+ # Remove the class so we can redefine it
22
+ TestModule.send(:remove_const, :TestModel)
23
+
24
+ ThinkingSphinx.indexed_models.delete "TestModule::TestModel"
25
+ end
26
+
27
+ it "should do nothing if indexes are disabled" do
28
+ ThinkingSphinx.stub_method(:define_indexes? => false)
29
+
30
+ TestModule::TestModel.define_index {}
31
+ ThinkingSphinx::Index.should_not have_received(:new)
32
+
33
+ ThinkingSphinx.unstub_method(:define_indexes?)
34
+ end
35
+
36
+ it "should add a new index to the model" do
37
+ TestModule::TestModel.define_index {}
38
+
39
+ TestModule::TestModel.sphinx_indexes.length.should == 1
40
+ end
41
+
42
+ it "should add to ThinkingSphinx.indexed_models if the model doesn't already exist in the array" do
43
+ TestModule::TestModel.define_index do; end
44
+
45
+ ThinkingSphinx.indexed_models.should include("TestModule::TestModel")
46
+ end
47
+
48
+ it "shouldn't add to ThinkingSphinx.indexed_models if the model already exists in the array" do
49
+ TestModule::TestModel.define_index do; end
50
+
51
+ ThinkingSphinx.indexed_models.select { |model|
52
+ model == "TestModule::TestModel"
53
+ }.length.should == 1
54
+
55
+ TestModule::TestModel.define_index do; end
56
+
57
+ ThinkingSphinx.indexed_models.select { |model|
58
+ model == "TestModule::TestModel"
59
+ }.length.should == 1
60
+ end
61
+
62
+ it "should add before_save and after_commit hooks to the model if delta indexing is enabled" do
63
+ @index.stub_method(:delta? => true)
64
+
65
+ TestModule::TestModel.define_index do; end
66
+
67
+ TestModule::TestModel.should have_received(:before_save).with(:toggle_delta)
68
+ TestModule::TestModel.should have_received(:after_commit).with(:index_delta)
69
+ end
70
+
71
+ it "should not add before_save and after_commit hooks to the model if delta indexing is disabled" do
72
+ TestModule::TestModel.define_index do; end
73
+
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
+ end
77
+
78
+ it "should add an after_destroy hook with delta indexing enabled" do
79
+ @index.stub_method(:delta? => true)
80
+
81
+ TestModule::TestModel.define_index do; end
82
+
83
+ TestModule::TestModel.should have_received(:after_destroy).with(:toggle_deleted)
84
+ end
85
+
86
+ it "should add an after_destroy hook with delta indexing disabled" do
87
+ TestModule::TestModel.define_index do; end
88
+
89
+ TestModule::TestModel.should have_received(:after_destroy).with(:toggle_deleted)
90
+ end
91
+
92
+ it "should return the new index" do
93
+ TestModule::TestModel.define_index.should == @index
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
165
+ end
166
+
167
+ describe "to_crc32 method" do
168
+ it "should return an integer" do
169
+ Person.to_crc32.should be_a_kind_of(Integer)
170
+ end
171
+ end
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
+
179
+ describe "toggle_deleted method" do
180
+ before :each do
181
+ ThinkingSphinx.stub_method(:sphinx_running? => true)
182
+
183
+ @configuration = ThinkingSphinx::Configuration.instance
184
+ @configuration.stub_methods(
185
+ :address => "an address",
186
+ :port => 123
187
+ )
188
+ @client = Riddle::Client.new
189
+ @client.stub!(:update => true)
190
+ @person = Person.find(:first)
191
+
192
+ Riddle::Client.stub_method(:new => @client)
193
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => false) }
194
+ @person.stub_method(:in_core_index? => true)
195
+ end
196
+
197
+ it "should create a client using the Configuration's address and port" do
198
+ @person.toggle_deleted
199
+
200
+ Riddle::Client.should have_received(:new).with(
201
+ @configuration.address, @configuration.port
202
+ )
203
+ end
204
+
205
+ it "should update the core index's deleted flag if in core index" do
206
+ @client.should_receive(:update).with(
207
+ "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
208
+ )
209
+
210
+ @person.toggle_deleted
211
+ end
212
+
213
+ it "shouldn't update the core index's deleted flag if the record isn't in it" do
214
+ @person.stub_method(:in_core_index? => false)
215
+ @client.should_not_receive(:update).with(
216
+ "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
217
+ )
218
+
219
+ @person.toggle_deleted
220
+ end
221
+
222
+ it "shouldn't attempt to update the deleted flag if sphinx isn't running" do
223
+ ThinkingSphinx.stub_method(:sphinx_running? => false)
224
+ @client.should_not_receive(:update)
225
+
226
+ @person.toggle_deleted
227
+
228
+ @person.should_not have_received(:in_core_index?)
229
+ end
230
+
231
+ it "should update the delta index's deleted flag if delta indexes are enabled and the instance's delta is true" do
232
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
233
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
234
+ @person.delta = true
235
+ @client.should_receive(:update).with(
236
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
237
+ )
238
+
239
+ @person.toggle_deleted
240
+ end
241
+
242
+ it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is false" do
243
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
244
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
245
+ @person.delta = false
246
+ @client.should_not_receive(:update).with(
247
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
248
+ )
249
+
250
+ @person.toggle_deleted
251
+ end
252
+
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
254
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
255
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
256
+ @person.delta = 0
257
+ @client.should_not_receive(:update).with(
258
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
259
+ )
260
+
261
+ @person.toggle_deleted
262
+ end
263
+
264
+ it "shouldn't update the delta index if delta indexes are disabled" do
265
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
266
+ @client.should_not_receive(:update).with(
267
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
268
+ )
269
+
270
+ @person.toggle_deleted
271
+ end
272
+
273
+ it "should not update the delta index if delta indexing is disabled" do
274
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
275
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
276
+ @person.delta = true
277
+ @client.should_not_receive(:update).with(
278
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
279
+ )
280
+
281
+ @person.toggle_deleted
282
+ end
283
+
284
+ it "should not update either index if updates are disabled" do
285
+ ThinkingSphinx.stub_methods(
286
+ :updates_enabled? => false,
287
+ :deltas_enabled => true
288
+ )
289
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
290
+ @person.delta = true
291
+ @client.should_not_receive(:update)
292
+
293
+ @person.toggle_deleted
294
+ end
295
+ end
296
+
297
+ describe "sphinx_indexes in the inheritance chain (STI)" do
298
+ it "should hand defined indexes on a class down to its child classes" do
299
+ Child.sphinx_indexes.should include(*Person.sphinx_indexes)
300
+ end
301
+
302
+ it "should allow associations to other STI models" do
303
+ source = Child.sphinx_indexes.last.sources.first
304
+ sql = source.to_riddle_for_core(0, 0).sql_query
305
+ sql.gsub!('$start', '0').gsub!('$end', '100')
306
+ lambda {
307
+ Child.connection.execute(sql)
308
+ }.should_not raise_error(ActiveRecord::StatementInvalid)
309
+ end
310
+ end
311
+
312
+ it "should return the sphinx document id as expected" do
313
+ person = Person.find(:first)
314
+ model_count = ThinkingSphinx.indexed_models.length
315
+ offset = ThinkingSphinx.indexed_models.index("Person")
316
+
317
+ (person.id * model_count + offset).should == person.sphinx_document_id
318
+
319
+ alpha = Alpha.find(:first)
320
+ offset = ThinkingSphinx.indexed_models.index("Alpha")
321
+
322
+ (alpha.id * model_count + offset).should == alpha.sphinx_document_id
323
+
324
+ beta = Beta.find(:first)
325
+ offset = ThinkingSphinx.indexed_models.index("Beta")
326
+
327
+ (beta.id * model_count + offset).should == beta.sphinx_document_id
328
+ end
329
+ end
@@ -0,0 +1,246 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Association do
4
+ describe "class-level children method" do
5
+ before :each do
6
+ @normal_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
7
+ :options => {:polymorphic => false}
8
+ )
9
+ @normal_association = ThinkingSphinx::Association.stub_instance
10
+ @poly_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
11
+ :options => {:polymorphic => true},
12
+ :macro => :has_many,
13
+ :name => "polly",
14
+ :active_record => "AR"
15
+ )
16
+ @non_poly_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance
17
+
18
+ Person.stub_method(:reflect_on_association => @normal_reflection)
19
+ ThinkingSphinx::Association.stub_methods(
20
+ :new => @normal_association,
21
+ :polymorphic_classes => [Person, Person],
22
+ :casted_options => {:casted => :options}
23
+ )
24
+ ::ActiveRecord::Reflection::AssociationReflection.stub_method(
25
+ :new => @non_poly_reflection
26
+ )
27
+ end
28
+
29
+ it "should return an empty array if no association exists" do
30
+ Person.stub_method(:reflect_on_association => nil)
31
+
32
+ ThinkingSphinx::Association.children(Person, :assoc).should == []
33
+ end
34
+
35
+ it "should return a single association instance in an array if assocation isn't polymorphic" do
36
+ ThinkingSphinx::Association.children(Person, :assoc).should == [@normal_association]
37
+ end
38
+
39
+ it "should return multiple association instances for polymorphic associations" do
40
+ Person.stub_method(:reflect_on_association => @poly_reflection)
41
+
42
+ ThinkingSphinx::Association.children(Person, :assoc).should ==
43
+ [@normal_association, @normal_association]
44
+ end
45
+
46
+ it "should generate non-polymorphic 'casted' associations for each polymorphic possibility" do
47
+ Person.stub_method(:reflect_on_association => @poly_reflection)
48
+
49
+ ThinkingSphinx::Association.children(Person, :assoc)
50
+
51
+ ThinkingSphinx::Association.should have_received(:casted_options).with(
52
+ Person, @poly_reflection
53
+ ).twice
54
+
55
+ ::ActiveRecord::Reflection::AssociationReflection.should have_received(:new).with(
56
+ :has_many, :polly_Person, {:casted => :options}, "AR"
57
+ ).twice
58
+
59
+ ThinkingSphinx::Association.should have_received(:new).with(
60
+ nil, @non_poly_reflection
61
+ ).twice
62
+ end
63
+ end
64
+
65
+ describe "instance-level children method" do
66
+ it "should return the children associations for the given association" do
67
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
68
+ :klass => :klass
69
+ )
70
+ @association = ThinkingSphinx::Association.new(nil, @reflection)
71
+ ThinkingSphinx::Association.stub_method(:children => :result)
72
+
73
+ @association.children(:assoc).should == :result
74
+ ThinkingSphinx::Association.should have_received(:children).with(:klass, :assoc, @association)
75
+ end
76
+ end
77
+
78
+ describe "join_to method" do
79
+ before :each do
80
+ @parent_join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
81
+ @join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
82
+ @parent = ThinkingSphinx::Association.new(nil, nil)
83
+ @parent.stub!(:join_to => true, :join => nil)
84
+ @base_join = Object.stub_instance(:joins => [:a, :b, :c])
85
+
86
+ ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_method(:new => @join)
87
+ end
88
+
89
+ it "should call the parent's join_to if parent has no join" do
90
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
91
+ @parent.should_receive(:join_to).with(@base_join)
92
+
93
+ @assoc.join_to(@base_join)
94
+ end
95
+
96
+ it "should not call the parent's join_to if it already has a join" do
97
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
98
+ @parent.stub_method(:join => @parent_join)
99
+ @parent.should_not_receive(:join_to)
100
+
101
+ @assoc.join_to(@base_join)
102
+ end
103
+
104
+ it "should define the join association with a JoinAssociation instance" do
105
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
106
+
107
+ @assoc.join_to(@base_join).should == @join
108
+ @assoc.join.should == @join
109
+ end
110
+ end
111
+
112
+ describe "to_sql method" do
113
+ before :each do
114
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
115
+ :klass => Person
116
+ )
117
+ @association = ThinkingSphinx::Association.new(nil, @reflection)
118
+ @parent = Object.stub_instance(:aliased_table_name => "ALIAS TABLE NAME")
119
+ @join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance(
120
+ :association_join => "full association join SQL",
121
+ :parent => @parent
122
+ )
123
+ @association.join = @join
124
+ end
125
+
126
+ it "should return the join's association join value" do
127
+ @association.to_sql.should == "full association join SQL"
128
+ end
129
+
130
+ it "should replace ::ts_join_alias:: with the aliased table name" do
131
+ @join.stub_method(:association_join => "text with ::ts_join_alias:: gone")
132
+
133
+ @association.to_sql.should == "text with `ALIAS TABLE NAME` gone"
134
+ end
135
+ end
136
+
137
+ describe "is_many? method" do
138
+ before :each do
139
+ @parent = ThinkingSphinx::Association.stub_instance(
140
+ :is_many? => :parent_is_many
141
+ )
142
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
143
+ :macro => :has_many
144
+ )
145
+ end
146
+
147
+ it "should return true if association is either a has_many or a habtm" do
148
+ association = ThinkingSphinx::Association.new(@parent, @reflection)
149
+ association.is_many?.should be_true
150
+
151
+ @reflection.stub_method(:macro => :has_and_belongs_to_many)
152
+ association.is_many?.should be_true
153
+ end
154
+
155
+ it "should return the parent value if not a has many or habtm and there is a parent" do
156
+ association = ThinkingSphinx::Association.new(@parent, @reflection)
157
+ @reflection.stub_method(:macro => :belongs_to)
158
+ association.is_many?.should == :parent_is_many
159
+ end
160
+
161
+ it "should return false if no parent and not a has many or habtm" do
162
+ association = ThinkingSphinx::Association.new(nil, @reflection)
163
+ @reflection.stub_method(:macro => :belongs_to)
164
+ association.is_many?.should be_false
165
+ end
166
+ end
167
+
168
+ describe "ancestors method" do
169
+ it "should return an array of associations - including all parents" do
170
+ parent = ThinkingSphinx::Association.stub_instance(:ancestors => [:all, :ancestors])
171
+ association = ThinkingSphinx::Association.new(parent, @reflection)
172
+ association.ancestors.should == [:all, :ancestors, association]
173
+ end
174
+ end
175
+
176
+ describe "polymorphic_classes method" do
177
+ it "should return all the polymorphic result types as classes" do
178
+ Person.connection.stub_method(:select_all => [
179
+ {"person_type" => "Person"},
180
+ {"person_type" => "Friendship"}
181
+ ])
182
+ ref = Object.stub_instance(
183
+ :active_record => Person,
184
+ :options => {:foreign_type => "person_type"}
185
+ )
186
+
187
+ ThinkingSphinx::Association.send(:polymorphic_classes, ref).should == [Person, Friendship]
188
+ end
189
+ end
190
+
191
+ describe "casted_options method" do
192
+ before :each do
193
+ @options = {
194
+ :foreign_key => "thing_id",
195
+ :foreign_type => "thing_type",
196
+ :polymorphic => true
197
+ }
198
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
199
+ :options => @options
200
+ )
201
+ end
202
+
203
+ it "should return a new options set for a specific class" do
204
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
205
+ :polymorphic => nil,
206
+ :class_name => "Person",
207
+ :foreign_key => "thing_id",
208
+ :foreign_type => "thing_type",
209
+ :conditions => "::ts_join_alias::.`thing_type` = 'Person'"
210
+ }
211
+ end
212
+
213
+ it "should append to existing Array of conditions" do
214
+ @options[:conditions] = ["first condition"]
215
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
216
+ :polymorphic => nil,
217
+ :class_name => "Person",
218
+ :foreign_key => "thing_id",
219
+ :foreign_type => "thing_type",
220
+ :conditions => ["first condition", "::ts_join_alias::.`thing_type` = 'Person'"]
221
+ }
222
+ end
223
+
224
+ it "should merge to an existing Hash of conditions" do
225
+ @options[:conditions] = {"field" => "value"}
226
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
227
+ :polymorphic => nil,
228
+ :class_name => "Person",
229
+ :foreign_key => "thing_id",
230
+ :foreign_type => "thing_type",
231
+ :conditions => {"field" => "value", "thing_type" => "Person"}
232
+ }
233
+ end
234
+
235
+ it "should append to an existing String of conditions" do
236
+ @options[:conditions] = "first condition"
237
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
238
+ :polymorphic => nil,
239
+ :class_name => "Person",
240
+ :foreign_key => "thing_id",
241
+ :foreign_type => "thing_type",
242
+ :conditions => "first condition AND ::ts_join_alias::.`thing_type` = 'Person'"
243
+ }
244
+ end
245
+ end
246
+ end