lib/mongoid/finders.rb in mongoid-2.0.0.rc.7 vs lib/mongoid/finders.rb in mongoid-2.0.0.rc.8

- old
+ new

@@ -1,14 +1,17 @@ # encoding: utf-8 module Mongoid #:nodoc: - module Finders #:nodoc: + # This module defines the finder methods that hang off the document at the + # class level. + module Finders + # Delegate to the criteria methods that are natural for creating a new # criteria. [ :all_in, :any_in, :any_of, :asc, :ascending, :avg, :desc, :descending, :excludes, :limit, :max, :min, :not_in, :only, :order_by, - :skip, :sum, :where, :update, :update_all, :near ].each do |name| + :skip, :sum, :without, :where, :update, :update_all, :near ].each do |name| define_method(name) do |*args| criteria.send(name, *args) end end @@ -26,31 +29,21 @@ # 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, false, *args).count + find(:all, *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, false, *args).limit(1).count == 1 + find(:all, *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(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 @@ -69,44 +62,35 @@ # # Returns: # # A document or criteria. def find(*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 - end + criteria.find(*args) end # Find the first +Document+ given the conditions, or creates a new document # with the conditions that were supplied # # Options: # # args: A +Hash+ of attributes # # <tt>Person.find_or_create_by(:attribute => "value")</tt> - def find_or_create_by(attrs = {}) - find_or(:create, attrs) + def find_or_create_by(attrs = {}, &block) + find_or(:create, attrs, &block) end # Find the first +Document+ given the conditions, or instantiates a new document # with the conditions that were supplied # # Options: # # args: A +Hash+ of attributes # # <tt>Person.find_or_initialize_by(:attribute => "value")</tt> - def find_or_initialize_by(attrs = {}) - find_or(:new, attrs) + def find_or_initialize_by(attrs = {}, &block) + find_or(:new, attrs, &block) end # Find the first +Document+ given the conditions. # # Options: @@ -141,33 +125,15 @@ # <tt>Person.paginate(:conditions => { :field => "Test" }, :page => 1, # :per_page => 20)</tt> # # Returns paginated array of docs. def paginate(params = {}) - Criteria.translate(self, false, params).paginate + find(:all, params).paginate end protected # Find the first object or create/initialize it. - def find_or(method, attrs = {}) - first(:conditions => attrs) || send(method, attrs) - end - - # Initializes and returns the current scope stack. - def scope_stack - scope_stack_for = Thread.current[:mongoid_scope_stack] ||= {} - scope_stack_for[object_id] ||= [] - end - - # 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 + def find_or(method, attrs = {}, &block) + first(:conditions => attrs) || send(method, attrs, &block) end end end