mongoid 0.2.7 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +1 -0
- data/Rakefile +2 -0
- data/VERSION +1 -1
- data/lib/mongoid.rb +1 -7
- data/lib/mongoid/associations.rb +5 -0
- data/lib/mongoid/associations/belongs_to_association.rb +7 -4
- data/lib/mongoid/associations/decorator.rb +25 -0
- data/lib/mongoid/associations/{association_factory.rb → factory.rb} +2 -2
- data/lib/mongoid/associations/has_many_association.rb +7 -0
- data/lib/mongoid/associations/has_one_association.rb +1 -12
- data/lib/mongoid/document.rb +56 -2
- data/lib/mongoid/extensions.rb +3 -0
- data/mongoid.gemspec +17 -11
- data/spec/spec_helper.rb +8 -0
- data/spec/unit/mongoid/associations/belongs_to_association_spec.rb +18 -1
- data/spec/unit/mongoid/associations/decorator_spec.rb +36 -0
- data/spec/unit/mongoid/associations/{association_factory_spec.rb → factory_spec.rb} +7 -7
- data/spec/unit/mongoid/associations/has_many_association_spec.rb +29 -2
- data/spec/unit/mongoid/document_spec.rb +142 -5
- metadata +29 -9
- data/History.txt +0 -10
- data/lib/mongoid/finders.rb +0 -60
- data/spec/unit/mongoid/finders_spec.rb +0 -165
data/README.textile
CHANGED
@@ -17,6 +17,7 @@
|
|
17
17
|
|
18
18
|
<a href="http://www.pivotaltracker.com/projects/27482">Mongoid on Pivotal Tracker</a>
|
19
19
|
<a href="http://groups.google.com/group/mongoid">Mongoid Google Group</a>
|
20
|
+
<a href="http://ci.hshrckt.com:4891/">Mongoid on CI Joe</a>
|
20
21
|
|
21
22
|
<h3>Compatibility</h3>
|
22
23
|
|
data/Rakefile
CHANGED
@@ -12,6 +12,8 @@ begin
|
|
12
12
|
gem.email = "durran@gmail.com"
|
13
13
|
gem.homepage = "http://github.com/durran/mongoid"
|
14
14
|
gem.authors = ["Durran Jordan"]
|
15
|
+
gem.add_dependency "durran-validatable"
|
16
|
+
gem.add_dependency "mislav-will_paginate"
|
15
17
|
gem.add_dependency "activesupport"
|
16
18
|
gem.add_dependency "mongodb-mongo"
|
17
19
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/mongoid.rb
CHANGED
@@ -31,14 +31,8 @@ require "active_support/core_ext"
|
|
31
31
|
require "delegate"
|
32
32
|
require "will_paginate/collection"
|
33
33
|
require "mongo"
|
34
|
-
require "mongoid/associations
|
35
|
-
require "mongoid/associations/belongs_to_association"
|
36
|
-
require "mongoid/associations/has_many_association"
|
37
|
-
require "mongoid/associations/has_one_association"
|
38
|
-
require "mongoid/extensions/array/conversions"
|
39
|
-
require "mongoid/extensions/object/conversions"
|
34
|
+
require "mongoid/associations"
|
40
35
|
require "mongoid/extensions"
|
41
|
-
require "mongoid/finders"
|
42
36
|
require "mongoid/document"
|
43
37
|
|
44
38
|
module Mongoid
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module Mongoid #:nodoc:
|
2
2
|
module Associations #:nodoc:
|
3
3
|
class BelongsToAssociation #:nodoc:
|
4
|
+
include Decorator
|
4
5
|
|
5
6
|
# Creates the new association by setting the internal
|
6
7
|
# document as the passed in Document. This should be the
|
@@ -10,12 +11,14 @@ module Mongoid #:nodoc:
|
|
10
11
|
# to the internal document itself.
|
11
12
|
def initialize(document)
|
12
13
|
@document = document.parent
|
14
|
+
decorate!
|
13
15
|
end
|
14
16
|
|
15
|
-
#
|
16
|
-
#
|
17
|
-
|
18
|
-
|
17
|
+
# Returns the parent document. The id param is present for
|
18
|
+
# compatibility with rails, however this could be overwritten
|
19
|
+
# in the future.
|
20
|
+
def find(id)
|
21
|
+
@document
|
19
22
|
end
|
20
23
|
|
21
24
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
module Associations #:nodoc:
|
3
|
+
module Decorator #:nodoc:
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
attr_reader :document
|
7
|
+
|
8
|
+
# Grabs all the public methods on the document and adds them
|
9
|
+
# to the association class. This is preferred over method_missing
|
10
|
+
# since we can ask the class for its methods and get an
|
11
|
+
# accurate list.
|
12
|
+
def decorate!
|
13
|
+
document.public_methods(false).each do |method|
|
14
|
+
(class << self; self; end).class_eval do
|
15
|
+
define_method method do |*args|
|
16
|
+
document.send method, *args
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Mongoid #:nodoc:
|
2
2
|
module Associations #:nodoc:
|
3
|
-
class
|
3
|
+
class Factory #:nodoc:
|
4
4
|
|
5
5
|
# Creates a new association, based on the type provided and
|
6
6
|
# passes the name and document into the newly instantiated
|
@@ -18,4 +18,4 @@ module Mongoid #:nodoc:
|
|
18
18
|
|
19
19
|
end
|
20
20
|
end
|
21
|
-
end
|
21
|
+
end
|
@@ -31,6 +31,13 @@ module Mongoid #:nodoc:
|
|
31
31
|
object
|
32
32
|
end
|
33
33
|
|
34
|
+
# Finds a document in this association.
|
35
|
+
# If :all is passed, returns all the documents
|
36
|
+
# If an id is passed, will return the document for that id.
|
37
|
+
def find(param)
|
38
|
+
return @documents if param == :all
|
39
|
+
return detect { |document| document.id == param }
|
40
|
+
end
|
34
41
|
end
|
35
42
|
end
|
36
43
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module Mongoid #:nodoc:
|
2
2
|
module Associations #:nodoc:
|
3
3
|
class HasOneAssociation #:nodoc:
|
4
|
+
include Decorator
|
4
5
|
|
5
|
-
attr_reader :document
|
6
6
|
delegate :valid?, :to => :document
|
7
7
|
|
8
8
|
# Creates the new association by finding the attributes in
|
@@ -19,17 +19,6 @@ module Mongoid #:nodoc:
|
|
19
19
|
decorate!
|
20
20
|
end
|
21
21
|
|
22
|
-
private
|
23
|
-
def decorate!
|
24
|
-
@document.public_methods(false).each do |method|
|
25
|
-
(class << self; self; end).class_eval do
|
26
|
-
define_method method do |*args|
|
27
|
-
@document.send method, *args
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
22
|
end
|
34
23
|
end
|
35
24
|
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -2,7 +2,9 @@ module Mongoid #:nodoc:
|
|
2
2
|
class Document #:nodoc:
|
3
3
|
include ActiveSupport::Callbacks
|
4
4
|
include Validatable
|
5
|
-
|
5
|
+
|
6
|
+
AGGREGATE_REDUCE = "function(obj, prev) { prev.count++; }"
|
7
|
+
GROUP_BY_REDUCE = "function(obj, prev) { prev.group.push(obj); }"
|
6
8
|
|
7
9
|
attr_reader :attributes, :parent
|
8
10
|
|
@@ -15,6 +17,12 @@ module Mongoid #:nodoc:
|
|
15
17
|
class << self
|
16
18
|
|
17
19
|
# Create an association to a parent Document.
|
20
|
+
# Get an aggregate count for the supplied group of fields and the
|
21
|
+
# selector that is provided.
|
22
|
+
def aggregate(fields, selector)
|
23
|
+
collection.group(fields, selector, { :count => 0 }, AGGREGATE_REDUCE)
|
24
|
+
end
|
25
|
+
|
18
26
|
def belongs_to(association_name)
|
19
27
|
add_association(:belongs_to, association_name.to_s.classify, association_name)
|
20
28
|
end
|
@@ -36,6 +44,31 @@ module Mongoid #:nodoc:
|
|
36
44
|
collection.drop
|
37
45
|
end
|
38
46
|
|
47
|
+
# Find all Documents in several ways.
|
48
|
+
# Model.find(:first, :attribute => "value")
|
49
|
+
# Model.find(:all, :attribute => "value")
|
50
|
+
def find(*args)
|
51
|
+
type, selector = args[0], args[1]
|
52
|
+
conditions = selector[:conditions] if selector
|
53
|
+
case type
|
54
|
+
when :all then find_all(conditions)
|
55
|
+
when :first then find_first(conditions)
|
56
|
+
else find_first(Mongo::ObjectID.from_string(type.to_s))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Find a single Document given the passed selector, which is a Hash of attributes that
|
61
|
+
# must match the Document in the database exactly.
|
62
|
+
def find_first(selector = nil)
|
63
|
+
new(collection.find_one(selector))
|
64
|
+
end
|
65
|
+
|
66
|
+
# Find all Documents given the passed selector, which is a Hash of attributes that
|
67
|
+
# must match the Document in the database exactly.
|
68
|
+
def find_all(selector = nil)
|
69
|
+
collection.find(selector).collect { |doc| new(doc) }
|
70
|
+
end
|
71
|
+
|
39
72
|
# Defines all the fields that are accessable on the Document
|
40
73
|
# For each field that is defined, a getter and setter will be
|
41
74
|
# added as an instance method to the Document.
|
@@ -48,6 +81,14 @@ module Mongoid #:nodoc:
|
|
48
81
|
end
|
49
82
|
end
|
50
83
|
|
84
|
+
# Find all Documents given the supplied criteria, grouped by the fields
|
85
|
+
# provided.
|
86
|
+
def group_by(fields, selector)
|
87
|
+
collection.group(fields, selector, { :group => [] }, GROUP_BY_REDUCE).collect do |docs|
|
88
|
+
docs["group"] = docs["group"].collect { |attrs| new(attrs) }; docs
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
51
92
|
# Create a one-to-many association between Documents.
|
52
93
|
def has_many(association_name)
|
53
94
|
add_association(:has_many, association_name.to_s.classify, association_name)
|
@@ -73,6 +114,19 @@ module Mongoid #:nodoc:
|
|
73
114
|
collection.create_index(name, options)
|
74
115
|
end
|
75
116
|
|
117
|
+
# Find all documents in paginated fashion given the supplied arguments.
|
118
|
+
# If no parameters are passed just default to offset 0 and limit 20.
|
119
|
+
def paginate(params = {})
|
120
|
+
WillPaginate::Collection.create(
|
121
|
+
params[:page] || 1,
|
122
|
+
params[:per_page] || 20,
|
123
|
+
0) do |pager|
|
124
|
+
results = collection.find(params[:conditions], { :limit => pager.per_page, :offset => pager.offset })
|
125
|
+
pager.total_entries = results.count
|
126
|
+
pager.replace(results.collect { |doc| new(doc) })
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
76
130
|
end
|
77
131
|
|
78
132
|
# Get the Mongo::Collection associated with this Document.
|
@@ -148,7 +202,7 @@ module Mongoid #:nodoc:
|
|
148
202
|
# then adds the accessors for the association.
|
149
203
|
def add_association(type, class_name, name)
|
150
204
|
define_method(name) do
|
151
|
-
Mongoid::Associations::
|
205
|
+
Mongoid::Associations::Factory.create(type, name, self)
|
152
206
|
end
|
153
207
|
define_method("#{name}=") do |object|
|
154
208
|
@attributes[name] = object.mongoidize
|
data/lib/mongoid/extensions.rb
CHANGED
data/mongoid.gemspec
CHANGED
@@ -5,44 +5,44 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Durran Jordan"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-03}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
16
16
|
]
|
17
17
|
s.files = [
|
18
18
|
".gitignore",
|
19
|
-
"History.txt",
|
20
19
|
"MIT_LICENSE",
|
21
20
|
"README.textile",
|
22
21
|
"Rakefile",
|
23
22
|
"VERSION",
|
24
23
|
"lib/mongoid.rb",
|
25
|
-
"lib/mongoid/associations
|
24
|
+
"lib/mongoid/associations.rb",
|
26
25
|
"lib/mongoid/associations/belongs_to_association.rb",
|
26
|
+
"lib/mongoid/associations/decorator.rb",
|
27
|
+
"lib/mongoid/associations/factory.rb",
|
27
28
|
"lib/mongoid/associations/has_many_association.rb",
|
28
29
|
"lib/mongoid/associations/has_one_association.rb",
|
29
30
|
"lib/mongoid/document.rb",
|
30
31
|
"lib/mongoid/extensions.rb",
|
31
32
|
"lib/mongoid/extensions/array/conversions.rb",
|
32
33
|
"lib/mongoid/extensions/object/conversions.rb",
|
33
|
-
"lib/mongoid/finders.rb",
|
34
34
|
"mongoid.gemspec",
|
35
35
|
"spec/integration/mongoid/document_spec.rb",
|
36
36
|
"spec/spec.opts",
|
37
37
|
"spec/spec_helper.rb",
|
38
|
-
"spec/unit/mongoid/associations/association_factory_spec.rb",
|
39
38
|
"spec/unit/mongoid/associations/belongs_to_association_spec.rb",
|
39
|
+
"spec/unit/mongoid/associations/decorator_spec.rb",
|
40
|
+
"spec/unit/mongoid/associations/factory_spec.rb",
|
40
41
|
"spec/unit/mongoid/associations/has_many_association_spec.rb",
|
41
42
|
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
42
43
|
"spec/unit/mongoid/document_spec.rb",
|
43
44
|
"spec/unit/mongoid/extensions/array/conversions_spec.rb",
|
44
|
-
"spec/unit/mongoid/extensions/object/conversions_spec.rb"
|
45
|
-
"spec/unit/mongoid/finders_spec.rb"
|
45
|
+
"spec/unit/mongoid/extensions/object/conversions_spec.rb"
|
46
46
|
]
|
47
47
|
s.homepage = %q{http://github.com/durran/mongoid}
|
48
48
|
s.rdoc_options = ["--charset=UTF-8"]
|
@@ -52,14 +52,14 @@ Gem::Specification.new do |s|
|
|
52
52
|
s.test_files = [
|
53
53
|
"spec/integration/mongoid/document_spec.rb",
|
54
54
|
"spec/spec_helper.rb",
|
55
|
-
"spec/unit/mongoid/associations/association_factory_spec.rb",
|
56
55
|
"spec/unit/mongoid/associations/belongs_to_association_spec.rb",
|
56
|
+
"spec/unit/mongoid/associations/decorator_spec.rb",
|
57
|
+
"spec/unit/mongoid/associations/factory_spec.rb",
|
57
58
|
"spec/unit/mongoid/associations/has_many_association_spec.rb",
|
58
59
|
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
59
60
|
"spec/unit/mongoid/document_spec.rb",
|
60
61
|
"spec/unit/mongoid/extensions/array/conversions_spec.rb",
|
61
|
-
"spec/unit/mongoid/extensions/object/conversions_spec.rb"
|
62
|
-
"spec/unit/mongoid/finders_spec.rb"
|
62
|
+
"spec/unit/mongoid/extensions/object/conversions_spec.rb"
|
63
63
|
]
|
64
64
|
|
65
65
|
if s.respond_to? :specification_version then
|
@@ -67,13 +67,19 @@ Gem::Specification.new do |s|
|
|
67
67
|
s.specification_version = 3
|
68
68
|
|
69
69
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
70
|
+
s.add_runtime_dependency(%q<durran-validatable>, [">= 0"])
|
71
|
+
s.add_runtime_dependency(%q<mislav-will_paginate>, [">= 0"])
|
70
72
|
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
71
73
|
s.add_runtime_dependency(%q<mongodb-mongo>, [">= 0"])
|
72
74
|
else
|
75
|
+
s.add_dependency(%q<durran-validatable>, [">= 0"])
|
76
|
+
s.add_dependency(%q<mislav-will_paginate>, [">= 0"])
|
73
77
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
74
78
|
s.add_dependency(%q<mongodb-mongo>, [">= 0"])
|
75
79
|
end
|
76
80
|
else
|
81
|
+
s.add_dependency(%q<durran-validatable>, [">= 0"])
|
82
|
+
s.add_dependency(%q<mislav-will_paginate>, [">= 0"])
|
77
83
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
78
84
|
s.add_dependency(%q<mongodb-mongo>, [">= 0"])
|
79
85
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -7,7 +7,24 @@ describe Mongoid::Associations::BelongsToAssociation do
|
|
7
7
|
@document = stub(:parent => @parent)
|
8
8
|
end
|
9
9
|
|
10
|
-
describe "#
|
10
|
+
describe "#find" do
|
11
|
+
|
12
|
+
before do
|
13
|
+
@association = Mongoid::Associations::BelongsToAssociation.new(@document)
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when finding by id" do
|
17
|
+
|
18
|
+
it "returns the document in the array with that id" do
|
19
|
+
name = @association.find(Mongo::ObjectID.new)
|
20
|
+
name.should == @parent
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when decorating" do
|
11
28
|
|
12
29
|
before do
|
13
30
|
@association = Mongoid::Associations::BelongsToAssociation.new(@document)
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Associations::Decorator do
|
4
|
+
|
5
|
+
describe "#included" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@person = Person.new
|
9
|
+
@decorated = Decorated.new(@person)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "adds a document reader" do
|
13
|
+
@decorated.should respond_to(:document)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "adds a decorate! instance method" do
|
17
|
+
@decorated.should respond_to(:decorate!)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#decorate!" do
|
23
|
+
|
24
|
+
before do
|
25
|
+
@person = Person.new
|
26
|
+
@decorated = Decorated.new(@person)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "adds all the documents public methods to the class" do
|
30
|
+
@decorated.decorate!
|
31
|
+
@decorated.should respond_to(:title, :terms, :age, :addresses, :name)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
2
|
|
3
|
-
describe Mongoid::Associations::
|
3
|
+
describe Mongoid::Associations::Factory do
|
4
4
|
|
5
5
|
describe "#create" do
|
6
6
|
|
@@ -11,7 +11,7 @@ describe Mongoid::Associations::AssociationFactory do
|
|
11
11
|
context "when type is has_many" do
|
12
12
|
|
13
13
|
it "returns a HasManyAssociationProxy" do
|
14
|
-
association = Mongoid::Associations::
|
14
|
+
association = Mongoid::Associations::Factory.create(:has_many, :addresses, @document)
|
15
15
|
association.should be_a_kind_of(Mongoid::Associations::HasManyAssociation)
|
16
16
|
end
|
17
17
|
|
@@ -20,7 +20,7 @@ describe Mongoid::Associations::AssociationFactory do
|
|
20
20
|
context "when type is has_one" do
|
21
21
|
|
22
22
|
it "returns a HashOneAssociationProxy" do
|
23
|
-
association = Mongoid::Associations::
|
23
|
+
association = Mongoid::Associations::Factory.create(:has_one, :name, @document)
|
24
24
|
association.should be_a_kind_of(Mongoid::Associations::HasOneAssociation)
|
25
25
|
end
|
26
26
|
|
@@ -29,7 +29,7 @@ describe Mongoid::Associations::AssociationFactory do
|
|
29
29
|
context "when type is belongs_to" do
|
30
30
|
|
31
31
|
it "returns a BelongsToAssociationProxy" do
|
32
|
-
association = Mongoid::Associations::
|
32
|
+
association = Mongoid::Associations::Factory.create(:belongs_to, :person, @document)
|
33
33
|
association.should be_a_kind_of(Mongoid::Associations::BelongsToAssociation)
|
34
34
|
end
|
35
35
|
|
@@ -37,12 +37,12 @@ describe Mongoid::Associations::AssociationFactory do
|
|
37
37
|
|
38
38
|
context "when type is invalid" do
|
39
39
|
|
40
|
-
it "
|
41
|
-
lambda { Mongoid::Associations::
|
40
|
+
it "raises an InvalidAssociationError" do
|
41
|
+
lambda { Mongoid::Associations::Factory.create(:something, :person, @document) }.should raise_error
|
42
42
|
end
|
43
43
|
|
44
44
|
end
|
45
45
|
|
46
46
|
end
|
47
47
|
|
48
|
-
end
|
48
|
+
end
|
@@ -3,9 +3,11 @@ require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
|
3
3
|
describe Mongoid::Associations::HasManyAssociation do
|
4
4
|
|
5
5
|
before do
|
6
|
+
@first_id = Mongo::ObjectID.new
|
7
|
+
@second_id = Mongo::ObjectID.new
|
6
8
|
@attributes = { :addresses => [
|
7
|
-
{ :street => "Street 1", :document_class => "Address" },
|
8
|
-
{ :street => "Street 2", :document_class => "Address" } ] }
|
9
|
+
{ :_id => @first_id, :street => "Street 1", :document_class => "Address" },
|
10
|
+
{ :_id => @second_id, :street => "Street 2", :document_class => "Address" } ] }
|
9
11
|
@document = stub(:attributes => @attributes)
|
10
12
|
end
|
11
13
|
|
@@ -68,6 +70,31 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
68
70
|
|
69
71
|
end
|
70
72
|
|
73
|
+
describe "#find" do
|
74
|
+
|
75
|
+
before do
|
76
|
+
@association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
|
77
|
+
end
|
78
|
+
|
79
|
+
context "when finding all" do
|
80
|
+
|
81
|
+
it "returns all the documents" do
|
82
|
+
@association.find(:all).should == @association
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
context "when finding by id" do
|
88
|
+
|
89
|
+
it "returns the document in the array with that id" do
|
90
|
+
address = @association.find(@second_id)
|
91
|
+
address.id.should == @second_id
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
71
98
|
describe "#first" do
|
72
99
|
|
73
100
|
context "when there are elements in the array" do
|
@@ -14,11 +14,6 @@ describe Mongoid::Document do
|
|
14
14
|
|
15
15
|
describe "#belongs_to" do
|
16
16
|
|
17
|
-
it "adds a new Association to the collection" do
|
18
|
-
address = Address.new
|
19
|
-
address.person.should_not be_nil
|
20
|
-
end
|
21
|
-
|
22
17
|
it "creates a reader for the association" do
|
23
18
|
address = Address.new
|
24
19
|
address.should respond_to(:person)
|
@@ -107,6 +102,122 @@ describe Mongoid::Document do
|
|
107
102
|
|
108
103
|
end
|
109
104
|
|
105
|
+
describe "#find" do
|
106
|
+
|
107
|
+
before do
|
108
|
+
@attributes = { :document_class => "Person" }
|
109
|
+
end
|
110
|
+
|
111
|
+
context "when an id is passed in" do
|
112
|
+
|
113
|
+
before do
|
114
|
+
@id = Mongo::ObjectID.new
|
115
|
+
end
|
116
|
+
|
117
|
+
it "delegates to find_first" do
|
118
|
+
@collection.expects(:find_one).with(Mongo::ObjectID.from_string(@id.to_s)).returns(@attributes)
|
119
|
+
Person.find(@id.to_s)
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
context "when finding first" do
|
125
|
+
|
126
|
+
it "delegates to find_first" do
|
127
|
+
@collection.expects(:find_one).with(:test => "Test" ).returns(@attributes)
|
128
|
+
Person.find(:first, :conditions => { :test => "Test" })
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
context "when finding all" do
|
134
|
+
|
135
|
+
before do
|
136
|
+
@cursor = mock
|
137
|
+
@people = []
|
138
|
+
end
|
139
|
+
|
140
|
+
it "delegates to find_all" do
|
141
|
+
@collection.expects(:find).with(:test => "Test").returns(@cursor)
|
142
|
+
@cursor.expects(:collect).returns(@people)
|
143
|
+
Person.find(:all, :conditions => { :test => "Test" })
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "#find_first" do
|
151
|
+
|
152
|
+
before do
|
153
|
+
@attributes = { :document_class => "Person" }
|
154
|
+
end
|
155
|
+
|
156
|
+
context "when a selector is provided" do
|
157
|
+
|
158
|
+
it "finds the first document from the collection and instantiates it" do
|
159
|
+
@collection.expects(:find_one).with(:test => "Test").returns(@attributes)
|
160
|
+
Person.find_first(:test => "Test").attributes.should == @attributes
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
context "when a selector is not provided" do
|
166
|
+
|
167
|
+
it "finds the first document from the collection and instantiates it" do
|
168
|
+
@collection.expects(:find_one).with(nil).returns(@attributes)
|
169
|
+
Person.find_first.attributes.should == @attributes
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
175
|
+
|
176
|
+
describe "#find_all" do
|
177
|
+
|
178
|
+
before do
|
179
|
+
@cursor = mock
|
180
|
+
@people = []
|
181
|
+
end
|
182
|
+
|
183
|
+
context "when a selector is provided" do
|
184
|
+
|
185
|
+
it "finds from the collection and instantiate objects for each returned" do
|
186
|
+
@collection.expects(:find).with(:test => "Test").returns(@cursor)
|
187
|
+
@cursor.expects(:collect).returns(@people)
|
188
|
+
Person.find_all(:test => "Test")
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
context "when a selector is not provided" do
|
194
|
+
|
195
|
+
it "finds from the collection and instantiate objects for each returned" do
|
196
|
+
@collection.expects(:find).with(nil).returns(@cursor)
|
197
|
+
@cursor.expects(:collect).returns(@people)
|
198
|
+
Person.find_all
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
end
|
204
|
+
|
205
|
+
describe "#group_by" do
|
206
|
+
|
207
|
+
before do
|
208
|
+
@reduce = "function(obj, prev) { prev.group.push(obj); }"
|
209
|
+
end
|
210
|
+
|
211
|
+
it "returns documents grouped by the supplied fields" do
|
212
|
+
results = [{ "title" => "Sir", "group" => [{ "title" => "Sir", "age" => 30 }] }]
|
213
|
+
@collection.expects(:group).with([:title], {}, { :group => [] }, @reduce).returns(results)
|
214
|
+
grouped = Person.group_by([:title], {})
|
215
|
+
people = grouped.first["group"]
|
216
|
+
people.first.should be_a_kind_of(Person)
|
217
|
+
end
|
218
|
+
|
219
|
+
end
|
220
|
+
|
110
221
|
describe "#has_many" do
|
111
222
|
|
112
223
|
it "adds a new Association to the collection" do
|
@@ -270,6 +381,32 @@ describe Mongoid::Document do
|
|
270
381
|
|
271
382
|
end
|
272
383
|
|
384
|
+
describe "#paginate" do
|
385
|
+
|
386
|
+
before do
|
387
|
+
@cursor = stub(:count => 100, :collect => [])
|
388
|
+
end
|
389
|
+
|
390
|
+
context "when pagination parameters are passed" do
|
391
|
+
|
392
|
+
it "delegates to will paginate with the results" do
|
393
|
+
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 20}).returns(@cursor)
|
394
|
+
Person.paginate(:conditions => { :test => "Test" }, :page => 2, :per_page => 20)
|
395
|
+
end
|
396
|
+
|
397
|
+
end
|
398
|
+
|
399
|
+
context "when pagination parameters are not passed" do
|
400
|
+
|
401
|
+
it "delegates to will paginate with default values" do
|
402
|
+
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 0}).returns(@cursor)
|
403
|
+
Person.paginate(:conditions => { :test => "Test" })
|
404
|
+
end
|
405
|
+
|
406
|
+
end
|
407
|
+
|
408
|
+
end
|
409
|
+
|
273
410
|
describe "#parent" do
|
274
411
|
|
275
412
|
before do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -9,9 +9,29 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-03 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: durran-validatable
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mislav-will_paginate
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
15
35
|
- !ruby/object:Gem::Dependency
|
16
36
|
name: activesupport
|
17
37
|
type: :runtime
|
@@ -42,33 +62,33 @@ extra_rdoc_files:
|
|
42
62
|
- README.textile
|
43
63
|
files:
|
44
64
|
- .gitignore
|
45
|
-
- History.txt
|
46
65
|
- MIT_LICENSE
|
47
66
|
- README.textile
|
48
67
|
- Rakefile
|
49
68
|
- VERSION
|
50
69
|
- lib/mongoid.rb
|
51
|
-
- lib/mongoid/associations
|
70
|
+
- lib/mongoid/associations.rb
|
52
71
|
- lib/mongoid/associations/belongs_to_association.rb
|
72
|
+
- lib/mongoid/associations/decorator.rb
|
73
|
+
- lib/mongoid/associations/factory.rb
|
53
74
|
- lib/mongoid/associations/has_many_association.rb
|
54
75
|
- lib/mongoid/associations/has_one_association.rb
|
55
76
|
- lib/mongoid/document.rb
|
56
77
|
- lib/mongoid/extensions.rb
|
57
78
|
- lib/mongoid/extensions/array/conversions.rb
|
58
79
|
- lib/mongoid/extensions/object/conversions.rb
|
59
|
-
- lib/mongoid/finders.rb
|
60
80
|
- mongoid.gemspec
|
61
81
|
- spec/integration/mongoid/document_spec.rb
|
62
82
|
- spec/spec.opts
|
63
83
|
- spec/spec_helper.rb
|
64
|
-
- spec/unit/mongoid/associations/association_factory_spec.rb
|
65
84
|
- spec/unit/mongoid/associations/belongs_to_association_spec.rb
|
85
|
+
- spec/unit/mongoid/associations/decorator_spec.rb
|
86
|
+
- spec/unit/mongoid/associations/factory_spec.rb
|
66
87
|
- spec/unit/mongoid/associations/has_many_association_spec.rb
|
67
88
|
- spec/unit/mongoid/associations/has_one_association_spec.rb
|
68
89
|
- spec/unit/mongoid/document_spec.rb
|
69
90
|
- spec/unit/mongoid/extensions/array/conversions_spec.rb
|
70
91
|
- spec/unit/mongoid/extensions/object/conversions_spec.rb
|
71
|
-
- spec/unit/mongoid/finders_spec.rb
|
72
92
|
has_rdoc: true
|
73
93
|
homepage: http://github.com/durran/mongoid
|
74
94
|
licenses: []
|
@@ -100,11 +120,11 @@ summary: Mongoid
|
|
100
120
|
test_files:
|
101
121
|
- spec/integration/mongoid/document_spec.rb
|
102
122
|
- spec/spec_helper.rb
|
103
|
-
- spec/unit/mongoid/associations/association_factory_spec.rb
|
104
123
|
- spec/unit/mongoid/associations/belongs_to_association_spec.rb
|
124
|
+
- spec/unit/mongoid/associations/decorator_spec.rb
|
125
|
+
- spec/unit/mongoid/associations/factory_spec.rb
|
105
126
|
- spec/unit/mongoid/associations/has_many_association_spec.rb
|
106
127
|
- spec/unit/mongoid/associations/has_one_association_spec.rb
|
107
128
|
- spec/unit/mongoid/document_spec.rb
|
108
129
|
- spec/unit/mongoid/extensions/array/conversions_spec.rb
|
109
130
|
- spec/unit/mongoid/extensions/object/conversions_spec.rb
|
110
|
-
- spec/unit/mongoid/finders_spec.rb
|
data/History.txt
DELETED
data/lib/mongoid/finders.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
module Mongoid #:nodoc:
|
2
|
-
module Finders #:nodoc:
|
3
|
-
|
4
|
-
AGGREGATE_REDUCE = "function(obj, prev) { prev.count++; }"
|
5
|
-
GROUP_BY_REDUCE = "function(obj, prev) { prev.group.push(obj); }"
|
6
|
-
|
7
|
-
# Get an aggregate count for the supplied group of fields and the
|
8
|
-
# selector that is provided.
|
9
|
-
def aggregate(fields, selector)
|
10
|
-
collection.group(fields, selector, { :count => 0 }, AGGREGATE_REDUCE)
|
11
|
-
end
|
12
|
-
|
13
|
-
# Find all Documents in several ways.
|
14
|
-
# Model.find(:first, :attribute => "value")
|
15
|
-
# Model.find(:all, :attribute => "value")
|
16
|
-
def find(*args)
|
17
|
-
type, selector = args[0], args[1]
|
18
|
-
conditions = selector[:conditions] if selector
|
19
|
-
case type
|
20
|
-
when :all then find_all(conditions)
|
21
|
-
when :first then find_first(conditions)
|
22
|
-
else find_first(Mongo::ObjectID.from_string(type.to_s))
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# Find a single Document given the passed selector, which is a Hash of attributes that
|
27
|
-
# must match the Document in the database exactly.
|
28
|
-
def find_first(selector = nil)
|
29
|
-
new(collection.find_one(selector))
|
30
|
-
end
|
31
|
-
|
32
|
-
# Find all Documents given the passed selector, which is a Hash of attributes that
|
33
|
-
# must match the Document in the database exactly.
|
34
|
-
def find_all(selector = nil)
|
35
|
-
collection.find(selector).collect { |doc| new(doc) }
|
36
|
-
end
|
37
|
-
|
38
|
-
# Find all Documents given the supplied criteria, grouped by the fields
|
39
|
-
# provided.
|
40
|
-
def group_by(fields, selector)
|
41
|
-
collection.group(fields, selector, { :group => [] }, GROUP_BY_REDUCE).collect do |docs|
|
42
|
-
docs["group"] = docs["group"].collect { |attrs| new(attrs) }; docs
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# Find all documents in paginated fashion given the supplied arguments.
|
47
|
-
# If no parameters are passed just default to offset 0 and limit 20.
|
48
|
-
def paginate(params = {})
|
49
|
-
WillPaginate::Collection.create(
|
50
|
-
params[:page] || 1,
|
51
|
-
params[:per_page] || 20,
|
52
|
-
0) do |pager|
|
53
|
-
results = collection.find(params[:conditions], { :limit => pager.per_page, :offset => pager.offset })
|
54
|
-
pager.total_entries = results.count
|
55
|
-
pager.replace(results.collect { |doc| new(doc) })
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
@@ -1,165 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
-
|
3
|
-
describe Mongoid::Finders do
|
4
|
-
|
5
|
-
before do
|
6
|
-
@collection = mock
|
7
|
-
@database = stub(:collection => @collection)
|
8
|
-
Mongoid.stubs(:database).returns(@database)
|
9
|
-
end
|
10
|
-
|
11
|
-
after do
|
12
|
-
Person.instance_variable_set(:@collection, nil)
|
13
|
-
end
|
14
|
-
|
15
|
-
describe "#find" do
|
16
|
-
|
17
|
-
before do
|
18
|
-
@attributes = { :document_class => "Person" }
|
19
|
-
end
|
20
|
-
|
21
|
-
context "when an id is passed in" do
|
22
|
-
|
23
|
-
before do
|
24
|
-
@id = Mongo::ObjectID.new
|
25
|
-
end
|
26
|
-
|
27
|
-
it "delegates to find_first" do
|
28
|
-
@collection.expects(:find_one).with(Mongo::ObjectID.from_string(@id.to_s)).returns(@attributes)
|
29
|
-
Person.find(@id.to_s)
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
context "when finding first" do
|
35
|
-
|
36
|
-
it "delegates to find_first" do
|
37
|
-
@collection.expects(:find_one).with(:test => "Test" ).returns(@attributes)
|
38
|
-
Person.find(:first, :conditions => { :test => "Test" })
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
context "when finding all" do
|
44
|
-
|
45
|
-
before do
|
46
|
-
@cursor = mock
|
47
|
-
@people = []
|
48
|
-
end
|
49
|
-
|
50
|
-
it "delegates to find_all" do
|
51
|
-
@collection.expects(:find).with(:test => "Test").returns(@cursor)
|
52
|
-
@cursor.expects(:collect).returns(@people)
|
53
|
-
Person.find(:all, :conditions => { :test => "Test" })
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
describe "#find_first" do
|
61
|
-
|
62
|
-
before do
|
63
|
-
@attributes = { :document_class => "Person" }
|
64
|
-
end
|
65
|
-
|
66
|
-
context "when a selector is provided" do
|
67
|
-
|
68
|
-
it "finds the first document from the collection and instantiates it" do
|
69
|
-
@collection.expects(:find_one).with(:test => "Test").returns(@attributes)
|
70
|
-
Person.find_first(:test => "Test").attributes.should == @attributes
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
context "when a selector is not provided" do
|
76
|
-
|
77
|
-
it "finds the first document from the collection and instantiates it" do
|
78
|
-
@collection.expects(:find_one).with(nil).returns(@attributes)
|
79
|
-
Person.find_first.attributes.should == @attributes
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
describe "#find_all" do
|
87
|
-
|
88
|
-
before do
|
89
|
-
@cursor = mock
|
90
|
-
@people = []
|
91
|
-
end
|
92
|
-
|
93
|
-
context "when a selector is provided" do
|
94
|
-
|
95
|
-
it "finds from the collection and instantiate objects for each returned" do
|
96
|
-
@collection.expects(:find).with(:test => "Test").returns(@cursor)
|
97
|
-
@cursor.expects(:collect).returns(@people)
|
98
|
-
Person.find_all(:test => "Test")
|
99
|
-
end
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
context "when a selector is not provided" do
|
104
|
-
|
105
|
-
it "finds from the collection and instantiate objects for each returned" do
|
106
|
-
@collection.expects(:find).with(nil).returns(@cursor)
|
107
|
-
@cursor.expects(:collect).returns(@people)
|
108
|
-
Person.find_all
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
114
|
-
|
115
|
-
describe "#group_by" do
|
116
|
-
|
117
|
-
before do
|
118
|
-
@reduce = "function(obj, prev) { prev.group.push(obj); }"
|
119
|
-
end
|
120
|
-
|
121
|
-
it "returns documents grouped by the supplied fields" do
|
122
|
-
results = [{ "title" => "Sir", "group" => [{ "title" => "Sir", "age" => 30 }] }]
|
123
|
-
@collection.expects(:group).with([:title], {}, { :group => [] }, @reduce).returns(results)
|
124
|
-
grouped = Person.group_by([:title], {})
|
125
|
-
people = grouped.first["group"]
|
126
|
-
people.first.should be_a_kind_of(Person)
|
127
|
-
end
|
128
|
-
|
129
|
-
end
|
130
|
-
|
131
|
-
describe "#paginate" do
|
132
|
-
|
133
|
-
before do
|
134
|
-
@cursor = stub(:count => 100, :collect => [])
|
135
|
-
end
|
136
|
-
|
137
|
-
context "when pagination parameters are passed" do
|
138
|
-
|
139
|
-
it "delegates to will paginate with the results" do
|
140
|
-
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 20}).returns(@cursor)
|
141
|
-
Person.paginate(:conditions => { :test => "Test" }, :page => 2, :per_page => 20)
|
142
|
-
end
|
143
|
-
|
144
|
-
end
|
145
|
-
|
146
|
-
context "when pagination parameters are not passed" do
|
147
|
-
|
148
|
-
it "delegates to will paginate with default values" do
|
149
|
-
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 0}).returns(@cursor)
|
150
|
-
Person.paginate(:conditions => { :test => "Test" })
|
151
|
-
end
|
152
|
-
|
153
|
-
end
|
154
|
-
|
155
|
-
context "when param keys are strings" do
|
156
|
-
|
157
|
-
it "converts the keys to symbols" do
|
158
|
-
|
159
|
-
end
|
160
|
-
|
161
|
-
end
|
162
|
-
|
163
|
-
end
|
164
|
-
|
165
|
-
end
|