Skip to content

Commit

Permalink
Moving collection accessors back into Document
Browse files Browse the repository at this point in the history
  • Loading branch information
Durran Jordan and Sandro Turriate authored and durran committed Oct 4, 2009
1 parent c01c123 commit 87ffb58
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 87 deletions.
13 changes: 12 additions & 1 deletion lib/mongoid/document.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Mongoid #:nodoc:
class Document #:nodoc:
include Finders
extend Finders
include ActiveSupport::Callbacks
include Validatable

Expand All @@ -18,6 +18,12 @@ def belongs_to(association_name)
add_association(:belongs_to, association_name.to_s.classify, association_name)
end

# Get the Mongo::Collection associated with this Document.
def collection
@collection_name = self.to_s.demodulize.tableize
@collection ||= Mongoid.database.collection(@collection_name)
end

# Create a new Document with the supplied attribtues, and insert it into the database.
def create(attributes = {})
new(attributes).save(true)
Expand Down Expand Up @@ -71,6 +77,11 @@ def index(name, options = { :unique => false })

end

# Get the Mongo::Collection associated with this Document.
def collection
self.class.collection
end

# Delete this Document from the database.
def destroy
collection.remove(:_id => id)
Expand Down
125 changes: 51 additions & 74 deletions lib/mongoid/finders.rb
Original file line number Diff line number Diff line change
@@ -1,91 +1,68 @@
module Mongoid #:nodoc:
module Finders #:nodoc:

def self.included(base)
base.class_eval do
include InstanceMethods; extend ClassMethods
end
end
AGGREGATE_REDUCE = "function(obj, prev) { prev.count++; }"
GROUP_BY_REDUCE = "function(obj, prev) { prev.group.push(obj); }"

module InstanceMethods #:nodoc:
# Get the Mongo::Collection associated with this Document.
def collection
self.class.collection
end
# Create an association to a parent Document.
# Get an aggregate count for the supplied group of fields and the
# selector that is provided.
def aggregate(fields, params = {})
selector = params[:conditions]
collection.group(fields, selector, { :count => 0 }, AGGREGATE_REDUCE)
end

module ClassMethods #:nodoc:

AGGREGATE_REDUCE = "function(obj, prev) { prev.count++; }"
GROUP_BY_REDUCE = "function(obj, prev) { prev.group.push(obj); }"

# Create an association to a parent Document.
# Get an aggregate count for the supplied group of fields and the
# selector that is provided.
def aggregate(fields, params = {})
selector = params[:conditions]
collection.group(fields, selector, { :count => 0 }, AGGREGATE_REDUCE)
end

# Get the Mongo::Collection associated with this Document.
def collection
@collection_name = self.to_s.demodulize.tableize
@collection ||= Mongoid.database.collection(@collection_name)
end

# Find all Documents in several ways.
# Model.find(:first, :attribute => "value")
# Model.find(:all, :attribute => "value")
def find(*args)
type, params = args[0], args[1]
case type
when :all then find_all(params)
when :first then find_first(params)
else find_first(Mongo::ObjectID.from_string(type.to_s))
end
# Find all Documents in several ways.
# Model.find(:first, :attribute => "value")
# Model.find(:all, :attribute => "value")
def find(*args)
type, params = args[0], args[1]
case type
when :all then find_all(params)
when :first then find_first(params)
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(params = {})
case params
when Hash then new(collection.find_one(params[:conditions]))
else new(collection.find_one(params))
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(params = {})
case params
when Hash then new(collection.find_one(params[:conditions]))
else new(collection.find_one(params))
end
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(params = {})
selector = params.delete(:conditions)
collection.find(selector, params).collect { |doc| new(doc) }
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(params = {})
selector = params.delete(:conditions)
collection.find(selector, params).collect { |doc| new(doc) }
end

# Find all Documents given the supplied criteria, grouped by the fields
# provided.
def group_by(fields, params = {})
selector = params[:condition]
collection.group(fields, selector, { :group => [] }, GROUP_BY_REDUCE).collect do |docs|
docs["group"] = docs["group"].collect { |attrs| new(attrs) }; docs
end
# Find all Documents given the supplied criteria, grouped by the fields
# provided.
def group_by(fields, params = {})
selector = params[:condition]
collection.group(fields, selector, { :group => [] }, GROUP_BY_REDUCE).collect do |docs|
docs["group"] = docs["group"].collect { |attrs| new(attrs) }; 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 = {})
selector = params[:conditions]
WillPaginate::Collection.create(
params[:page] || 1,
params[:per_page] || 20,
0) do |pager|
results = collection.find(selector, { :sort => (params[:sort] || {}),
:limit => pager.per_page,
:offset => pager.offset })
pager.total_entries = results.count
pager.replace(results.collect { |doc| new(doc) })
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 = {})
selector = params[:conditions]
WillPaginate::Collection.create(
params[:page] || 1,
params[:per_page] || 20,
0) do |pager|
results = collection.find(selector, { :sort => (params[:sort] || {}),
:limit => pager.per_page,
:offset => pager.offset })
pager.total_entries = results.count
pager.replace(results.collect { |doc| new(doc) })
end

end

end
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@

end

describe "#collection" do

before do
@person = Person.new
end

it "sets the collection name to the class pluralized" do
Person.collection.name.should == "people"
end

end

describe "#create" do

context "with no attributes" do
Expand Down
12 changes: 0 additions & 12 deletions spec/unit/mongoid/finders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@

end

describe "#collection" do

before do
@person = Person.new
end

it "sets the collection name to the class pluralized" do
Person.collection.name.should == "people"
end

end

describe "#find" do

before do
Expand Down

0 comments on commit 87ffb58

Please sign in to comment.