DrMark-thinking-sphinx 0.9.9 → 1.1.6

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 (77) hide show
  1. data/README +64 -2
  2. data/lib/thinking_sphinx.rb +88 -11
  3. data/lib/thinking_sphinx/active_record.rb +136 -21
  4. data/lib/thinking_sphinx/active_record/delta.rb +43 -62
  5. data/lib/thinking_sphinx/active_record/has_many_association.rb +1 -1
  6. data/lib/thinking_sphinx/active_record/search.rb +7 -0
  7. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +42 -0
  8. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +54 -0
  9. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +130 -0
  10. data/lib/thinking_sphinx/association.rb +17 -0
  11. data/lib/thinking_sphinx/attribute.rb +171 -97
  12. data/lib/thinking_sphinx/collection.rb +126 -2
  13. data/lib/thinking_sphinx/configuration.rb +120 -171
  14. data/lib/thinking_sphinx/core/string.rb +15 -0
  15. data/lib/thinking_sphinx/deltas.rb +27 -0
  16. data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
  17. data/lib/thinking_sphinx/deltas/default_delta.rb +67 -0
  18. data/lib/thinking_sphinx/deltas/delayed_delta.rb +25 -0
  19. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
  20. data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
  21. data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
  22. data/lib/thinking_sphinx/facet.rb +58 -0
  23. data/lib/thinking_sphinx/facet_collection.rb +60 -0
  24. data/lib/thinking_sphinx/field.rb +18 -52
  25. data/lib/thinking_sphinx/index.rb +246 -199
  26. data/lib/thinking_sphinx/index/builder.rb +85 -16
  27. data/lib/thinking_sphinx/rails_additions.rb +85 -5
  28. data/lib/thinking_sphinx/search.rb +459 -190
  29. data/lib/thinking_sphinx/tasks.rb +128 -0
  30. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +53 -124
  31. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +2 -2
  32. data/spec/unit/thinking_sphinx/active_record_spec.rb +110 -30
  33. data/spec/unit/thinking_sphinx/attribute_spec.rb +16 -149
  34. data/spec/unit/thinking_sphinx/collection_spec.rb +14 -0
  35. data/spec/unit/thinking_sphinx/configuration_spec.rb +54 -412
  36. data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
  37. data/spec/unit/thinking_sphinx/field_spec.rb +0 -79
  38. data/spec/unit/thinking_sphinx/index/builder_spec.rb +1 -29
  39. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +1 -39
  40. data/spec/unit/thinking_sphinx/index_spec.rb +78 -226
  41. data/spec/unit/thinking_sphinx/search_spec.rb +29 -228
  42. data/spec/unit/thinking_sphinx_spec.rb +23 -19
  43. data/tasks/distribution.rb +48 -0
  44. data/tasks/rails.rake +1 -0
  45. data/tasks/testing.rb +86 -0
  46. data/vendor/after_commit/LICENSE +20 -0
  47. data/vendor/after_commit/README +16 -0
  48. data/vendor/after_commit/Rakefile +22 -0
  49. data/vendor/after_commit/init.rb +8 -0
  50. data/vendor/after_commit/lib/after_commit.rb +45 -0
  51. data/vendor/after_commit/lib/after_commit/active_record.rb +114 -0
  52. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
  53. data/vendor/after_commit/test/after_commit_test.rb +53 -0
  54. data/vendor/delayed_job/lib/delayed/job.rb +251 -0
  55. data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
  56. data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
  57. data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
  58. data/{lib → vendor/riddle/lib}/riddle.rb +9 -5
  59. data/{lib → vendor/riddle/lib}/riddle/client.rb +6 -26
  60. data/{lib → vendor/riddle/lib}/riddle/client/filter.rb +10 -1
  61. data/{lib → vendor/riddle/lib}/riddle/client/message.rb +0 -0
  62. data/{lib → vendor/riddle/lib}/riddle/client/response.rb +0 -0
  63. data/vendor/riddle/lib/riddle/configuration.rb +33 -0
  64. data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
  65. data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
  66. data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
  67. data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
  68. data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
  69. data/vendor/riddle/lib/riddle/configuration/section.rb +37 -0
  70. data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
  71. data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
  72. data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
  73. data/vendor/riddle/lib/riddle/controller.rb +44 -0
  74. metadata +63 -10
  75. data/lib/test.rb +0 -46
  76. data/tasks/thinking_sphinx_tasks.rake +0 -1
  77. data/tasks/thinking_sphinx_tasks.rb +0 -86
@@ -0,0 +1,128 @@
1
+ require 'fileutils'
2
+
3
+ namespace :thinking_sphinx do
4
+ task :app_env do
5
+ Rake::Task[:environment].invoke if defined?(RAILS_ROOT)
6
+ Rake::Task[:merb_env].invoke if defined?(Merb)
7
+ end
8
+
9
+ desc "Stop if running, then start a Sphinx searchd daemon using Thinking Sphinx's settings"
10
+ task :running_start => :app_env do
11
+ Rake::Task["thinking_sphinx:stop"].invoke if sphinx_running?
12
+ Rake::Task["thinking_sphinx:start"].invoke
13
+ end
14
+
15
+ desc "Start a Sphinx searchd daemon using Thinking Sphinx's settings"
16
+ task :start => :app_env do
17
+ config = ThinkingSphinx::Configuration.instance
18
+
19
+ FileUtils.mkdir_p config.searchd_file_path
20
+ raise RuntimeError, "searchd is already running." if sphinx_running?
21
+
22
+ Dir["#{config.searchd_file_path}/*.spl"].each { |file| File.delete(file) }
23
+
24
+ cmd = "#{config.bin_path}searchd --pidfile --config #{config.config_file}"
25
+ puts cmd
26
+ system cmd
27
+
28
+ sleep(2)
29
+
30
+ if sphinx_running?
31
+ puts "Started successfully (pid #{sphinx_pid})."
32
+ else
33
+ puts "Failed to start searchd daemon. Check #{config.searchd_log_file}."
34
+ end
35
+ end
36
+
37
+ desc "Stop Sphinx using Thinking Sphinx's settings"
38
+ task :stop => :app_env do
39
+ raise RuntimeError, "searchd is not running." unless sphinx_running?
40
+ config = ThinkingSphinx::Configuration.instance
41
+ pid = sphinx_pid
42
+ system "#{config.bin_path}searchd --stop --config #{config.config_file}"
43
+ puts "Stopped search daemon (pid #{pid})."
44
+ end
45
+
46
+ desc "Restart Sphinx"
47
+ task :restart => [:app_env, :stop, :start]
48
+
49
+ desc "Generate the Sphinx configuration file using Thinking Sphinx's settings"
50
+ task :configure => :app_env do
51
+ config = ThinkingSphinx::Configuration.instance
52
+ puts "Generating Configuration to #{config.config_file}"
53
+ config.build
54
+ end
55
+
56
+ desc "Index data for Sphinx using Thinking Sphinx's settings"
57
+ task :index => :app_env do
58
+ ThinkingSphinx::Deltas::Job.cancel_thinking_sphinx_jobs
59
+
60
+ config = ThinkingSphinx::Configuration.instance
61
+ unless ENV["INDEX_ONLY"] == "true"
62
+ puts "Generating Configuration to #{config.config_file}"
63
+ config.build
64
+ end
65
+
66
+ FileUtils.mkdir_p config.searchd_file_path
67
+ cmd = "#{config.bin_path}indexer --config #{config.config_file} --all"
68
+ cmd << " --rotate" if sphinx_running?
69
+ puts cmd
70
+ system cmd
71
+ end
72
+
73
+ namespace :index do
74
+ task :delta => :app_env do
75
+ ThinkingSphinx.indexed_models.select { |model|
76
+ model.constantize.sphinx_indexes.any? { |index| index.delta? }
77
+ }.each do |model|
78
+ model.constantize.sphinx_indexes.select { |index|
79
+ index.delta? && index.delta_object.respond_to?(:delayed_index)
80
+ }.each { |index|
81
+ index.delta_object.delayed_index(index.model)
82
+ }
83
+ end
84
+ end
85
+ end
86
+
87
+ desc "Process stored delta index requests"
88
+ task :delayed_delta => :app_env do
89
+ require 'delayed/worker'
90
+
91
+ Delayed::Worker.new(
92
+ :min_priority => ENV['MIN_PRIORITY'],
93
+ :max_priority => ENV['MAX_PRIORITY']
94
+ ).start
95
+ end
96
+ end
97
+
98
+ namespace :ts do
99
+ desc "Stop if running, then start a Sphinx searchd daemon using Thinking Sphinx's settings"
100
+ task :run => "thinking_sphinx:running_start"
101
+ desc "Start a Sphinx searchd daemon using Thinking Sphinx's settings"
102
+ task :start => "thinking_sphinx:start"
103
+ desc "Stop Sphinx using Thinking Sphinx's settings"
104
+ task :stop => "thinking_sphinx:stop"
105
+ desc "Index data for Sphinx using Thinking Sphinx's settings"
106
+ task :in => "thinking_sphinx:index"
107
+ namespace :in do
108
+ desc "Index Thinking Sphinx datetime delta indexes"
109
+ task :delta => "thinking_sphinx:index:delta"
110
+ end
111
+ task :index => "thinking_sphinx:index"
112
+ desc "Restart Sphinx"
113
+ task :restart => "thinking_sphinx:restart"
114
+ desc "Generate the Sphinx configuration file using Thinking Sphinx's settings"
115
+ task :conf => "thinking_sphinx:configure"
116
+ desc "Generate the Sphinx configuration file using Thinking Sphinx's settings"
117
+ task :config => "thinking_sphinx:configure"
118
+ desc "Process stored delta index requests"
119
+ task :dd => "thinking_sphinx:delayed_delta"
120
+ end
121
+
122
+ def sphinx_pid
123
+ ThinkingSphinx.sphinx_pid
124
+ end
125
+
126
+ def sphinx_running?
127
+ ThinkingSphinx.sphinx_running?
128
+ end
@@ -1,136 +1,60 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
3
  describe "ThinkingSphinx::ActiveRecord::Delta" do
4
- describe "after_commit callback" do
5
- before :each do
6
- Person.stub_method(:write_inheritable_array => true)
7
- end
8
-
9
- # This spec only passes with ActiveRecord 2.0.2 or earlier.
10
- # it "should add callbacks" do
11
- # Person.after_commit :toggle_delta
12
- #
13
- # Person.should have_received(:write_inheritable_array).with(
14
- # :after_commit, [:toggle_delta]
15
- # )
16
- # end
17
-
18
- it "should have an after_commit method by default" do
19
- Person.instance_methods.should include("after_commit")
20
- end
21
- end
22
-
23
- describe "save_with_after_commit_callback method" do
24
- before :each do
25
- @person = Person.new
26
- @person.stub_methods(
27
- :save_without_after_commit_callback => true,
28
- :callback => true
29
- )
30
- end
31
-
32
- it "should call the normal save method" do
33
- @person.save
34
-
35
- @person.should have_received(:save_without_after_commit_callback)
36
- end
4
+ it "should call the toggle_delta method after a save" do
5
+ @beta = Beta.new(:name => 'beta')
6
+ @beta.stub_method(:toggle_delta => true)
37
7
 
38
- it "should call the callbacks if the save was successful" do
39
- @person.save
40
-
41
- @person.should have_received(:callback).with(:after_commit)
42
- end
8
+ @beta.save
43
9
 
44
- it "shouldn't call the callbacks if the save failed" do
45
- @person.stub_method(:save_without_after_commit_callback => false)
46
-
47
- @person.save
48
-
49
- @person.should_not have_received(:callback)
50
- end
51
-
52
- it "should return the normal save's result" do
53
- @person.save.should be_true
54
-
55
- @person.stub_method(:save_without_after_commit_callback => false)
56
-
57
- @person.save.should be_false
58
- end
10
+ @beta.should have_received(:toggle_delta)
59
11
  end
60
12
 
61
- describe "save_with_after_commit_callback! method" do
62
- before :each do
63
- @person = Person.new
64
- @person.stub_methods(
65
- :save_without_after_commit_callback! => true,
66
- :callback => true
67
- )
68
- end
69
-
70
- it "should call the normal save! method" do
71
- @person.save!
72
-
73
- @person.should have_received(:save_without_after_commit_callback!)
74
- end
75
-
76
- it "should call the callbacks if the save! was successful" do
77
- @person.save!
78
-
79
- @person.should have_received(:callback).with(:after_commit)
80
- end
13
+ it "should call the toggle_delta method after a save!" do
14
+ @beta = Beta.new(:name => 'beta')
15
+ @beta.stub_method(:toggle_delta => true)
81
16
 
82
- it "shouldn't call the callbacks if the save! failed" do
83
- @person.stub_method(:save_without_after_commit_callback! => false)
84
-
85
- @person.save!
86
-
87
- @person.should_not have_received(:callback)
88
- end
17
+ @beta.save!
89
18
 
90
- it "should return the normal save's result" do
91
- @person.save!.should be_true
92
-
93
- @person.stub_method(:save_without_after_commit_callback! => false)
94
-
95
- @person.save!.should be_false
96
- end
19
+ @beta.should have_received(:toggle_delta)
97
20
  end
98
-
99
- describe "destroy_with_after_commit_callback method" do
21
+
22
+ describe "suspended_delta method" do
100
23
  before :each do
101
- @person = Person.new
102
- @person.stub_methods(
103
- :destroy_without_after_commit_callback => true,
104
- :callback => true
24
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
25
+ Person.sphinx_indexes.first.delta_object.stub_method(:` => "")
26
+ end
27
+
28
+ it "should execute the argument block with deltas disabled" do
29
+ ThinkingSphinx.should_receive(:deltas_enabled=).once.with(false)
30
+ ThinkingSphinx.should_receive(:deltas_enabled=).once.with(true)
31
+ lambda { Person.suspended_delta { raise 'i was called' } }.should(
32
+ raise_error(Exception)
105
33
  )
106
34
  end
107
-
108
- it "should call the normal destroy method" do
109
- @person.destroy
110
-
111
- @person.should have_received(:destroy_without_after_commit_callback)
35
+
36
+ it "should restore deltas_enabled to its original setting" do
37
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
38
+ ThinkingSphinx.should_receive(:deltas_enabled=).twice.with(false)
39
+ Person.suspended_delta { 'no-op' }
112
40
  end
113
-
114
- it "should call the callbacks if the destroy was successful" do
115
- @person.destroy
116
-
117
- @person.should have_received(:callback).with(:after_commit)
41
+
42
+ it "should restore deltas_enabled to its original setting even if there was an exception" do
43
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
44
+ ThinkingSphinx.should_receive(:deltas_enabled=).twice.with(false)
45
+ lambda { Person.suspended_delta { raise 'bad error' } }.should(
46
+ raise_error(Exception)
47
+ )
118
48
  end
119
-
120
- it "shouldn't call the callbacks if the destroy failed" do
121
- @person.stub_method(:destroy_without_after_commit_callback => false)
122
-
123
- @person.destroy
124
-
125
- @person.should_not have_received(:callback)
49
+
50
+ it "should reindex by default after the code block is run" do
51
+ Person.should_receive(:index_delta)
52
+ Person.suspended_delta { 'no-op' }
126
53
  end
127
54
 
128
- it "should return the normal save's result" do
129
- @person.destroy.should be_true
130
-
131
- @person.stub_method(:destroy_without_after_commit_callback => false)
132
-
133
- @person.destroy.should be_false
55
+ it "should not reindex after the code block if false is passed in" do
56
+ Person.should_not_receive(:index_delta)
57
+ Person.suspended_delta(false) { 'no-op' }
134
58
  end
135
59
  end
136
60
 
@@ -147,10 +71,14 @@ describe "ThinkingSphinx::ActiveRecord::Delta" do
147
71
  describe "index_delta method" do
148
72
  before :each do
149
73
  ThinkingSphinx::Configuration.stub_method(:environment => "spec")
150
- ThinkingSphinx.stub_method(:deltas_enabled? => true)
74
+ ThinkingSphinx.stub_method(:deltas_enabled? => true, :sphinx_running? => true)
75
+ Person.delta_object.stub_methods(:` => "", :toggled => true)
151
76
 
152
77
  @person = Person.new
153
- @person.stub_method(:system => true, :in_core_index? => false)
78
+ @person.stub_method(
79
+ :in_both_indexes? => false,
80
+ :sphinx_document_id => 1
81
+ )
154
82
 
155
83
  @client = Riddle::Client.stub_instance(:update => true)
156
84
  Riddle::Client.stub_method(:new => @client)
@@ -161,7 +89,7 @@ describe "ThinkingSphinx::ActiveRecord::Delta" do
161
89
 
162
90
  @person.send(:index_delta)
163
91
 
164
- @person.should_not have_received(:system)
92
+ Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
165
93
  @client.should_not have_received(:update)
166
94
  end
167
95
 
@@ -170,23 +98,24 @@ describe "ThinkingSphinx::ActiveRecord::Delta" do
170
98
 
171
99
  @person.send(:index_delta)
172
100
 
173
- @person.should_not have_received(:system)
101
+ Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
174
102
  end
175
103
 
176
104
  it "shouldn't index if the environment is 'test'" do
177
105
  ThinkingSphinx.unstub_method(:deltas_enabled?)
106
+ ThinkingSphinx.deltas_enabled = nil
178
107
  ThinkingSphinx::Configuration.stub_method(:environment => "test")
179
108
 
180
109
  @person.send(:index_delta)
181
110
 
182
- @person.should_not have_received(:system)
111
+ Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
183
112
  end
184
113
 
185
114
  it "should call indexer for the delta index" do
186
115
  @person.send(:index_delta)
187
116
 
188
- @person.should have_received(:system).with(
189
- "indexer --config #{ThinkingSphinx::Configuration.new.config_file} --rotate person_delta"
117
+ Person.sphinx_indexes.first.delta_object.should have_received(:`).with(
118
+ "#{ThinkingSphinx::Configuration.instance.bin_path}indexer --config #{ThinkingSphinx::Configuration.instance.config_file} --rotate person_delta"
190
119
  )
191
120
  end
192
121
 
@@ -197,7 +126,7 @@ describe "ThinkingSphinx::ActiveRecord::Delta" do
197
126
  end
198
127
 
199
128
  it "should update the deleted attribute if in the core index" do
200
- @person.stub_method(:in_core_index? => true)
129
+ @person.stub_method(:in_both_indexes? => true)
201
130
 
202
131
  @person.send(:index_delta)
203
132
 
@@ -6,7 +6,7 @@ describe 'ThinkingSphinx::ActiveRecord::HasManyAssociation' do
6
6
  Friendship.stub_method(:search => true)
7
7
 
8
8
  @person = Person.find(:first)
9
- @index = Friendship.indexes.first
9
+ @index = Friendship.sphinx_indexes.first
10
10
  end
11
11
 
12
12
  it "should raise an error if the required attribute doesn't exist" do
@@ -31,7 +31,7 @@ describe 'ThinkingSphinx::ActiveRecord::HasManyAssociation' do
31
31
  Person.stub_method(:search => true)
32
32
 
33
33
  @person = Person.find(:first)
34
- @index = Person.indexes.first
34
+ @index = Person.sphinx_indexes.first
35
35
  end
36
36
 
37
37
  it "should raise an error if the required attribute doesn't exist" do
@@ -36,7 +36,7 @@ describe "ThinkingSphinx::ActiveRecord" do
36
36
  it "should add a new index to the model" do
37
37
  TestModule::TestModel.define_index do; end
38
38
 
39
- TestModule::TestModel.indexes.length.should == 1
39
+ TestModule::TestModel.sphinx_indexes.length.should == 1
40
40
  end
41
41
 
42
42
  it "should add to ThinkingSphinx.indexed_models if the model doesn't already exist in the array" do
@@ -93,35 +93,87 @@ describe "ThinkingSphinx::ActiveRecord" do
93
93
  TestModule::TestModel.define_index.should == @index
94
94
  end
95
95
  end
96
+
97
+ describe "index methods" do
98
+ before(:all) do
99
+ @person = Person.find(:first)
100
+ end
101
+
102
+ describe "in_both_indexes?" do
103
+ it "should return true if in core and delta indexes" do
104
+ @person.should_receive(:in_core_index?).and_return(true)
105
+ @person.should_receive(:in_delta_index?).and_return(true)
106
+ @person.in_both_indexes?.should be_true
107
+ end
108
+
109
+ it "should return false if in one index and not the other" do
110
+ @person.should_receive(:in_core_index?).and_return(true)
111
+ @person.should_receive(:in_delta_index?).and_return(false)
112
+ @person.in_both_indexes?.should be_false
113
+ end
114
+ end
115
+
116
+ describe "in_core_index?" do
117
+ it "should call in_index? with core" do
118
+ @person.should_receive(:in_index?).with('core')
119
+ @person.in_core_index?
120
+ end
121
+ end
122
+
123
+ describe "in_delta_index?" do
124
+ it "should call in_index? with delta" do
125
+ @person.should_receive(:in_index?).with('delta')
126
+ @person.in_delta_index?
127
+ end
128
+ end
129
+
130
+ describe "in_index?" do
131
+ it "should return true if in the specified index" do
132
+ @person.should_receive(:sphinx_document_id).and_return(1)
133
+ @person.should_receive(:sphinx_index_name).and_return('person_core')
134
+ Person.should_receive(:search_for_id).with(1, 'person_core').and_return(true)
135
+
136
+ @person.in_index?('core').should be_true
137
+ end
138
+ end
139
+ end
140
+
141
+ describe "source_of_sphinx_index method" do
142
+ it "should return self if model defines an index" do
143
+ Person.source_of_sphinx_index.should == Person
144
+ end
145
+
146
+ it "should return the parent if model inherits an index" do
147
+ Parent.source_of_sphinx_index.should == Person
148
+ end
149
+ end
96
150
 
97
151
  describe "to_crc32 method" do
98
152
  it "should return an integer" do
99
153
  Person.to_crc32.should be_a_kind_of(Integer)
100
154
  end
101
155
  end
102
-
103
- describe "in_core_index? method" do
104
- it "should return the model's corresponding search_for_id value" do
105
- Person.stub_method(:search_for_id => :searching_for_id)
106
-
107
- person = Person.find(:first)
108
- person.in_core_index?.should == :searching_for_id
109
- Person.should have_received(:search_for_id).with(person.id, "person_core")
156
+
157
+ describe "to_crc32s method" do
158
+ it "should return an array" do
159
+ Person.to_crc32s.should be_a_kind_of(Array)
110
160
  end
111
161
  end
112
-
162
+
113
163
  describe "toggle_deleted method" do
114
164
  before :each do
115
- @configuration = ThinkingSphinx::Configuration.stub_instance(
165
+ ThinkingSphinx.stub_method(:sphinx_running? => true)
166
+
167
+ @configuration = ThinkingSphinx::Configuration.instance
168
+ @configuration.stub_methods(
116
169
  :address => "an address",
117
170
  :port => 123
118
171
  )
119
172
  @client = Riddle::Client.stub_instance(:update => true)
120
- @person = Person.new
173
+ @person = Person.find(:first)
121
174
 
122
- ThinkingSphinx::Configuration.stub_method(:new => @configuration)
123
175
  Riddle::Client.stub_method(:new => @client)
124
- Person.indexes.each { |index| index.stub_method(:delta? => false) }
176
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => false) }
125
177
  @person.stub_method(:in_core_index? => true)
126
178
  end
127
179
 
@@ -137,7 +189,7 @@ describe "ThinkingSphinx::ActiveRecord" do
137
189
  @person.toggle_deleted
138
190
 
139
191
  @client.should have_received(:update).with(
140
- "person_core", ["sphinx_deleted"], {@person.id => 1}
192
+ "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
141
193
  )
142
194
  end
143
195
 
@@ -147,43 +199,52 @@ describe "ThinkingSphinx::ActiveRecord" do
147
199
  @person.toggle_deleted
148
200
 
149
201
  @client.should_not have_received(:update).with(
150
- "person_core", ["sphinx_deleted"], {@person.id => 1}
202
+ "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
151
203
  )
152
204
  end
153
205
 
206
+ it "shouldn't attempt to update the deleted flag if sphinx isn't running" do
207
+ ThinkingSphinx.stub_method(:sphinx_running? => false)
208
+
209
+ @person.toggle_deleted
210
+
211
+ @person.should_not have_received(:in_core_index?)
212
+ @client.should_not have_received(:update)
213
+ end
214
+
154
215
  it "should update the delta index's deleted flag if delta indexes are enabled and the instance's delta is true" do
155
216
  ThinkingSphinx.stub_method(:deltas_enabled? => true)
156
- Person.indexes.each { |index| index.stub_method(:delta? => true) }
217
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
157
218
  @person.delta = true
158
219
 
159
220
  @person.toggle_deleted
160
221
 
161
222
  @client.should have_received(:update).with(
162
- "person_delta", ["sphinx_deleted"], {@person.id => 1}
223
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
163
224
  )
164
225
  end
165
226
 
166
227
  it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is false" do
167
228
  ThinkingSphinx.stub_method(:deltas_enabled? => true)
168
- Person.indexes.each { |index| index.stub_method(:delta? => true) }
229
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
169
230
  @person.delta = false
170
231
 
171
232
  @person.toggle_deleted
172
233
 
173
234
  @client.should_not have_received(:update).with(
174
- "person_delta", ["sphinx_deleted"], {@person.id => 1}
235
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
175
236
  )
176
237
  end
177
238
 
178
239
  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
179
240
  ThinkingSphinx.stub_method(:deltas_enabled? => true)
180
- Person.indexes.each { |index| index.stub_method(:delta? => true) }
241
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
181
242
  @person.delta = 0
182
243
 
183
244
  @person.toggle_deleted
184
245
 
185
246
  @client.should_not have_received(:update).with(
186
- "person_delta", ["sphinx_deleted"], {@person.id => 1}
247
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
187
248
  )
188
249
  end
189
250
 
@@ -192,19 +253,19 @@ describe "ThinkingSphinx::ActiveRecord" do
192
253
  @person.toggle_deleted
193
254
 
194
255
  @client.should_not have_received(:update).with(
195
- "person_delta", ["sphinx_deleted"], {@person.id => 1}
256
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
196
257
  )
197
258
  end
198
259
 
199
260
  it "should not update the delta index if delta indexing is disabled" do
200
261
  ThinkingSphinx.stub_method(:deltas_enabled? => false)
201
- Person.indexes.each { |index| index.stub_method(:delta? => true) }
262
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
202
263
  @person.delta = true
203
264
 
204
265
  @person.toggle_deleted
205
266
 
206
267
  @client.should_not have_received(:update).with(
207
- "person_delta", ["sphinx_deleted"], {@person.id => 1}
268
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
208
269
  )
209
270
  end
210
271
 
@@ -213,7 +274,7 @@ describe "ThinkingSphinx::ActiveRecord" do
213
274
  :updates_enabled? => false,
214
275
  :deltas_enabled => true
215
276
  )
216
- Person.indexes.each { |index| index.stub_method(:delta? => true) }
277
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
217
278
  @person.delta = true
218
279
 
219
280
  @person.toggle_deleted
@@ -222,15 +283,34 @@ describe "ThinkingSphinx::ActiveRecord" do
222
283
  end
223
284
  end
224
285
 
225
- describe "indexes in the inheritance chain (STI)" do
286
+ describe "sphinx_indexes in the inheritance chain (STI)" do
226
287
  it "should hand defined indexes on a class down to its child classes" do
227
- Child.indexes.should include(*Person.indexes)
288
+ Child.sphinx_indexes.should include(*Person.sphinx_indexes)
228
289
  end
229
290
 
230
291
  it "should allow associations to other STI models" do
231
- Child.indexes.last.link!
232
- sql = Child.indexes.last.to_sql.gsub('$start', '0').gsub('$end', '100')
292
+ Child.sphinx_indexes.last.link!
293
+ sql = Child.sphinx_indexes.last.to_riddle_for_core(0, 0).sql_query
294
+ sql.gsub!('$start', '0').gsub!('$end', '100')
233
295
  lambda { Child.connection.execute(sql) }.should_not raise_error(ActiveRecord::StatementInvalid)
234
296
  end
235
297
  end
298
+
299
+ it "should return the sphinx document id as expected" do
300
+ person = Person.find(:first)
301
+ model_count = ThinkingSphinx.indexed_models.length
302
+ offset = ThinkingSphinx.indexed_models.index("Person")
303
+
304
+ (person.id * model_count + offset).should == person.sphinx_document_id
305
+
306
+ alpha = Alpha.find(:first)
307
+ offset = ThinkingSphinx.indexed_models.index("Alpha")
308
+
309
+ (alpha.id * model_count + offset).should == alpha.sphinx_document_id
310
+
311
+ beta = Beta.find(:first)
312
+ offset = ThinkingSphinx.indexed_models.index("Beta")
313
+
314
+ (beta.id * model_count + offset).should == beta.sphinx_document_id
315
+ end
236
316
  end