Skip to content

Commit

Permalink
Added criteria to the index to allow the selection of which objects f…
Browse files Browse the repository at this point in the history
…rom the class will be indexed
  • Loading branch information
eddloschi committed Dec 19, 2013
1 parent 5cc0602 commit d9f51dc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions lib/mongoid/giza/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def initialize(klass, settings = {})
@klass = klass
@settings = settings
@name = @klass.name.to_sym
@criteria = klass.all
@fields = []
@attributes = []
end
Expand Down Expand Up @@ -88,6 +89,10 @@ def name(new_name = nil)
@name
end

def criteria(new_criteria = nil)
@criteria = new_criteria || @criteria
end

# Generates a XML document according to the XMLPipe2 specification from Sphinx
#
# @param buffer [#<<] any IO object that supports appending content using <<
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/giza/xml_pipe2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def generate_schema
# Generates the content part of the XML document.
# Used internally by {#generate!} so you should never need to call it directly
def generate_docset
@index.klass.all.each do |object|
@index.criteria.each do |object|
@xml.sphinx :document, id: object.giza_id do
generate_doc_tags(@index.fields, object)
generate_doc_tags(@index.attributes, object)
Expand Down
18 changes: 18 additions & 0 deletions spec/mongoid/giza/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
let(:klass) do
klass = double("klass")
allow(klass).to receive(:name) { "Klass" }
allow(klass).to receive(:all)
klass
end

Expand Down Expand Up @@ -141,4 +142,21 @@
index.generate_xmlpipe2(buffer)
end
end

describe "criteria" do
let(:all) { double("all") }

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

it "should default to all" do
allow(klass).to receive(:all) { all }
expect(index.criteria).to be(all)
end

it "should accept a new criteria as a parameter" do
allow(klass).to receive(:where) { criteria }
index.criteria(klass.where(name: "one"))
expect(index.criteria).to be(criteria)
end
end
end
4 changes: 1 addition & 3 deletions spec/mongoid/giza/xml_pipe2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@

describe "generate_docset" do
before do
person_class = double("Person")
field = double("field")
attribute = double("attribute")
person = double("person")
allow(@index).to receive(:klass) { person_class }
allow(@index).to receive(:fields) { [field] }
allow(@index).to receive(:attributes) { [attribute] }
allow(person_class).to receive(:all) { [person] }
allow(@index).to receive(:criteria) { [person] }
allow(person).to receive(:giza_id) { 1 }
allow(xmlpipe2).to receive(:generate_doc_tags).with([field], person) do
@buffer << "<name>Person One</name>"
Expand Down

0 comments on commit d9f51dc

Please sign in to comment.