Skip to content

Commit

Permalink
If method_missing gets called on criteria without another criteria, e…
Browse files Browse the repository at this point in the history
…xpect array behaviour
  • Loading branch information
durran committed Dec 1, 2009
1 parent 9566e94 commit 9c58921
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/mongoid/criteria.rb
Expand Up @@ -292,9 +292,13 @@ def merge(other)
#
# Returns: <tt>Criteria</tt>
def method_missing(name, *args)
new_scope = @klass.send(name)
new_scope.merge(self)
new_scope
if @klass.respond_to?(name)
new_scope = @klass.send(name)
new_scope.merge(self)
return new_scope
else
return collect.send(name, *args)
end
end

# Adds a criterion to the +Criteria+ that specifies values where none
Expand Down Expand Up @@ -367,11 +371,6 @@ def per_page
(@options[:limit] || 20).to_i
end

# Execute the criteria and collect in order to handle random requests.
def rand
collect.rand
end

# Adds a criterion to the +Criteria+ that specifies the fields that will
# get returned from the Document. Used mainly for list views that do not
# require all fields to be present. This is similar to SQL "SELECT" values.
Expand Down
28 changes: 28 additions & 0 deletions spec/unit/mongoid/criteria_spec.rb
Expand Up @@ -457,6 +457,34 @@

end

context "when expecting behaviour of an array" do

before do
@array = mock
@document = mock
end

describe "#[]" do

it "collects the criteria and calls []" do
@criteria.expects(:collect).returns([@document])
@criteria[0].should == @document
end

end

describe "#rand" do

it "collects the criteria and call rand" do
@criteria.expects(:collect).returns(@array)
@array.expects(:send).with(:rand).returns(@document)
@criteria.rand
end

end

end

end

describe "#not_in" do
Expand Down

0 comments on commit 9c58921

Please sign in to comment.