lib/mongoid/finders.rb in mongoid-2.0.0.beta.20 vs lib/mongoid/finders.rb in mongoid-2.0.0.rc.1
- old
+ new
@@ -26,48 +26,57 @@
# Returns a count of matching records in the database based on the
# provided arguments.
#
# <tt>Person.count(:conditions => { :attribute => "value" })</tt>
def count(*args)
- Criteria.translate(self, *args).count
+ Criteria.translate(self, false, *args).count
end
# Returns true if there are on document in database based on the
# provided arguments.
#
# <tt>Person.exists?(:conditions => { :attribute => "value" })</tt>
def exists?(*args)
- Criteria.translate(self, *args).limit(1).count == 1
+ Criteria.translate(self, false, *args).limit(1).count == 1
end
# Helper to initialize a new +Criteria+ object for this class, or return
# the currently scoped +Criteria+ object.
#
# Example:
#
# <tt>Person.criteria</tt>
- def criteria
- scope_stack.last || Criteria.new(self)
+ def criteria(embedded = false)
+ scope_stack.last || Criteria.new(self, embedded)
end
# Find a +Document+ in several different ways.
#
# If a +String+ is provided, it will be assumed that it is a
# representation of a Mongo::ObjectID and will attempt to find a single
# +Document+ based on that id. If a +Symbol+ and +Hash+ is provided then
# it will attempt to find either a single +Document+ or multiples based
# on the conditions provided and the first parameter.
#
- # <tt>Person.find(:first, :conditions => { :attribute => "value" })</tt>
+ # Example:
#
+ # <tt>Person.find(:first, :conditions => { :attribute => "value" })</tt>
# <tt>Person.find(:all, :conditions => { :attribute => "value" })</tt>
+ # <tt>Person.find(BSON::ObjectId)</tt>
#
- # <tt>Person.find(Mongo::ObjectID.new.to_s)</tt>
+ # Options:
+ #
+ # args: An assortment of finder options.
+ #
+ # Returns:
+ #
+ # A document or criteria.
def find(*args)
- raise Errors::InvalidOptions.new(:calling_document_find_with_nil_is_invalid, {}) if args[0].nil?
- type = args.delete_at(0) if args[0].is_a?(Symbol)
- criteria = Criteria.translate(self, *args)
+ raise Errors::InvalidOptions.new(
+ :calling_document_find_with_nil_is_invalid, {}
+ ) if args[0].nil?
+ type, criteria = Criteria.parse!(self, false, *args)
case type
when :first then return criteria.one
when :last then return criteria.last
else
return criteria
@@ -132,11 +141,11 @@
# <tt>Person.paginate(:conditions => { :field => "Test" }, :page => 1,
# :per_page => 20)</tt>
#
# Returns paginated array of docs.
def paginate(params = {})
- Criteria.translate(self, params).paginate
+ Criteria.translate(self, false, params).paginate
end
protected
# Find the first object or create/initialize it.
def find_or(method, attrs = {})
@@ -152,10 +161,9 @@
# Pushes the provided criteria onto the scope stack, and removes it after the
# provided block is yielded.
def with_scope(criteria)
scope_stack = self.scope_stack
scope_stack << criteria
-
begin
yield criteria
ensure
scope_stack.pop
end