Permalink
Browse files

Adding the ability to set indexed models, instead of TS figuring it o…

…ut (which can be slow).
  • Loading branch information...
1 parent 72262c4 commit 575b4c3d705379e544f673a0b770337ddacfed9a @pat pat committed Jun 5, 2010
Showing with 42 additions and 2 deletions.
  1. +11 −1 lib/thinking_sphinx.rb
  2. +4 −1 lib/thinking_sphinx/context.rb
  3. +27 −0 spec/thinking_sphinx_spec.rb
View
12 lib/thinking_sphinx.rb
@@ -70,20 +70,30 @@ def self.context
if @@context.nil?
@@sphinx_mutex.synchronize do
if @@context.nil?
- @@context = ThinkingSphinx::Context.new
+ set_context
@@context.prepare
end
end
end
@@context
end
+
+ def self.set_context(*classes)
+ @@context = ThinkingSphinx::Context.new(*classes)
+ end
def self.reset_context!
@@sphinx_mutex.synchronize do
@@context = nil
end
end
+
+ def self.indexed_models=(classes = [])
+ @@sphinx_mutex.synchronize do
+ set_context *classes
+ end
+ end
def self.unique_id_expression(offset = nil)
"* #{context.indexed_models.size} + #{offset || 0}"
View
5 lib/thinking_sphinx/context.rb
@@ -1,8 +1,11 @@
class ThinkingSphinx::Context
attr_reader :indexed_models
- def initialize
+ def initialize(*models)
@indexed_models = []
+ models.each do |model|
+ add_indexed_model model
+ end
end
def prepare
View
27 spec/thinking_sphinx_spec.rb
@@ -16,6 +16,22 @@
end
end
+ describe '.set_context' do
+ before :each do
+ ThinkingSphinx.reset_context!
+ end
+
+ it "should set the context instance" do
+ ThinkingSphinx.set_context
+ ThinkingSphinx.context.should be_a(ThinkingSphinx::Context)
+ end
+
+ it "should pass through any given arguments to the initialiser" do
+ ThinkingSphinx::Context.should_receive(:new).with(Alpha, Beta)
+ ThinkingSphinx.set_context Alpha, Beta
+ end
+ end
+
describe '.reset_context!' do
it "should remove the existing Context instance" do
existing = ThinkingSphinx.context
@@ -27,6 +43,17 @@
end
end
+ describe '.indexed_models=' do
+ before :each do
+ ThinkingSphinx.reset_context!
+ end
+
+ it "should set the indexed models for the context" do
+ ThinkingSphinx.indexed_models = [Alpha, Beta]
+ ThinkingSphinx.context.indexed_models.should == ['Alpha', 'Beta']
+ end
+ end
+
describe '.define_indexes?' do
it "should define indexes by default" do
ThinkingSphinx.define_indexes?.should be_true

0 comments on commit 575b4c3

Please sign in to comment.