From a4d6b69d2f1527dcfbed99a6caadf433c8644c6c Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Thu, 1 Oct 2009 19:48:28 -0400 Subject: [PATCH] Moving finders into their own module --- lib/mongoid.rb | 7 +++--- lib/mongoid/document.rb | 49 +------------------------------------ lib/mongoid/finders.rb | 53 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 51 deletions(-) create mode 100644 lib/mongoid/finders.rb diff --git a/lib/mongoid.rb b/lib/mongoid.rb index f19f10718c..ab5fa60934 100644 --- a/lib/mongoid.rb +++ b/lib/mongoid.rb @@ -1,5 +1,5 @@ # Copyright (c) 2009 Durran Jordan -# +# # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including @@ -7,10 +7,10 @@ # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: -# +# # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -38,6 +38,7 @@ require "mongoid/extensions/array/conversions" require "mongoid/extensions/object/conversions" require "mongoid/extensions" +require "mongoid/finders" require "mongoid/document" module Mongoid diff --git a/lib/mongoid/document.rb b/lib/mongoid/document.rb index eaa43c0419..bce1ca79ec 100644 --- a/lib/mongoid/document.rb +++ b/lib/mongoid/document.rb @@ -2,9 +2,7 @@ module Mongoid #:nodoc: class Document #:nodoc: include ActiveSupport::Callbacks include Validatable - - AGGREGATE_REDUCE = "function(obj, prev) { prev.count++; }" - GROUP_BY_REDUCE = "function(obj, prev) { prev.group.push(obj); }" + extend Finders attr_reader :attributes, :parent @@ -50,38 +48,6 @@ def fields(*names) end end - # Find all Documents in several ways. - # Model.find(:first, :attribute => "value") - # Model.find(:all, :attribute => "value") - def find(*args) - type, selector = args[0], args[1] - case type - when :all then find_all(selector[:conditions]) - when :first then find_first(selector[:conditions]) - else find_first(Mongo::ObjectID.from_string(type.to_s)) - end - end - - # Find a single Document given the passed selector, which is a Hash of attributes that - # must match the Document in the database exactly. - def find_first(selector = nil) - new(collection.find_one(selector)) - end - - # Find all Documents given the passed selector, which is a Hash of attributes that - # must match the Document in the database exactly. - def find_all(selector = nil) - collection.find(selector).collect { |doc| new(doc) } - end - - # Find all Documents given the supplied criteria, grouped by the fields - # provided. - def group_by(fields, selector) - collection.group(fields, selector, { :group => [] }, GROUP_BY_REDUCE).collect do |docs| - group!(docs) - end - end - # Create a one-to-many association between Documents. def has_many(association_name) add_association(:has_many, association_name.to_s.classify, association_name) @@ -98,19 +64,6 @@ def index(name, options = { :unique => false }) collection.create_index(name, options) end - # Find all documents in paginated fashion given the supplied arguments. - # If no parameters are passed just default to offset 0 and limit 20. - def paginate(params = {}) - WillPaginate::Collection.create( - params[:page] || 1, - params[:per_page] || 20, - 0) do |pager| - results = collection.find(params[:conditions], { :limit => pager.per_page, :offset => pager.offset }) - pager.total_entries = results.count - pager.replace(results.collect { |doc| new(doc) }) - end - end - end # Get the Mongo::Collection associated with this Document. diff --git a/lib/mongoid/finders.rb b/lib/mongoid/finders.rb new file mode 100644 index 0000000000..e12bd1387c --- /dev/null +++ b/lib/mongoid/finders.rb @@ -0,0 +1,53 @@ +module Mongoid + module Finders + + AGGREGATE_REDUCE = "function(obj, prev) { prev.count++; }" + GROUP_BY_REDUCE = "function(obj, prev) { prev.group.push(obj); }" + + # Find all Documents in several ways. + # Model.find(:first, :attribute => "value") + # Model.find(:all, :attribute => "value") + def find(*args) + type, selector = args[0], args[1] + case type + when :all then find_all(selector[:conditions]) + when :first then find_first(selector[:conditions]) + else find_first(Mongo::ObjectID.from_string(type.to_s)) + end + end + + # Find a single Document given the passed selector, which is a Hash of attributes that + # must match the Document in the database exactly. + def find_first(selector = nil) + new(collection.find_one(selector)) + end + + # Find all Documents given the passed selector, which is a Hash of attributes that + # must match the Document in the database exactly. + def find_all(selector = nil) + collection.find(selector).collect { |doc| new(doc) } + end + + # Find all Documents given the supplied criteria, grouped by the fields + # provided. + def group_by(fields, selector) + collection.group(fields, selector, { :group => [] }, GROUP_BY_REDUCE).collect do |docs| + group!(docs) + end + end + + # Find all documents in paginated fashion given the supplied arguments. + # If no parameters are passed just default to offset 0 and limit 20. + def paginate(params = {}) + WillPaginate::Collection.create( + params[:page] || 1, + params[:per_page] || 20, + 0) do |pager| + results = collection.find(params[:conditions], { :limit => pager.per_page, :offset => pager.offset }) + pager.total_entries = results.count + pager.replace(results.collect { |doc| new(doc) }) + end + end + + end +end