mongoid 0.8.2 → 0.8.3

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.2
1
+ 0.8.3
data/lib/mongoid.rb CHANGED
@@ -24,7 +24,7 @@ gem "activesupport", "2.3.4"
24
24
  gem "mongo", "0.17"
25
25
  gem "mongo_ext", "0.17"
26
26
  gem "durran-validatable", "1.8.2"
27
- gem "will_paginate", "2.3.11"
27
+ gem "leshill-will_paginate", "2.3.11"
28
28
 
29
29
  require "delegate"
30
30
  require "observer"
@@ -42,6 +42,7 @@ require "mongoid/commands"
42
42
  require "mongoid/criteria"
43
43
  require "mongoid/extensions"
44
44
  require "mongoid/field"
45
+ require "mongoid/finders"
45
46
  require "mongoid/timestamps"
46
47
  require "mongoid/versioning"
47
48
  require "mongoid/document"
@@ -59,6 +60,18 @@ module Mongoid
59
60
  # Raised when a persisence method ending in ! fails validation.
60
61
  class ValidationsError < RuntimeError; end
61
62
 
63
+ # A common case of errors is to instantiate a child document without a
64
+ # reference to a parent, which will result in trying to save on a nil
65
+ # collection. This error is raised to help debug the issue.
66
+ class MissingParentError < RuntimeError
67
+ def initialize(doc)
68
+ @document = doc
69
+ end
70
+ def message
71
+ "Attempted to save embedded document #{@document.class.name}, but there was no associated parent"
72
+ end
73
+ end
74
+
62
75
  # Connect to the database name supplied. This should be run
63
76
  # for initial setup, potentially in a rails initializer.
64
77
  def self.connect_to(name)
@@ -1,7 +1,9 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Attributes #:nodoc:
3
3
  # Process the provided attributes casting them to their proper values if a
4
- # field exists for them on the +Document+.
4
+ # field exists for them on the +Document+. This will be limited to only the
5
+ # attributes provided in the suppied +Hash+ so that no extra nil values get
6
+ # put into the document's attributes.
5
7
  def process(attrs = {})
6
8
  attrs.each_pair do |key, value|
7
9
  send("#{key}=", value)
@@ -13,7 +13,12 @@ module Mongoid #:nodoc:
13
13
  return false unless Validate.execute(doc)
14
14
  doc.run_callbacks :before_save
15
15
  parent = doc.parent
16
- parent ? Save.execute(parent) : doc.collection.save(doc.attributes)
16
+ if parent
17
+ Save.execute(parent)
18
+ else
19
+ collection = doc.collection
20
+ collection ? collection.save(doc.attributes) : raise(MissingParentError.new(doc))
21
+ end
17
22
  doc.run_callbacks :after_save
18
23
  return doc
19
24
  end
@@ -270,6 +270,22 @@ module Mongoid #:nodoc:
270
270
  end
271
271
  end
272
272
 
273
+ # Executes the +Criteria+ and paginates the results.
274
+ #
275
+ # Options:
276
+ #
277
+ # klass: Optional class name to execute the criteria on.
278
+ #
279
+ # Example:
280
+ #
281
+ # <tt>criteria.paginate(Person)</tt>
282
+ def paginate(klass = nil)
283
+ results = execute(klass)
284
+ WillPaginate::Collection.create(page, per_page, count) do |pager|
285
+ pager.replace(results)
286
+ end
287
+ end
288
+
273
289
  # Returns the number of results per page or the default of 20.
274
290
  def per_page
275
291
  (@options[:limit] || 20).to_i
@@ -2,6 +2,7 @@ module Mongoid #:nodoc:
2
2
  class Document
3
3
  include ActiveSupport::Callbacks
4
4
  include Associations, Attributes, Commands, Observable, Validatable
5
+ extend Finders
5
6
 
6
7
  attr_accessor :association_name, :parent
7
8
  attr_reader :attributes, :new_record
@@ -20,17 +21,6 @@ module Mongoid #:nodoc:
20
21
 
21
22
  class << self
22
23
 
23
- # Find +Documents+ given the conditions.
24
- #
25
- # Options:
26
- #
27
- # args: A +Hash+ with a conditions key and other options
28
- #
29
- # <tt>Person.all(:conditions => { :attribute => "value" })</tt>
30
- def all(*args)
31
- find(:all, *args)
32
- end
33
-
34
24
  # Returns the collection associated with this +Document+. If the
35
25
  # document is embedded, there will be no collection associated
36
26
  # with it.
@@ -42,14 +32,6 @@ module Mongoid #:nodoc:
42
32
  @collection ||= Mongoid.database.collection(@collection_name)
43
33
  end
44
34
 
45
- # Returns a count of matching records in the database based on the
46
- # provided arguments.
47
- #
48
- # <tt>Person.count(:first, :conditions => { :attribute => "value" })</tt>
49
- def count(*args)
50
- Criteria.translate(*args).count(self)
51
- end
52
-
53
35
  # Returns a hash of all the default values
54
36
  def defaults
55
37
  @defaults
@@ -86,39 +68,6 @@ module Mongoid #:nodoc:
86
68
  @fields
87
69
  end
88
70
 
89
- # Find a +Document+ in several different ways.
90
- #
91
- # If a +String+ is provided, it will be assumed that it is a
92
- # representation of a Mongo::ObjectID and will attempt to find a single
93
- # +Document+ based on that id. If a +Symbol+ and +Hash+ is provided then
94
- # it will attempt to find either a single +Document+ or multiples based
95
- # on the conditions provided and the first parameter.
96
- #
97
- # <tt>Person.find(:first, :conditions => { :attribute => "value" })</tt>
98
- #
99
- # <tt>Person.find(:all, :conditions => { :attribute => "value" })</tt>
100
- #
101
- # <tt>Person.find(Mongo::ObjectID.new.to_s)</tt>
102
- def find(*args)
103
- Criteria.translate(*args).execute(self)
104
- end
105
-
106
- # Find a +Document+ by its id.
107
- def find_by_id(id)
108
- find(id)
109
- end
110
-
111
- # Find the first +Document+ given the conditions.
112
- #
113
- # Options:
114
- #
115
- # args: A +Hash+ with a conditions key and other options
116
- #
117
- # <tt>Person.first(:conditions => { :attribute => "value" })</tt>
118
- def first(*args)
119
- find(:first, *args)
120
- end
121
-
122
71
  # Adds an index on the field specified. Options can be :unique => true or
123
72
  # :unique => false. It will default to the latter.
124
73
  def index(name, options = { :unique => false })
@@ -157,44 +106,6 @@ module Mongoid #:nodoc:
157
106
  @primary_key
158
107
  end
159
108
 
160
- # Find all documents in paginated fashion given the supplied arguments.
161
- # If no parameters are passed just default to offset 0 and limit 20.
162
- #
163
- # Options:
164
- #
165
- # params: A +Hash+ of params to pass to the Criteria API.
166
- #
167
- # Example:
168
- #
169
- # <tt>Person.paginate(:conditions => { :field => "Test" }, :page => 1,
170
- # :per_page => 20)</tt>
171
- #
172
- # Returns paginated array of docs.
173
- def paginate(params = {})
174
- criteria = Criteria.translate(:all, params)
175
- results = criteria.execute(self)
176
- WillPaginate::Collection.create(criteria.page, criteria.per_page, criteria.count) do |pager|
177
- pager.replace(results)
178
- end
179
- end
180
-
181
- # Entry point for creating a new criteria from a Document. This will
182
- # instantiate a new +Criteria+ object with the supplied select criterion
183
- # already added to it.
184
- #
185
- # Options:
186
- #
187
- # args: A list of field names to retrict the returned fields to.
188
- #
189
- # Example:
190
- #
191
- # <tt>Person.select(:field1, :field2, :field3)</tt>
192
- #
193
- # Returns: <tt>Criteria</tt>
194
- def select(*args)
195
- Criteria.new(:all, self).select(*args)
196
- end
197
-
198
109
  end
199
110
 
200
111
  # Performs equality checking on the attributes.
@@ -2,6 +2,7 @@ require "mongoid/extensions/array/conversions"
2
2
  require "mongoid/extensions/array/parentization"
3
3
  require "mongoid/extensions/boolean/conversions"
4
4
  require "mongoid/extensions/date/conversions"
5
+ require "mongoid/extensions/datetime/conversions"
5
6
  require "mongoid/extensions/float/conversions"
6
7
  require "mongoid/extensions/hash/accessors"
7
8
  require "mongoid/extensions/hash/conversions"
@@ -25,6 +26,10 @@ class Date #:nodoc
25
26
  extend Mongoid::Extensions::Date::Conversions
26
27
  end
27
28
 
29
+ class DateTime #:nodoc
30
+ extend Mongoid::Extensions::DateTime::Conversions
31
+ end
32
+
28
33
  class Float #:nodoc
29
34
  extend Mongoid::Extensions::Float::Conversions
30
35
  end
@@ -0,0 +1,16 @@
1
+ module Mongoid #:nodoc:
2
+ module Extensions #:nodoc:
3
+ module DateTime #:nodoc:
4
+ module Conversions #:nodoc:
5
+ def set(value)
6
+ return nil if value.blank?
7
+ ::DateTime.parse(value.to_s).utc
8
+ end
9
+ def get(value)
10
+ return nil if value.blank?
11
+ ::Time.zone ? ::Time.parse(value.to_s).getlocal.to_datetime : value.to_datetime
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,90 @@
1
+ module Mongoid #:nodoc:
2
+ module Finders #:nodoc:
3
+ # Find +Documents+ given the conditions.
4
+ #
5
+ # Options:
6
+ #
7
+ # args: A +Hash+ with a conditions key and other options
8
+ #
9
+ # <tt>Person.all(:conditions => { :attribute => "value" })</tt>
10
+ def all(*args)
11
+ find(:all, *args)
12
+ end
13
+
14
+ # Returns a count of matching records in the database based on the
15
+ # provided arguments.
16
+ #
17
+ # <tt>Person.count(:first, :conditions => { :attribute => "value" })</tt>
18
+ def count(*args)
19
+ Criteria.translate(*args).count(self)
20
+ end
21
+
22
+ # Find a +Document+ in several different ways.
23
+ #
24
+ # If a +String+ is provided, it will be assumed that it is a
25
+ # representation of a Mongo::ObjectID and will attempt to find a single
26
+ # +Document+ based on that id. If a +Symbol+ and +Hash+ is provided then
27
+ # it will attempt to find either a single +Document+ or multiples based
28
+ # on the conditions provided and the first parameter.
29
+ #
30
+ # <tt>Person.find(:first, :conditions => { :attribute => "value" })</tt>
31
+ #
32
+ # <tt>Person.find(:all, :conditions => { :attribute => "value" })</tt>
33
+ #
34
+ # <tt>Person.find(Mongo::ObjectID.new.to_s)</tt>
35
+ def find(*args)
36
+ Criteria.translate(*args).execute(self)
37
+ end
38
+
39
+ # Find a +Document+ by its id.
40
+ def find_by_id(id)
41
+ find(id)
42
+ end
43
+
44
+ # Find the first +Document+ given the conditions.
45
+ #
46
+ # Options:
47
+ #
48
+ # args: A +Hash+ with a conditions key and other options
49
+ #
50
+ # <tt>Person.first(:conditions => { :attribute => "value" })</tt>
51
+ def first(*args)
52
+ find(:first, *args)
53
+ end
54
+
55
+ # Find all documents in paginated fashion given the supplied arguments.
56
+ # If no parameters are passed just default to offset 0 and limit 20.
57
+ #
58
+ # Options:
59
+ #
60
+ # params: A +Hash+ of params to pass to the Criteria API.
61
+ #
62
+ # Example:
63
+ #
64
+ # <tt>Person.paginate(:conditions => { :field => "Test" }, :page => 1,
65
+ # :per_page => 20)</tt>
66
+ #
67
+ # Returns paginated array of docs.
68
+ def paginate(params = {})
69
+ Criteria.translate(:all, params).paginate(self)
70
+ end
71
+
72
+ # Entry point for creating a new criteria from a Document. This will
73
+ # instantiate a new +Criteria+ object with the supplied select criterion
74
+ # already added to it.
75
+ #
76
+ # Options:
77
+ #
78
+ # args: A list of field names to retrict the returned fields to.
79
+ #
80
+ # Example:
81
+ #
82
+ # <tt>Person.select(:field1, :field2, :field3)</tt>
83
+ #
84
+ # Returns: <tt>Criteria</tt>
85
+ def select(*args)
86
+ Criteria.new(:all, self).select(*args)
87
+ end
88
+
89
+ end
90
+ end
data/mongoid.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.8.2"
8
+ s.version = "0.8.3"
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-11-17}
12
+ s.date = %q{2009-11-19}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -45,6 +45,7 @@ Gem::Specification.new do |s|
45
45
  "lib/mongoid/extensions/array/parentization.rb",
46
46
  "lib/mongoid/extensions/boolean/conversions.rb",
47
47
  "lib/mongoid/extensions/date/conversions.rb",
48
+ "lib/mongoid/extensions/datetime/conversions.rb",
48
49
  "lib/mongoid/extensions/float/conversions.rb",
49
50
  "lib/mongoid/extensions/hash/accessors.rb",
50
51
  "lib/mongoid/extensions/hash/conversions.rb",
@@ -56,6 +57,7 @@ Gem::Specification.new do |s|
56
57
  "lib/mongoid/extensions/symbol/inflections.rb",
57
58
  "lib/mongoid/extensions/time/conversions.rb",
58
59
  "lib/mongoid/field.rb",
60
+ "lib/mongoid/finders.rb",
59
61
  "lib/mongoid/timestamps.rb",
60
62
  "lib/mongoid/versioning.rb",
61
63
  "mongoid.gemspec",
@@ -85,6 +87,7 @@ Gem::Specification.new do |s|
85
87
  "spec/unit/mongoid/extensions/array/parentization_spec.rb",
86
88
  "spec/unit/mongoid/extensions/boolean/conversions_spec.rb",
87
89
  "spec/unit/mongoid/extensions/date/conversions_spec.rb",
90
+ "spec/unit/mongoid/extensions/datetime/conversions_spec.rb",
88
91
  "spec/unit/mongoid/extensions/float/conversions_spec.rb",
89
92
  "spec/unit/mongoid/extensions/hash/accessors_spec.rb",
90
93
  "spec/unit/mongoid/extensions/hash/conversions_spec.rb",
@@ -95,6 +98,7 @@ Gem::Specification.new do |s|
95
98
  "spec/unit/mongoid/extensions/symbol/inflections_spec.rb",
96
99
  "spec/unit/mongoid/extensions/time/conversions_spec.rb",
97
100
  "spec/unit/mongoid/field_spec.rb",
101
+ "spec/unit/mongoid/finders_spec.rb",
98
102
  "spec/unit/mongoid/timestamps_spec.rb",
99
103
  "spec/unit/mongoid/versioning_spec.rb"
100
104
  ]
@@ -128,6 +132,7 @@ Gem::Specification.new do |s|
128
132
  "spec/unit/mongoid/extensions/array/parentization_spec.rb",
129
133
  "spec/unit/mongoid/extensions/boolean/conversions_spec.rb",
130
134
  "spec/unit/mongoid/extensions/date/conversions_spec.rb",
135
+ "spec/unit/mongoid/extensions/datetime/conversions_spec.rb",
131
136
  "spec/unit/mongoid/extensions/float/conversions_spec.rb",
132
137
  "spec/unit/mongoid/extensions/hash/accessors_spec.rb",
133
138
  "spec/unit/mongoid/extensions/hash/conversions_spec.rb",
@@ -138,6 +143,7 @@ Gem::Specification.new do |s|
138
143
  "spec/unit/mongoid/extensions/symbol/inflections_spec.rb",
139
144
  "spec/unit/mongoid/extensions/time/conversions_spec.rb",
140
145
  "spec/unit/mongoid/field_spec.rb",
146
+ "spec/unit/mongoid/finders_spec.rb",
141
147
  "spec/unit/mongoid/timestamps_spec.rb",
142
148
  "spec/unit/mongoid/versioning_spec.rb"
143
149
  ]
@@ -56,7 +56,7 @@ describe Mongoid::Associations do
56
56
 
57
57
  end
58
58
 
59
- describe "#belongs_to" do
59
+ describe ".belongs_to" do
60
60
 
61
61
  it "creates a reader for the association" do
62
62
  address = Address.new
@@ -93,7 +93,7 @@ describe Mongoid::Associations do
93
93
 
94
94
  end
95
95
 
96
- describe "#has_many" do
96
+ describe ".has_many" do
97
97
 
98
98
  it "adds a new Association to the collection" do
99
99
  person = Person.new
@@ -160,7 +160,7 @@ describe Mongoid::Associations do
160
160
 
161
161
  end
162
162
 
163
- describe "#has_one" do
163
+ describe ".has_one" do
164
164
 
165
165
  it "adds a new Association to the collection" do
166
166
  person = Person.new
@@ -72,4 +72,20 @@ describe Mongoid::Commands::Save do
72
72
 
73
73
  end
74
74
 
75
+ context "when the document is embedded" do
76
+
77
+ before do
78
+ @child_name = Name.new(:first_name => "Testy")
79
+ end
80
+
81
+ context "when parent reference does not exist" do
82
+
83
+ it "raises an error" do
84
+ lambda { @child_name.save }.should raise_error
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+
75
91
  end
@@ -127,7 +127,7 @@ describe Mongoid::Commands do
127
127
 
128
128
  context "class methods" do
129
129
 
130
- describe "#create" do
130
+ describe ".create" do
131
131
 
132
132
  it "delegates to the Create command" do
133
133
  Mongoid::Commands::Create.expects(:execute)
@@ -141,7 +141,7 @@ describe Mongoid::Commands do
141
141
 
142
142
  end
143
143
 
144
- describe "#create!" do
144
+ describe ".create!" do
145
145
 
146
146
  it "delegates to the Create command" do
147
147
  Mongoid::Commands::Create.expects(:execute).returns(Person.new)
@@ -165,7 +165,7 @@ describe Mongoid::Commands do
165
165
 
166
166
  end
167
167
 
168
- describe "#delete_all" do
168
+ describe ".delete_all" do
169
169
 
170
170
  it "delegates to the DeleteAll command" do
171
171
  Mongoid::Commands::DeleteAll.expects(:execute).with(Person, {})
@@ -174,7 +174,7 @@ describe Mongoid::Commands do
174
174
 
175
175
  end
176
176
 
177
- describe "#destroy_all" do
177
+ describe ".destroy_all" do
178
178
 
179
179
  it "delegates to the DestroyAll command" do
180
180
  Mongoid::Commands::DestroyAll.expects(:execute).with(Person, {})
@@ -441,6 +441,23 @@ describe Mongoid::Criteria do
441
441
 
442
442
  end
443
443
 
444
+ describe "#paginate" do
445
+
446
+ before do
447
+ @collection = mock
448
+ Person.expects(:collection).returns(@collection)
449
+ @criteria = Person.select.where(:_id => "1").skip(60).limit(20)
450
+ @collection.expects(:find).with({:_id => "1"}, :skip => 60, :limit => 20).returns([])
451
+ @results = @criteria.paginate
452
+ end
453
+
454
+ it "executes and paginates the results" do
455
+ @results.current_page.should == 4
456
+ @results.per_page.should == 20
457
+ end
458
+
459
+ end
460
+
444
461
  describe "#per_page" do
445
462
 
446
463
  context "when the per_page option exists" do
@@ -521,7 +538,7 @@ describe Mongoid::Criteria do
521
538
 
522
539
  end
523
540
 
524
- describe "#translate" do
541
+ describe ".translate" do
525
542
 
526
543
  context "with a single argument" do
527
544
 
@@ -56,35 +56,6 @@ describe Mongoid::Document do
56
56
 
57
57
  end
58
58
 
59
- describe "#all" do
60
-
61
- before do
62
- @cursor = stub(:count => 100)
63
- @people = []
64
- end
65
-
66
- context "when a selector is provided" do
67
-
68
- it "finds from the collection and instantiate objects for each returned" do
69
- @collection.expects(:find).with({ :test => "Test" }, {}).returns(@cursor)
70
- @cursor.expects(:collect).returns(@people)
71
- Person.all(:conditions => {:test => "Test"})
72
- end
73
-
74
- end
75
-
76
- context "when a selector is not provided" do
77
-
78
- it "finds from the collection and instantiate objects for each returned" do
79
- @collection.expects(:find).with(nil, {}).returns(@cursor)
80
- @cursor.expects(:collect).returns(@people)
81
- Person.all
82
- end
83
-
84
- end
85
-
86
- end
87
-
88
59
  describe "#clone" do
89
60
 
90
61
  before do
@@ -99,7 +70,7 @@ describe Mongoid::Document do
99
70
 
100
71
  end
101
72
 
102
- describe "#collection" do
73
+ describe ".collection" do
103
74
 
104
75
  before do
105
76
  @person = Person.new
@@ -123,31 +94,6 @@ describe Mongoid::Document do
123
94
 
124
95
  end
125
96
 
126
- describe "#count" do
127
-
128
- before do
129
- @params = { :conditions => { :title => "Sir" } }
130
- @criteria = mock
131
- end
132
-
133
- it "delegates to the criteria api" do
134
- Mongoid::Criteria.expects(:translate).with(@params).returns(@criteria)
135
- @criteria.expects(:count).with(Person).returns(10)
136
- Person.count(@params).should == 10
137
- end
138
-
139
- context "when no options provided" do
140
-
141
- it "adds in the default parameters" do
142
- Mongoid::Criteria.expects(:translate).with(nil).returns(@criteria)
143
- @criteria.expects(:count).with(Person).returns(10)
144
- Person.count.should == 10
145
- end
146
-
147
- end
148
-
149
- end
150
-
151
97
  describe ".defaults" do
152
98
 
153
99
  it "returns a hash of all the default values" do
@@ -210,7 +156,7 @@ describe Mongoid::Document do
210
156
 
211
157
  end
212
158
 
213
- describe "#field" do
159
+ describe ".field" do
214
160
 
215
161
  context "with no options" do
216
162
 
@@ -248,107 +194,6 @@ describe Mongoid::Document do
248
194
 
249
195
  end
250
196
 
251
- describe "#find" do
252
-
253
- before do
254
- @attributes = {}
255
- @criteria = mock
256
- end
257
-
258
- context "when an id is passed in" do
259
-
260
- before do
261
- @id = Mongo::ObjectID.new.to_s
262
- end
263
-
264
- it "delegates to criteria" do
265
- Mongoid::Criteria.expects(:translate).with(@id.to_s).returns(@criteria)
266
- @criteria.expects(:execute).with(Person).returns(@attributes)
267
- Person.find(@id.to_s)
268
- end
269
-
270
- end
271
-
272
- context "when finding first" do
273
-
274
- it "delegates to criteria" do
275
- Mongoid::Criteria.expects(:translate).with(:first, :conditions => { :test => "Test" }).returns(@criteria)
276
- @criteria.expects(:execute).with(Person).returns(@attributes)
277
- Person.find(:first, :conditions => { :test => "Test" })
278
- end
279
-
280
- end
281
-
282
- context "when finding all" do
283
-
284
- before do
285
- @cursor = stub(:count => 100)
286
- @people = []
287
- end
288
-
289
- it "delegates to find_all" do
290
- @collection.expects(:find).with({:test => "Test"}, {}).returns(@cursor)
291
- @cursor.expects(:collect).returns(@people)
292
- Person.find(:all, :conditions => { :test => "Test" })
293
- end
294
-
295
- end
296
-
297
- context "when sorting" do
298
-
299
- before do
300
- @cursor = stub(:count => 50)
301
- @people = []
302
- end
303
-
304
- it "adds the sort parameters for the collection call" do
305
- @collection.expects(:find).with({ :test => "Test" }, { :sort => { :test => -1 }}).returns(@cursor)
306
- @cursor.expects(:collect).returns(@people)
307
- Person.find(:all, :conditions => { :test => "Test" }, :sort => { :test => -1 })
308
- end
309
- end
310
-
311
- end
312
-
313
- describe "#find_by_id" do
314
-
315
- before do
316
- @criteria = stub_everything
317
- end
318
-
319
- it "delegates to find with an id parameter" do
320
- Mongoid::Criteria.expects(:translate).with("1").returns(@criteria)
321
- Person.find_by_id("1")
322
- end
323
-
324
- end
325
-
326
- describe "#first" do
327
-
328
- before do
329
- @attributes = { "age" => 100 }
330
- end
331
-
332
- context "when a selector is provided" do
333
-
334
- it "finds the first document from the collection and instantiates it" do
335
- @collection.expects(:find_one).with({ :test => "Test" }, {}).returns(@attributes)
336
- Person.first(:conditions => {:test => "Test"}).attributes.except(:_id).should == @attributes
337
- end
338
-
339
- end
340
-
341
- context "when a selector is not provided" do
342
-
343
- it "finds the first document from the collection and instantiates it" do
344
- @collection.expects(:find_one).with(nil, {}).returns(@attributes)
345
- Person.first.attributes.except(:_id).should == @attributes
346
- end
347
-
348
- end
349
-
350
- end
351
-
352
197
  describe "#_id" do
353
198
 
354
199
  before do
@@ -361,7 +206,7 @@ describe Mongoid::Document do
361
206
 
362
207
  end
363
208
 
364
- describe "#index" do
209
+ describe ".index" do
365
210
 
366
211
  context "when unique options are not provided" do
367
212
 
@@ -383,7 +228,7 @@ describe Mongoid::Document do
383
228
 
384
229
  end
385
230
 
386
- describe "#instantiate" do
231
+ describe ".instantiate" do
387
232
 
388
233
  context "when document is new" do
389
234
 
@@ -413,7 +258,7 @@ describe Mongoid::Document do
413
258
 
414
259
  end
415
260
 
416
- describe "#key" do
261
+ describe ".key" do
417
262
 
418
263
  context "when key is single field" do
419
264
 
@@ -449,19 +294,6 @@ describe Mongoid::Document do
449
294
 
450
295
  end
451
296
 
452
- describe "#last" do
453
-
454
- before do
455
- @attributes = { :_id => 1, :title => "Sir" }
456
- @collection.expects(:find_one).with({}, :sort => [[:_id, :asc]]).returns(@attributes)
457
- end
458
-
459
- it "finds the last document by the id" do
460
- Person.last.should == Person.instantiate(@attributes)
461
- end
462
-
463
- end
464
-
465
297
  describe "#new" do
466
298
 
467
299
  context "with no attributes" do
@@ -547,42 +379,6 @@ describe Mongoid::Document do
547
379
 
548
380
  end
549
381
 
550
- describe "#paginate" do
551
-
552
- before do
553
- @criteria = stub(:page => 1, :per_page => "20", :count => 100)
554
- end
555
-
556
- context "when pagination parameters are passed" do
557
-
558
- before do
559
- @params = { :conditions => { :test => "Test" }, :page => 2, :per_page => 20 }
560
- end
561
-
562
- it "delegates to will paginate with the results" do
563
- Mongoid::Criteria.expects(:translate).with(:all, @params).returns(@criteria)
564
- @criteria.expects(:execute).with(Person).returns([])
565
- Person.paginate(@params)
566
- end
567
-
568
- end
569
-
570
- context "when pagination parameters are not passed" do
571
-
572
- before do
573
- @params = { :conditions => { :test => "Test" }}
574
- end
575
-
576
- it "delegates to will paginate with default values" do
577
- Mongoid::Criteria.expects(:translate).with(:all, @params).returns(@criteria)
578
- @criteria.expects(:execute).with(Person).returns([])
579
- Person.paginate(:conditions => { :test => "Test" })
580
- end
581
-
582
- end
583
-
584
- end
585
-
586
382
  describe "#parent" do
587
383
 
588
384
  before do
@@ -692,15 +488,6 @@ describe Mongoid::Document do
692
488
 
693
489
  end
694
490
 
695
- describe "#select" do
696
-
697
- it "returns a new criteria with select conditions added" do
698
- criteria = Person.select(:title, :age)
699
- criteria.options.should == { :fields => [ :title, :age ] }
700
- end
701
-
702
- end
703
-
704
491
  describe "#to_param" do
705
492
 
706
493
  it "returns the id" do
@@ -0,0 +1,65 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb"))
2
+
3
+ describe Mongoid::Extensions::DateTime::Conversions do
4
+
5
+ describe "#set" do
6
+
7
+ before do
8
+ @date_time = DateTime.civil(2005, 11, 19).new_offset(1800)
9
+ end
10
+
11
+ context "when value is a string" do
12
+
13
+ it "converts to a utc time" do
14
+ DateTime.set(@date_time.to_s).utc_offset.should == 0
15
+ end
16
+
17
+ end
18
+
19
+ context "when value is a date_time" do
20
+
21
+ it "converts to a utc time" do
22
+ DateTime.set(@date_time).utc_offset.should == 0
23
+ end
24
+
25
+ end
26
+
27
+ context "when value is nil" do
28
+
29
+ it "returns nil" do
30
+ DateTime.set(nil).should be_nil
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ describe "#get" do
38
+
39
+ before do
40
+ @time = Time.now.utc
41
+ end
42
+
43
+ context "when time is provided" do
44
+
45
+ it "returns a DateTime" do
46
+ DateTime.get(@time.dup).should be_a_kind_of(DateTime)
47
+ end
48
+
49
+ it "returns the local date_time" do
50
+ DateTime.get(@time.dup).utc_offset.should == @time.utc_offset
51
+ end
52
+
53
+ end
54
+
55
+ context "when time is nil" do
56
+
57
+ it "returns nil" do
58
+ DateTime.get(nil).should be_nil
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,230 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "/../../spec_helper.rb"))
2
+
3
+ describe Mongoid::Finders do
4
+
5
+ before do
6
+ @collection = stub(:name => "people")
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
+ @database = nil
14
+ @collection = nil
15
+ end
16
+
17
+ describe ".all" do
18
+
19
+ before do
20
+ @cursor = stub(:count => 100)
21
+ @people = []
22
+ end
23
+
24
+ context "when a selector is provided" do
25
+
26
+ it "finds from the collection and instantiate objects for each returned" do
27
+ @collection.expects(:find).with({ :test => "Test" }, {}).returns(@cursor)
28
+ @cursor.expects(:collect).returns(@people)
29
+ Person.all(:conditions => {:test => "Test"})
30
+ end
31
+
32
+ end
33
+
34
+ context "when a selector is not provided" do
35
+
36
+ it "finds from the collection and instantiate objects for each returned" do
37
+ @collection.expects(:find).with(nil, {}).returns(@cursor)
38
+ @cursor.expects(:collect).returns(@people)
39
+ Person.all
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ describe ".count" do
47
+
48
+ before do
49
+ @params = { :conditions => { :title => "Sir" } }
50
+ @criteria = mock
51
+ end
52
+
53
+ it "delegates to the criteria api" do
54
+ Mongoid::Criteria.expects(:translate).with(@params).returns(@criteria)
55
+ @criteria.expects(:count).with(Person).returns(10)
56
+ Person.count(@params).should == 10
57
+ end
58
+
59
+ context "when no options provided" do
60
+
61
+ it "adds in the default parameters" do
62
+ Mongoid::Criteria.expects(:translate).with(nil).returns(@criteria)
63
+ @criteria.expects(:count).with(Person).returns(10)
64
+ Person.count.should == 10
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ describe ".find" do
72
+
73
+ before do
74
+ @attributes = {}
75
+ @criteria = mock
76
+ end
77
+
78
+ context "when an id is passed in" do
79
+
80
+ before do
81
+ @id = Mongo::ObjectID.new.to_s
82
+ end
83
+
84
+ it "delegates to criteria" do
85
+ Mongoid::Criteria.expects(:translate).with(@id.to_s).returns(@criteria)
86
+ @criteria.expects(:execute).with(Person).returns(@attributes)
87
+ Person.find(@id.to_s)
88
+ end
89
+
90
+ end
91
+
92
+ context "when finding first" do
93
+
94
+ it "delegates to criteria" do
95
+ Mongoid::Criteria.expects(:translate).with(:first, :conditions => { :test => "Test" }).returns(@criteria)
96
+ @criteria.expects(:execute).with(Person).returns(@attributes)
97
+ Person.find(:first, :conditions => { :test => "Test" })
98
+ end
99
+
100
+ end
101
+
102
+ context "when finding all" do
103
+
104
+ before do
105
+ @cursor = stub(:count => 100)
106
+ @people = []
107
+ end
108
+
109
+ it "delegates to find_all" do
110
+ @collection.expects(:find).with({:test => "Test"}, {}).returns(@cursor)
111
+ @cursor.expects(:collect).returns(@people)
112
+ Person.find(:all, :conditions => { :test => "Test" })
113
+ end
114
+
115
+ end
116
+
117
+ context "when sorting" do
118
+
119
+ before do
120
+ @cursor = stub(:count => 50)
121
+ @people = []
122
+ end
123
+
124
+ it "adds the sort parameters for the collection call" do
125
+ @collection.expects(:find).with({ :test => "Test" }, { :sort => { :test => -1 }}).returns(@cursor)
126
+ @cursor.expects(:collect).returns(@people)
127
+ Person.find(:all, :conditions => { :test => "Test" }, :sort => { :test => -1 })
128
+ end
129
+ end
130
+
131
+ end
132
+
133
+ describe ".find_by_id" do
134
+
135
+ before do
136
+ @criteria = stub_everything
137
+ end
138
+
139
+ it "delegates to find with an id parameter" do
140
+ Mongoid::Criteria.expects(:translate).with("1").returns(@criteria)
141
+ Person.find_by_id("1")
142
+ end
143
+
144
+ end
145
+
146
+ describe "#first" do
147
+
148
+ before do
149
+ @attributes = { "age" => 100 }
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.first(:conditions => {:test => "Test"}).attributes.except(:_id).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.first.attributes.except(:_id).should == @attributes
166
+ end
167
+
168
+ end
169
+
170
+ end
171
+
172
+ describe ".last" do
173
+
174
+ before do
175
+ @attributes = { :_id => 1, :title => "Sir" }
176
+ @collection.expects(:find_one).with({}, :sort => [[:_id, :asc]]).returns(@attributes)
177
+ end
178
+
179
+ it "finds the last document by the id" do
180
+ Person.last.should == Person.instantiate(@attributes)
181
+ end
182
+
183
+ end
184
+
185
+ describe ".paginate" do
186
+
187
+ before do
188
+ @criteria = stub(:page => 1, :per_page => "20", :count => 100)
189
+ end
190
+
191
+ context "when pagination parameters are passed" do
192
+
193
+ before do
194
+ @params = { :conditions => { :test => "Test" }, :page => 2, :per_page => 20 }
195
+ end
196
+
197
+ it "delegates to will paginate with the results" do
198
+ Mongoid::Criteria.expects(:translate).with(:all, @params).returns(@criteria)
199
+ @criteria.expects(:paginate).with(Person).returns([])
200
+ Person.paginate(@params)
201
+ end
202
+
203
+ end
204
+
205
+ context "when pagination parameters are not passed" do
206
+
207
+ before do
208
+ @params = { :conditions => { :test => "Test" }}
209
+ end
210
+
211
+ it "delegates to will paginate with default values" do
212
+ Mongoid::Criteria.expects(:translate).with(:all, @params).returns(@criteria)
213
+ @criteria.expects(:paginate).with(Person).returns([])
214
+ Person.paginate(:conditions => { :test => "Test" })
215
+ end
216
+
217
+ end
218
+
219
+ end
220
+
221
+ describe ".select" do
222
+
223
+ it "returns a new criteria with select conditions added" do
224
+ criteria = Person.select(:title, :age)
225
+ criteria.options.should == { :fields => [ :title, :age ] }
226
+ end
227
+
228
+ end
229
+
230
+ end
@@ -2,7 +2,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), "/../../spec_helper.r
2
2
 
3
3
  describe Mongoid::Timestamps do
4
4
 
5
- describe "#included" do
5
+ describe ".included" do
6
6
 
7
7
  before do
8
8
  @person = Person.new
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.8.2
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-17 00:00:00 -05:00
12
+ date: 2009-11-19 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -101,6 +101,7 @@ files:
101
101
  - lib/mongoid/extensions/array/parentization.rb
102
102
  - lib/mongoid/extensions/boolean/conversions.rb
103
103
  - lib/mongoid/extensions/date/conversions.rb
104
+ - lib/mongoid/extensions/datetime/conversions.rb
104
105
  - lib/mongoid/extensions/float/conversions.rb
105
106
  - lib/mongoid/extensions/hash/accessors.rb
106
107
  - lib/mongoid/extensions/hash/conversions.rb
@@ -112,6 +113,7 @@ files:
112
113
  - lib/mongoid/extensions/symbol/inflections.rb
113
114
  - lib/mongoid/extensions/time/conversions.rb
114
115
  - lib/mongoid/field.rb
116
+ - lib/mongoid/finders.rb
115
117
  - lib/mongoid/timestamps.rb
116
118
  - lib/mongoid/versioning.rb
117
119
  - mongoid.gemspec
@@ -141,6 +143,7 @@ files:
141
143
  - spec/unit/mongoid/extensions/array/parentization_spec.rb
142
144
  - spec/unit/mongoid/extensions/boolean/conversions_spec.rb
143
145
  - spec/unit/mongoid/extensions/date/conversions_spec.rb
146
+ - spec/unit/mongoid/extensions/datetime/conversions_spec.rb
144
147
  - spec/unit/mongoid/extensions/float/conversions_spec.rb
145
148
  - spec/unit/mongoid/extensions/hash/accessors_spec.rb
146
149
  - spec/unit/mongoid/extensions/hash/conversions_spec.rb
@@ -151,6 +154,7 @@ files:
151
154
  - spec/unit/mongoid/extensions/symbol/inflections_spec.rb
152
155
  - spec/unit/mongoid/extensions/time/conversions_spec.rb
153
156
  - spec/unit/mongoid/field_spec.rb
157
+ - spec/unit/mongoid/finders_spec.rb
154
158
  - spec/unit/mongoid/timestamps_spec.rb
155
159
  - spec/unit/mongoid/versioning_spec.rb
156
160
  has_rdoc: true
@@ -206,6 +210,7 @@ test_files:
206
210
  - spec/unit/mongoid/extensions/array/parentization_spec.rb
207
211
  - spec/unit/mongoid/extensions/boolean/conversions_spec.rb
208
212
  - spec/unit/mongoid/extensions/date/conversions_spec.rb
213
+ - spec/unit/mongoid/extensions/datetime/conversions_spec.rb
209
214
  - spec/unit/mongoid/extensions/float/conversions_spec.rb
210
215
  - spec/unit/mongoid/extensions/hash/accessors_spec.rb
211
216
  - spec/unit/mongoid/extensions/hash/conversions_spec.rb
@@ -216,5 +221,6 @@ test_files:
216
221
  - spec/unit/mongoid/extensions/symbol/inflections_spec.rb
217
222
  - spec/unit/mongoid/extensions/time/conversions_spec.rb
218
223
  - spec/unit/mongoid/field_spec.rb
224
+ - spec/unit/mongoid/finders_spec.rb
219
225
  - spec/unit/mongoid/timestamps_spec.rb
220
226
  - spec/unit/mongoid/versioning_spec.rb