Skip to content

Commit

Permalink
Adding type options for field if fields were defined.
Browse files Browse the repository at this point in the history
  • Loading branch information
Durran Jordan and Tim Pope authored and durran committed Jan 7, 2010
1 parent b4c6a66 commit 1b02507
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/mongoid/criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def and(selector = nil)
#
# Returns: <tt>Integer</tt>
def count
@count ||= @klass.collection.find(@selector, @options.dup).count
@count ||= @klass.collection.find(@selector, process_options).count
end

# Iterate over each +Document+ in the results. This can take an optional
Expand Down Expand Up @@ -228,7 +228,7 @@ def initialize(klass)
#
# <tt>Criteria.select(:name).where(:name = "Chrissy").last</tt>
def last
opts = @options.dup
opts = process_options
sorting = opts[:sort]
sorting = [[:_id, :asc]] unless sorting
opts[:sort] = sorting.collect { |option| [ option[0], option[1].invert ] }
Expand Down Expand Up @@ -372,7 +372,7 @@ def offset
#
# <tt>Criteria.select(:name).where(:name = "Chrissy").one</tt>
def one
attributes = @klass.collection.find_one(@selector, @options.dup)
attributes = @klass.collection.find_one(@selector, process_options)
attributes ? attributes["_type"].constantize.instantiate(attributes) : nil
end

Expand Down Expand Up @@ -537,7 +537,7 @@ def where(selector = nil)
# If this is a +Criteria+ to find multiple results, will return an +Array+ of
# objects of the type of class provided.
def execute
attributes = @klass.collection.find(@selector, @options.dup)
attributes = @klass.collection.find(@selector, process_options)
if attributes
@count = attributes.count
attributes.collect { |doc| doc["_type"].constantize.instantiate(doc) }
Expand All @@ -546,6 +546,17 @@ def execute
end
end

# Filters the field list. If no fields have been supplied, then it will be
# empty. If fields have been defined then _type will be included as well.
def process_options
fields = @options[:fields]
if fields && fields.size > 0 && !fields.include?(:_type)
fields << :_type
@options[:fields] = fields
end
@options.dup
end

# Filters the unused options out of the options +Hash+. Currently this
# takes into account the "page" and "per_page" options that would be passed
# in if using will_paginate.
Expand All @@ -558,12 +569,6 @@ def filter_options
end
end

# Update the selector setting the operator on the value for each key in the
# supplied attributes +Hash+.
def update_selector(attributes, operator)
attributes.each { |key, value| @selector[key] = { operator => value } }; self
end

# Common functionality for grouping operations. Currently used by min, max
# and sum. Will gsub the field name in the supplied reduce function.
def grouped(start, field, reduce)
Expand All @@ -576,5 +581,12 @@ def grouped(start, field, reduce)
)
collection.first[start.to_s]
end

# Update the selector setting the operator on the value for each key in the
# supplied attributes +Hash+.
def update_selector(attributes, operator)
attributes.each { |key, value| @selector[key] = { operator => value } }; self
end

end
end
9 changes: 9 additions & 0 deletions spec/integration/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@

end

context "limiting result fields" do

it "adds the type field to the options" do
people = Person.all(:fields => [ :title ])
people.first.title.should == "Test"
end

end

end

describe "#find_by_id" do
Expand Down

0 comments on commit 1b02507

Please sign in to comment.