mongoid 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/History.txt +5 -0
- data/VERSION +1 -1
- data/lib/mongoid.rb +4 -4
- data/lib/mongoid/document.rb +11 -62
- data/lib/mongoid/finders.rb +60 -0
- data/mongoid.gemspec +4 -4
- data/spec/integration/mongoid/document_spec.rb +1 -1
- data/spec/unit/mongoid/document_spec.rb +9 -143
- data/spec/unit/mongoid/finders_spec.rb +157 -0
- metadata +4 -4
- data/lib/mongoid/paginator.rb +0 -22
- data/spec/unit/mongoid/paginator_spec.rb +0 -74
data/.gitignore
CHANGED
data/History.txt
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.6
|
data/lib/mongoid.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# Copyright (c) 2009 Durran Jordan
|
2
|
-
#
|
2
|
+
#
|
3
3
|
# Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
# a copy of this software and associated documentation files (the
|
5
5
|
# "Software"), to deal in the Software without restriction, including
|
@@ -7,10 +7,10 @@
|
|
7
7
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
8
8
|
# permit persons to whom the Software is furnished to do so, subject to
|
9
9
|
# the following conditions:
|
10
|
-
#
|
10
|
+
#
|
11
11
|
# The above copyright notice and this permission notice shall be
|
12
12
|
# included in all copies or substantial portions of the Software.
|
13
|
-
#
|
13
|
+
#
|
14
14
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
15
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
16
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
@@ -38,8 +38,8 @@ require "mongoid/associations/has_one_association"
|
|
38
38
|
require "mongoid/extensions/array/conversions"
|
39
39
|
require "mongoid/extensions/object/conversions"
|
40
40
|
require "mongoid/extensions"
|
41
|
+
require "mongoid/finders"
|
41
42
|
require "mongoid/document"
|
42
|
-
require "mongoid/paginator"
|
43
43
|
|
44
44
|
module Mongoid
|
45
45
|
|
data/lib/mongoid/document.rb
CHANGED
@@ -2,9 +2,7 @@ module Mongoid #:nodoc:
|
|
2
2
|
class Document #:nodoc:
|
3
3
|
include ActiveSupport::Callbacks
|
4
4
|
include Validatable
|
5
|
-
|
6
|
-
AGGREGATE_REDUCE = "function(obj, prev) { prev.count++; }"
|
7
|
-
GROUP_BY_REDUCE = "function(obj, prev) { prev.group.push(obj); }"
|
5
|
+
extend Finders
|
8
6
|
|
9
7
|
attr_reader :attributes, :parent
|
10
8
|
|
@@ -16,12 +14,6 @@ module Mongoid #:nodoc:
|
|
16
14
|
|
17
15
|
class << self
|
18
16
|
|
19
|
-
# Get an aggregate count for the supplied group of fields and the
|
20
|
-
# selector that is provided.
|
21
|
-
def aggregate(fields, selector)
|
22
|
-
collection.group(fields, selector, { :count => 0 }, AGGREGATE_REDUCE)
|
23
|
-
end
|
24
|
-
|
25
17
|
# Create an association to a parent Document.
|
26
18
|
def belongs_to(association_name)
|
27
19
|
add_association(:belongs_to, association_name.to_s.classify, association_name)
|
@@ -35,7 +27,13 @@ module Mongoid #:nodoc:
|
|
35
27
|
|
36
28
|
# Create a new Document with the supplied attribtues, and insert it into the database.
|
37
29
|
def create(attributes = {})
|
38
|
-
new(attributes).save
|
30
|
+
new(attributes).save(true)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Deletes all records from the collection. Will actually call drop on the
|
34
|
+
# Mongo::Collection for efficiency.
|
35
|
+
def destroy_all
|
36
|
+
collection.drop
|
39
37
|
end
|
40
38
|
|
41
39
|
# Defines all the fields that are accessable on the Document
|
@@ -50,38 +48,6 @@ module Mongoid #:nodoc:
|
|
50
48
|
end
|
51
49
|
end
|
52
50
|
|
53
|
-
# Find all Documents in several ways.
|
54
|
-
# Model.find(:first, :attribute => "value")
|
55
|
-
# Model.find(:all, :attribute => "value")
|
56
|
-
def find(*args)
|
57
|
-
type, selector = args[0], args[1]
|
58
|
-
case type
|
59
|
-
when :all then find_all(selector[:conditions])
|
60
|
-
when :first then find_first(selector[:conditions])
|
61
|
-
else find_first(Mongo::ObjectID.from_string(type.to_s))
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
# Find a single Document given the passed selector, which is a Hash of attributes that
|
66
|
-
# must match the Document in the database exactly.
|
67
|
-
def find_first(selector = nil)
|
68
|
-
new(collection.find_one(selector))
|
69
|
-
end
|
70
|
-
|
71
|
-
# Find all Documents given the passed selector, which is a Hash of attributes that
|
72
|
-
# must match the Document in the database exactly.
|
73
|
-
def find_all(selector = nil)
|
74
|
-
collection.find(selector).collect { |doc| new(doc) }
|
75
|
-
end
|
76
|
-
|
77
|
-
# Find all Documents given the supplied criteria, grouped by the fields
|
78
|
-
# provided.
|
79
|
-
def group_by(fields, selector)
|
80
|
-
collection.group(fields, selector, { :group => [] }, GROUP_BY_REDUCE).collect do |docs|
|
81
|
-
group!(docs)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
51
|
# Create a one-to-many association between Documents.
|
86
52
|
def has_many(association_name)
|
87
53
|
add_association(:has_many, association_name.to_s.classify, association_name)
|
@@ -98,19 +64,6 @@ module Mongoid #:nodoc:
|
|
98
64
|
collection.create_index(name, options)
|
99
65
|
end
|
100
66
|
|
101
|
-
# Find all documents in paginated fashion given the supplied arguments.
|
102
|
-
# If no parameters are passed just default to offset 0 and limit 20.
|
103
|
-
def paginate(selector = {}, params = {})
|
104
|
-
WillPaginate::Collection.create(
|
105
|
-
params[:page] || 1,
|
106
|
-
params[:per_page] || 20,
|
107
|
-
0) do |pager|
|
108
|
-
results = collection.find(selector[:conditions], { :limit => pager.per_page, :offset => pager.offset })
|
109
|
-
pager.total_entries = results.count
|
110
|
-
pager.replace(results.collect { |doc| new(doc) })
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
67
|
end
|
115
68
|
|
116
69
|
# Get the Mongo::Collection associated with this Document.
|
@@ -150,12 +103,14 @@ module Mongoid #:nodoc:
|
|
150
103
|
# in the object graph, it will save itself, and return self. If the
|
151
104
|
# document is embedded within another document, or is multiple levels down
|
152
105
|
# the tree, the root object will get saved, and return itself.
|
153
|
-
def save
|
106
|
+
def save(creating = false)
|
154
107
|
if @parent
|
155
108
|
@parent.save
|
156
109
|
else
|
110
|
+
run_callbacks(:before_create) if creating
|
157
111
|
run_callbacks(:before_save)
|
158
112
|
collection.save(@attributes)
|
113
|
+
run_callbacks(:after_create) if creating
|
159
114
|
run_callbacks(:after_save)
|
160
115
|
return self
|
161
116
|
end
|
@@ -186,12 +141,6 @@ module Mongoid #:nodoc:
|
|
186
141
|
end
|
187
142
|
end
|
188
143
|
|
189
|
-
# Takes the supplied raw grouping of documents and alters it to a
|
190
|
-
# grouping of actual document objects.
|
191
|
-
def group!(docs)
|
192
|
-
docs["group"] = docs["group"].collect { |attrs| new(attrs) }; docs
|
193
|
-
end
|
194
|
-
|
195
144
|
end
|
196
145
|
|
197
146
|
# Read from the attributes hash.
|
@@ -0,0 +1,60 @@
|
|
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
|
data/mongoid.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.6"
|
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"]
|
@@ -30,7 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
"lib/mongoid/extensions.rb",
|
31
31
|
"lib/mongoid/extensions/array/conversions.rb",
|
32
32
|
"lib/mongoid/extensions/object/conversions.rb",
|
33
|
-
"lib/mongoid/
|
33
|
+
"lib/mongoid/finders.rb",
|
34
34
|
"mongoid.gemspec",
|
35
35
|
"spec/integration/mongoid/document_spec.rb",
|
36
36
|
"spec/spec.opts",
|
@@ -42,7 +42,7 @@ Gem::Specification.new do |s|
|
|
42
42
|
"spec/unit/mongoid/document_spec.rb",
|
43
43
|
"spec/unit/mongoid/extensions/array/conversions_spec.rb",
|
44
44
|
"spec/unit/mongoid/extensions/object/conversions_spec.rb",
|
45
|
-
"spec/unit/mongoid/
|
45
|
+
"spec/unit/mongoid/finders_spec.rb"
|
46
46
|
]
|
47
47
|
s.homepage = %q{http://github.com/durran/mongoid}
|
48
48
|
s.rdoc_options = ["--charset=UTF-8"]
|
@@ -59,7 +59,7 @@ Gem::Specification.new do |s|
|
|
59
59
|
"spec/unit/mongoid/document_spec.rb",
|
60
60
|
"spec/unit/mongoid/extensions/array/conversions_spec.rb",
|
61
61
|
"spec/unit/mongoid/extensions/object/conversions_spec.rb",
|
62
|
-
"spec/unit/mongoid/
|
62
|
+
"spec/unit/mongoid/finders_spec.rb"
|
63
63
|
]
|
64
64
|
|
65
65
|
if s.respond_to? :specification_version then
|
@@ -79,6 +79,15 @@ describe Mongoid::Document do
|
|
79
79
|
|
80
80
|
end
|
81
81
|
|
82
|
+
describe "#destroy_all" do
|
83
|
+
|
84
|
+
it "deletes all documents from the collection" do
|
85
|
+
@collection.expects(:drop)
|
86
|
+
Person.destroy_all
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
82
91
|
describe "#fields" do
|
83
92
|
|
84
93
|
before do
|
@@ -98,122 +107,6 @@ describe Mongoid::Document do
|
|
98
107
|
|
99
108
|
end
|
100
109
|
|
101
|
-
describe "#find" do
|
102
|
-
|
103
|
-
before do
|
104
|
-
@attributes = { :document_class => "Person" }
|
105
|
-
end
|
106
|
-
|
107
|
-
context "when an id is passed in" do
|
108
|
-
|
109
|
-
before do
|
110
|
-
@id = Mongo::ObjectID.new
|
111
|
-
end
|
112
|
-
|
113
|
-
it "delegates to find_first" do
|
114
|
-
@collection.expects(:find_one).with(Mongo::ObjectID.from_string(@id.to_s)).returns(@attributes)
|
115
|
-
Person.find(@id.to_s)
|
116
|
-
end
|
117
|
-
|
118
|
-
end
|
119
|
-
|
120
|
-
context "when finding first" do
|
121
|
-
|
122
|
-
it "delegates to find_first" do
|
123
|
-
@collection.expects(:find_one).with(:test => "Test" ).returns(@attributes)
|
124
|
-
Person.find(:first, :conditions => { :test => "Test" })
|
125
|
-
end
|
126
|
-
|
127
|
-
end
|
128
|
-
|
129
|
-
context "when finding all" do
|
130
|
-
|
131
|
-
before do
|
132
|
-
@cursor = mock
|
133
|
-
@people = []
|
134
|
-
end
|
135
|
-
|
136
|
-
it "delegates to find_all" do
|
137
|
-
@collection.expects(:find).with(:test => "Test").returns(@cursor)
|
138
|
-
@cursor.expects(:collect).returns(@people)
|
139
|
-
Person.find(:all, :conditions => { :test => "Test" })
|
140
|
-
end
|
141
|
-
|
142
|
-
end
|
143
|
-
|
144
|
-
end
|
145
|
-
|
146
|
-
describe "#find_first" do
|
147
|
-
|
148
|
-
before do
|
149
|
-
@attributes = { :document_class => "Person" }
|
150
|
-
end
|
151
|
-
|
152
|
-
context "when a selector is provided" do
|
153
|
-
|
154
|
-
it "finds the first document from the collection and instantiates it" do
|
155
|
-
@collection.expects(:find_one).with(:test => "Test").returns(@attributes)
|
156
|
-
Person.find_first(:test => "Test").attributes.should == @attributes
|
157
|
-
end
|
158
|
-
|
159
|
-
end
|
160
|
-
|
161
|
-
context "when a selector is not provided" do
|
162
|
-
|
163
|
-
it "finds the first document from the collection and instantiates it" do
|
164
|
-
@collection.expects(:find_one).with(nil).returns(@attributes)
|
165
|
-
Person.find_first.attributes.should == @attributes
|
166
|
-
end
|
167
|
-
|
168
|
-
end
|
169
|
-
|
170
|
-
end
|
171
|
-
|
172
|
-
describe "#find_all" do
|
173
|
-
|
174
|
-
before do
|
175
|
-
@cursor = mock
|
176
|
-
@people = []
|
177
|
-
end
|
178
|
-
|
179
|
-
context "when a selector is provided" do
|
180
|
-
|
181
|
-
it "finds from the collection and instantiate objects for each returned" do
|
182
|
-
@collection.expects(:find).with(:test => "Test").returns(@cursor)
|
183
|
-
@cursor.expects(:collect).returns(@people)
|
184
|
-
Person.find_all(:test => "Test")
|
185
|
-
end
|
186
|
-
|
187
|
-
end
|
188
|
-
|
189
|
-
context "when a selector is not provided" do
|
190
|
-
|
191
|
-
it "finds from the collection and instantiate objects for each returned" do
|
192
|
-
@collection.expects(:find).with(nil).returns(@cursor)
|
193
|
-
@cursor.expects(:collect).returns(@people)
|
194
|
-
Person.find_all
|
195
|
-
end
|
196
|
-
|
197
|
-
end
|
198
|
-
|
199
|
-
end
|
200
|
-
|
201
|
-
describe "#group_by" do
|
202
|
-
|
203
|
-
before do
|
204
|
-
@reduce = "function(obj, prev) { prev.group.push(obj); }"
|
205
|
-
end
|
206
|
-
|
207
|
-
it "returns documents grouped by the supplied fields" do
|
208
|
-
results = [{ "title" => "Sir", "group" => [{ "title" => "Sir", "age" => 30 }] }]
|
209
|
-
@collection.expects(:group).with([:title], {}, { :group => [] }, @reduce).returns(results)
|
210
|
-
grouped = Person.group_by([:title], {})
|
211
|
-
people = grouped.first["group"]
|
212
|
-
people.first.should be_a_kind_of(Person)
|
213
|
-
end
|
214
|
-
|
215
|
-
end
|
216
|
-
|
217
110
|
describe "#has_many" do
|
218
111
|
|
219
112
|
it "adds a new Association to the collection" do
|
@@ -362,32 +255,6 @@ describe Mongoid::Document do
|
|
362
255
|
|
363
256
|
end
|
364
257
|
|
365
|
-
describe "#paginate" do
|
366
|
-
|
367
|
-
before do
|
368
|
-
@cursor = stub(:count => 100, :collect => [])
|
369
|
-
end
|
370
|
-
|
371
|
-
context "when pagination parameters are passed" do
|
372
|
-
|
373
|
-
it "delegates to will paginate with the results" do
|
374
|
-
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 20}).returns(@cursor)
|
375
|
-
Person.paginate({ :conditions => { :test => "Test" } }, { :page => 2, :per_page => 20 })
|
376
|
-
end
|
377
|
-
|
378
|
-
end
|
379
|
-
|
380
|
-
context "when pagination parameters are not passed" do
|
381
|
-
|
382
|
-
it "delegates to will paginate with default values" do
|
383
|
-
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 0}).returns(@cursor)
|
384
|
-
Person.paginate(:conditions => { :test => "Test" })
|
385
|
-
end
|
386
|
-
|
387
|
-
end
|
388
|
-
|
389
|
-
end
|
390
|
-
|
391
258
|
describe "#parent" do
|
392
259
|
|
393
260
|
before do
|
@@ -676,5 +543,4 @@ describe Mongoid::Document do
|
|
676
543
|
|
677
544
|
end
|
678
545
|
|
679
|
-
|
680
546
|
end
|
@@ -0,0 +1,157 @@
|
|
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
|
+
end
|
156
|
+
|
157
|
+
end
|
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.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -56,7 +56,7 @@ files:
|
|
56
56
|
- lib/mongoid/extensions.rb
|
57
57
|
- lib/mongoid/extensions/array/conversions.rb
|
58
58
|
- lib/mongoid/extensions/object/conversions.rb
|
59
|
-
- lib/mongoid/
|
59
|
+
- lib/mongoid/finders.rb
|
60
60
|
- mongoid.gemspec
|
61
61
|
- spec/integration/mongoid/document_spec.rb
|
62
62
|
- spec/spec.opts
|
@@ -68,7 +68,7 @@ files:
|
|
68
68
|
- spec/unit/mongoid/document_spec.rb
|
69
69
|
- spec/unit/mongoid/extensions/array/conversions_spec.rb
|
70
70
|
- spec/unit/mongoid/extensions/object/conversions_spec.rb
|
71
|
-
- spec/unit/mongoid/
|
71
|
+
- spec/unit/mongoid/finders_spec.rb
|
72
72
|
has_rdoc: true
|
73
73
|
homepage: http://github.com/durran/mongoid
|
74
74
|
licenses: []
|
@@ -107,4 +107,4 @@ test_files:
|
|
107
107
|
- spec/unit/mongoid/document_spec.rb
|
108
108
|
- spec/unit/mongoid/extensions/array/conversions_spec.rb
|
109
109
|
- spec/unit/mongoid/extensions/object/conversions_spec.rb
|
110
|
-
- spec/unit/mongoid/
|
110
|
+
- spec/unit/mongoid/finders_spec.rb
|
data/lib/mongoid/paginator.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
module Mongoid #:nodoc:
|
2
|
-
class Paginator #:nodoc:
|
3
|
-
|
4
|
-
attr_reader :limit, :offset
|
5
|
-
|
6
|
-
# Create the new Paginator with the supplied options.
|
7
|
-
# * Will default to offset 0 if no page defined in the options.
|
8
|
-
# * Will default to limit 20 if no per_page defined in the options.
|
9
|
-
def initialize(options = {})
|
10
|
-
page = options[:page]
|
11
|
-
@limit = options[:per_page] || 20
|
12
|
-
@offset = page ? (page - 1) * @limit : 0
|
13
|
-
end
|
14
|
-
|
15
|
-
# Generate the options needed for returning the correct
|
16
|
-
# results given the supplied parameters
|
17
|
-
def options
|
18
|
-
{ :limit => @limit, :offset => @offset }
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
@@ -1,74 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
-
|
3
|
-
describe Mongoid::Paginator do
|
4
|
-
|
5
|
-
describe "#limit" do
|
6
|
-
|
7
|
-
context "when per_page is defined" do
|
8
|
-
|
9
|
-
before do
|
10
|
-
@options = { :per_page => 50 }
|
11
|
-
@paginator = Mongoid::Paginator.new(@options)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "returns the per_page value" do
|
15
|
-
@paginator.limit.should == 50
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
context "when per_page is not defined" do
|
21
|
-
|
22
|
-
before do
|
23
|
-
@options = {}
|
24
|
-
@paginator = Mongoid::Paginator.new(@options)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "returns the default of 20" do
|
28
|
-
@paginator.limit.should == 20
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
describe "#offset" do
|
36
|
-
|
37
|
-
context "when page is defined" do
|
38
|
-
|
39
|
-
before do
|
40
|
-
@options = { :page => 11 }
|
41
|
-
@paginator = Mongoid::Paginator.new(@options)
|
42
|
-
end
|
43
|
-
|
44
|
-
it "returns the page value - 1 * limit" do
|
45
|
-
@paginator.offset.should == 200
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
context "when page is not defined" do
|
51
|
-
|
52
|
-
before do
|
53
|
-
@options = {}
|
54
|
-
@paginator = Mongoid::Paginator.new(@options)
|
55
|
-
end
|
56
|
-
|
57
|
-
it "returns the default of 0" do
|
58
|
-
@paginator.offset.should == 0
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
describe "#options" do
|
66
|
-
|
67
|
-
it "returns a hash of the limit and offset" do
|
68
|
-
@paginator = Mongoid::Paginator.new
|
69
|
-
@paginator.options.should == { :limit => 20, :offset => 0 }
|
70
|
-
end
|
71
|
-
|
72
|
-
end
|
73
|
-
|
74
|
-
end
|