Skip to content

Commit

Permalink
Adds generated indexes to the configuration when they are created
Browse files Browse the repository at this point in the history
  • Loading branch information
eddloschi committed Dec 7, 2013
1 parent b30e2f9 commit 47cdfa9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/mongoid/giza.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ def sphinx_index(settings = {}, &block)
# The block receives one argument that is the current object of the class for which the index will be generated
def add_dynamic_sphinx_index(settings, block)
dynamic_index = DynamicIndex.new(self, settings, block)
sphinx_generated_indexes.merge(dynamic_index.generate!)
sphinx_dynamic_indexes << dynamic_index
generated = dynamic_index.generate!
sphinx_generated_indexes.merge!(generated)
generated.each { |name, index| @giza_configuration.add_index(index, true) }
end

# Adds an static index to the class
Expand Down
22 changes: 19 additions & 3 deletions spec/mongoid/giza_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ class Person
describe "add_dynamic_sphinx_index" do
let(:dynamic_index) { double("dynamic index") }

let(:generated) { double("generated") }

before do
allow(dynamic_index).to receive(:generate!) { generated }
allow(generated).to receive(:each)
end

it "should create a dynamic index" do
allow(dynamic_index).to receive(:generate!) { double.as_null_object }
allow(Person).to receive(:sphinx_generated_indexes) { double.as_null_object }
Expand All @@ -109,12 +116,10 @@ class Person
end

it "should register the generated indexes" do
generated = double("generated")
indexes = double("indexes")
allow(Mongoid::Giza::DynamicIndex).to receive(:new) { dynamic_index }
allow(dynamic_index).to receive(:generate!) { generated }
allow(Person).to receive(:sphinx_generated_indexes) { indexes }
expect(indexes).to receive(:merge).with(generated)
expect(indexes).to receive(:merge!).with(generated)
Person.add_dynamic_sphinx_index({}, Proc.new { })
end

Expand All @@ -127,6 +132,17 @@ class Person
expect(indexes).to receive(:<<).with(dynamic_index)
Person.add_dynamic_sphinx_index({}, Proc.new { })
end

it "should add the generated indexes to the configuration" do
each_generated = double("each generated")
indexes = double("indexes")
allow(Person).to receive(:sphinx_generated_indexes) { indexes }
allow(indexes).to receive(:merge!)
allow(Mongoid::Giza::DynamicIndex).to receive(:new) { dynamic_index }
allow(generated).to receive(:each).and_yield(:Person, each_generated)
expect(config).to receive(:add_index)
Person.add_dynamic_sphinx_index({}, Proc.new { })
end
end

describe "search" do
Expand Down

0 comments on commit 47cdfa9

Please sign in to comment.