lib/mongoid/finders.rb in mongoid-2.8.1 vs lib/mongoid/finders.rb in mongoid-3.0.0.rc

- old
+ new

@@ -1,44 +1,27 @@ # encoding: utf-8 -module Mongoid #:nodoc: +module Mongoid # This module defines the finder methods that hang off the document at the # class level. module Finders + extend Origin::Forwardable - # Delegate to the criteria methods that are natural for creating a new - # criteria. - critera_methods = [ :all_in, :all_of, :any_in, :any_of, :asc, :ascending, - :avg, :desc, :descending, :excludes, - :includes, :limit, :max, :min, :not_in, :only, - :order_by, :search, :skip, :sum, :without, :where, - :update, :update_all, :near ] - delegate *(critera_methods.dup << { :to => :criteria }) + select_with :with_default_scope + delegate :aggregates, :avg, :each, :extras, :find_and_modify, :includes, + :map_reduce, :max, :min, :sum, :update, :update_all, to: :with_default_scope - # Find all documents that match the given conditions. + # Returns a count of records in the database. + # If you want to specify conditions use where. # - # @example Find all matching documents given conditions. - # Person.all(:conditions => { :attribute => "value" }) - # - # @param [ Array ] args The conditions with options. - # - # @return [ Criteria ] The matching documents. - def all(*args) - find(:all, *args) - end - - # Returns a count of matching records in the database based on the - # provided arguments. - # # @example Get the count of matching documents. - # Person.count(:conditions => { :attribute => "value" }) + # Person.count + # Person.where(title: "Sir").count # - # @param [ Array ] args The conditions. - # # @return [ Integer ] The number of matching documents. - def count(*args) - find(:all, *args).count + def count + with_default_scope.count end # Returns true if count is zero # # @example Are there no saved documents for this model? @@ -51,39 +34,33 @@ # Returns true if there are on document in database based on the # provided arguments. # # @example Do any documents exist for the conditions? - # Person.exists?(:conditions => { :attribute => "value" }) + # Person.exists? # # @param [ Array ] args The conditions. - def exists?(*args) - find(:all, *args).count > 0 + def exists? + with_default_scope.exists? 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. # - # @example Find the first matching document. - # Person.find(:first, :conditions => { :attribute => "value" }) - # - # @example Find all matching documents. - # Person.find(:all, :conditions => { :attribute => "value" }) - # # @example Find a single document by an id. # Person.find(BSON::ObjectId) # # @param [ Array ] args An assortment of finder options. # # @return [ Document, nil, Criteria ] A document or matching documents. def find(*args) - criteria.find(*args) + with_default_scope.find(*args) end # Find the first +Document+ given the conditions, or creates a new document # with the conditions that were supplied. # @@ -108,32 +85,54 @@ # @return [ Document ] A matching or newly initialized document. def find_or_initialize_by(attrs = {}, &block) find_or(:new, attrs, &block) end + # Find the first +Document+ given the conditions, or raises + # Mongoid::Errors::DocumentNotFound + # + # @example Find the document by attribute other than id + # Person.find_by(:username => "superuser") + # + # @param [ Hash ] attrs The attributes to check. + # + # @raise [ Errors::DocumentNotFound ] If no document found. + # + # @return [ Document ] A matching document. + # + # @since 3.0.0 + def find_by(attrs = {}) + result = where(attrs).first + if result.nil? && Mongoid.raise_not_found_error + raise(Errors::DocumentNotFound.new(self, attrs)) + end + yield(result) if result && block_given? + result + end + # Find the first +Document+ given the conditions. # # @example Find the first document. # Person.first(:conditions => { :attribute => "value" }) # # @param [ Array ] args The conditions with options. # # @return [ Document ] The first matching document. - def first(*args) - find(:first, *args) + def first + with_default_scope.first end # Find the last +Document+ given the conditions. # # @example Find the last document. # Person.last(:conditions => { :attribute => "value" }) # # @param [ Array ] args The conditions with options. # # @return [ Document ] The last matching document. - def last(*args) - find(:last, *args) + def last + with_default_scope.last end protected # Find the first object or create/initialize it. @@ -144,9 +143,9 @@ # @param [ Symbol ] method The method to invoke. # @param [ Hash ] attrs The attributes to query or set. # # @return [ Document ] The first or new document. def find_or(method, attrs = {}, &block) - first(:conditions => attrs) || send(method, attrs, &block) + where(attrs).first || send(method, attrs, &block) end end end