Skip to content

Commit

Permalink
Adding the ability to set indexed models, instead of TS figuring it o…
Browse files Browse the repository at this point in the history
…ut (which can be slow).
  • Loading branch information
pat committed Jun 5, 2010
1 parent 72262c4 commit 575b4c3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/thinking_sphinx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
5 changes: 4 additions & 1 deletion lib/thinking_sphinx/context.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 27 additions & 0 deletions spec/thinking_sphinx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 575b4c3

Please sign in to comment.