mongoid 6.0.0.rc0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27ea8a75a0efde500417b73f4c4d38ab25f60c0b
4
- data.tar.gz: 998d18056b79086719e963a84be38ca6d3682c6d
3
+ metadata.gz: ba5a14c6242427982c5072b4f2e56ab499ff7559
4
+ data.tar.gz: 39d47341e981c5facfd7360ae9a83c6b3bd2dc00
5
5
  SHA512:
6
- metadata.gz: 1b948f4ba63c9fa1d749a71d5bb33856894736c3173bf52781702102ea90bdcb0b5b9f0547cc90d906589f1b1d088eef39b3a7f829c3dd7522142cb9f9113c51
7
- data.tar.gz: 1e6feb48bf957d9596f6b3f150c1054f393f2ee0562e0be41e5827bc0518e73bd2d86c1344379729eea9c9f2660b05014a197c5721d2b40733eba9891cc69b2e
6
+ metadata.gz: 9e932dbd7064df798fae822b792a82b5b387a9c8daa93826158b15e5cbffe0ee59a0f92eb5b003d1e051097f4b017d6fc6f0c989bc00b363f4a8853a45689ccc
7
+ data.tar.gz: 4121c17953fa7b3786e30ed6d3f0c473d5ed7bd26dc59e700c9d41061cd83e318d0c6a62c0539a4d74d7561f8b9bf9eaa437cce41421358d98c139705ad9325b
Binary file
data.tar.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -21,7 +21,7 @@ Project Tracking
21
21
  Compatibility
22
22
  -------------
23
23
 
24
- Mongoid is tested against MRI 1.9.3, 2.0.0, 2.1.0. 2.2.0 and JRuby (1.9).
24
+ Mongoid is tested against MRI 2.2, 2.3 and JRuby (9.1).
25
25
 
26
26
  Documentation
27
27
  -------------
@@ -268,11 +268,29 @@ module Mongoid
268
268
  # @since 3.0.0
269
269
  def documents
270
270
  return results["results"] if results.has_key?("results")
271
- view = client[output_collection].find
271
+ view = output_database[output_collection].find
272
272
  view.no_cursor_timeout if criteria.options[:timeout] == false
273
273
  view
274
274
  end
275
275
 
276
+ # Get the database that the map/reduce results were stored in.
277
+ #
278
+ # @api private
279
+ #
280
+ # @example Get the output database.
281
+ # map_reduce.output_database
282
+ #
283
+ # @return [ Mongo::Database ] The output database.
284
+ #
285
+ # @since 6.0.0
286
+ def output_database
287
+ if db = command[:out].fetch(:db, command[:out]['db'])
288
+ client.with(database: db).database
289
+ else
290
+ client.database
291
+ end
292
+ end
293
+
276
294
  # Get the collection that the map/reduce results were stored in.
277
295
  #
278
296
  # @api private
@@ -49,7 +49,7 @@ module Mongoid
49
49
  #
50
50
  # @since 2.0.0
51
51
  def freeze
52
- as_document.freeze and self
52
+ as_attributes.freeze and self
53
53
  end
54
54
 
55
55
  # Checks if the document is frozen
@@ -172,20 +172,7 @@ module Mongoid
172
172
  #
173
173
  # @since 1.0.0
174
174
  def as_document
175
- return attributes if frozen?
176
- embedded_relations.each_pair do |name, meta|
177
- without_autobuild do
178
- relation, stored = send(name), meta.store_as
179
- if attributes.key?(stored) || !relation.blank?
180
- if relation
181
- attributes[stored] = relation.as_document
182
- else
183
- attributes.delete(stored)
184
- end
185
- end
186
- end
187
- end
188
- attributes
175
+ BSON::Document.new(as_attributes)
189
176
  end
190
177
 
191
178
  # Calls #as_json on the document with additional, Mongoid-specific options.
@@ -285,6 +272,25 @@ module Mongoid
285
272
  nil
286
273
  end
287
274
 
275
+ private
276
+
277
+ def as_attributes
278
+ return attributes if frozen?
279
+ embedded_relations.each_pair do |name, meta|
280
+ without_autobuild do
281
+ relation, stored = send(name), meta.store_as
282
+ if attributes.key?(stored) || !relation.blank?
283
+ if relation
284
+ attributes[stored] = relation.as_document
285
+ else
286
+ attributes.delete(stored)
287
+ end
288
+ end
289
+ end
290
+ end
291
+ attributes
292
+ end
293
+
288
294
  module ClassMethods
289
295
 
290
296
  # Performs class equality checking.
@@ -105,6 +105,7 @@ module Mongoid
105
105
  keys = string.split(".")
106
106
  value = self
107
107
  keys.each do |key|
108
+ return nil if value.nil?
108
109
  nested = value[key] || value[key.to_i]
109
110
  value = nested
110
111
  end
@@ -48,7 +48,7 @@ module Mongoid
48
48
  @loaded = []
49
49
  while shift_metadata
50
50
  preload
51
- @loaded << @docs.collect { |d| d.send(@metadata.name) }
51
+ @loaded << @docs.collect { |d| d.send(@metadata.name) if d.respond_to?(@metadata.name) }
52
52
  end
53
53
  @loaded.flatten
54
54
  end
@@ -99,12 +99,12 @@ module Mongoid
99
99
  # @example Return a hash with the current documents grouped by key.
100
100
  # loader.grouped_docs
101
101
  #
102
- # @return [ Hash ] hash with groupd documents.
102
+ # @return [ Hash ] hash with grouped documents.
103
103
  #
104
104
  # @since 4.0.0
105
105
  def grouped_docs
106
106
  @grouped_docs[@metadata.name] ||= @docs.group_by do |doc|
107
- doc.send(group_by_key)
107
+ doc.send(group_by_key) if doc.respond_to?(group_by_key)
108
108
  end
109
109
  end
110
110
 
@@ -145,7 +145,7 @@ module Mongoid
145
145
  #
146
146
  # @since 4.0.0
147
147
  def set_relation(doc, element)
148
- doc.set_relation(@metadata.name, element)
148
+ doc.set_relation(@metadata.name, element) unless doc.blank?
149
149
  end
150
150
  end
151
151
  end
@@ -31,7 +31,7 @@ module Mongoid
31
31
  end
32
32
 
33
33
  def set_relation(doc, element)
34
- doc.__build__(@metadata.name, element, @metadata)
34
+ doc.__build__(@metadata.name, element, @metadata) unless doc.blank?
35
35
  end
36
36
 
37
37
  def group_by_key
@@ -22,7 +22,7 @@ module Mongoid
22
22
  end
23
23
 
24
24
  def set_relation(doc, element)
25
- doc.__build__(@metadata.name, element, @metadata)
25
+ doc.__build__(@metadata.name, element, @metadata) unless doc.blank?
26
26
  end
27
27
 
28
28
  def group_by_key
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "6.0.0.rc0"
3
+ VERSION = "6.0.0"
4
4
  end
@@ -115,21 +115,6 @@ describe Mongoid::Contextual::Mongo do
115
115
  end
116
116
  end
117
117
 
118
- context "when provided a document" do
119
-
120
- let(:context) do
121
- described_class.new(criteria)
122
- end
123
-
124
- let(:count) do
125
- context.count(depeche)
126
- end
127
-
128
- it "returns the number of documents that match" do
129
- expect(count).to eq(1)
130
- end
131
- end
132
-
133
118
  context "when provided a block" do
134
119
 
135
120
  let(:context) do
@@ -1400,6 +1385,65 @@ describe Mongoid::Contextual::Mongo do
1400
1385
  end
1401
1386
  end
1402
1387
 
1388
+ context "when the output specifies a different db" do
1389
+
1390
+ let(:criteria) do
1391
+ Band.limit(1)
1392
+ end
1393
+
1394
+ let(:context) do
1395
+ described_class.new(criteria)
1396
+ end
1397
+
1398
+ after do
1399
+ Band.with(database: 'another-db') do |b|
1400
+ b.all.delete
1401
+ end
1402
+ end
1403
+
1404
+ context 'when db is a string' do
1405
+
1406
+ let(:results) do
1407
+ context.map_reduce(map, reduce).out(merge: :mr_output, db: 'another-db')
1408
+ end
1409
+
1410
+ it "returns the correct number of documents" do
1411
+ expect(results.count).to eq(1)
1412
+ end
1413
+
1414
+ it "contains the entire results" do
1415
+ expect(results).to eq([
1416
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
1417
+ ])
1418
+ end
1419
+
1420
+ it 'writes to the specified db' do
1421
+ expect(Band.mongo_client.with(database: 'another-db')[:mr_output].find.count).to eq(1)
1422
+ end
1423
+ end
1424
+
1425
+ context 'when db is a symbol' do
1426
+
1427
+ let(:results) do
1428
+ context.map_reduce(map, reduce).out(merge: :mr_output, 'db' => 'another-db')
1429
+ end
1430
+
1431
+ it "returns the correct number of documents" do
1432
+ expect(results.count).to eq(1)
1433
+ end
1434
+
1435
+ it "contains the entire results" do
1436
+ expect(results).to eq([
1437
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }}
1438
+ ])
1439
+ end
1440
+
1441
+ it 'writes to the specified db' do
1442
+ expect(Band.mongo_client.with(database: 'another-db')[:mr_output].find.count).to eq(1)
1443
+ end
1444
+ end
1445
+ end
1446
+
1403
1447
  context "when providing no output" do
1404
1448
 
1405
1449
  let(:criteria) do
@@ -450,14 +450,26 @@ describe Mongoid::Document do
450
450
  expect(person.as_document).to have_key("name")
451
451
  end
452
452
 
453
+ it "includes embeds one attributes as a symbol" do
454
+ expect(person.as_document).to have_key(:name)
455
+ end
456
+
453
457
  it "includes embeds many attributes" do
454
458
  expect(person.as_document).to have_key("addresses")
455
459
  end
456
460
 
461
+ it "includes embeds many attributes as a symbol" do
462
+ expect(person.as_document).to have_key(:addresses)
463
+ end
464
+
457
465
  it "includes second level embeds many attributes" do
458
466
  expect(person.as_document["addresses"].first).to have_key("locations")
459
467
  end
460
468
 
469
+ it "includes second level embeds many attributes as a symbol" do
470
+ expect(person.as_document["addresses"].first).to have_key(:locations)
471
+ end
472
+
461
473
  context "with relation define store_as option in embeded_many" do
462
474
 
463
475
  let!(:phone) do
@@ -468,6 +480,10 @@ describe Mongoid::Document do
468
480
  expect(person.as_document).to have_key("mobile_phones")
469
481
  end
470
482
 
483
+ it 'includes the store_as key association as a symbol' do
484
+ expect(person.as_document).to have_key(:mobile_phones)
485
+ end
486
+
471
487
  it 'should not include the key of association' do
472
488
  expect(person.as_document).to_not have_key("phones")
473
489
  end
@@ -247,6 +247,21 @@ describe Mongoid::Extensions::Hash do
247
247
  end
248
248
  end
249
249
 
250
+ context "when the parent key is not present" do
251
+
252
+ let(:hash) do
253
+ { "101" => { "name" => "hundred and one" } }
254
+ end
255
+
256
+ let(:nested) do
257
+ hash.__nested__("100.name")
258
+ end
259
+
260
+ it "should return nil" do
261
+ expect(nested).to eq(nil)
262
+ end
263
+ end
264
+
250
265
  describe ".demongoize" do
251
266
 
252
267
  let(:hash) do
@@ -38,7 +38,7 @@ describe Mongoid::QueryCache do
38
38
  end
39
39
 
40
40
  it 'queries for each access to the base' do
41
- expect(server).to receive(:context).exactly(relations.size).times.and_call_original
41
+ expect(server).to receive(:with_connection).exactly(relations.size).times.and_call_original
42
42
  relations.each do |object|
43
43
  object.person
44
44
  end
@@ -52,7 +52,7 @@ describe Mongoid::QueryCache do
52
52
  end
53
53
 
54
54
  it 'queries only once for the base' do
55
- expect(server).to receive(:context).exactly(1).times.and_call_original
55
+ expect(server).to receive(:with_connection).exactly(1).times.and_call_original
56
56
  relations.each do |object|
57
57
  object.person
58
58
  end
@@ -181,6 +181,46 @@ describe Mongoid::Relations::Eager do
181
181
  expect(Mongoid::Relations::Eager::HasAndBelongsToMany).to receive(:new).with([houses_metadata], docs).once.and_call_original
182
182
  context.eager_load(docs)
183
183
  end
184
+
185
+ context 'when one of the eager loading definitions is nested' do
186
+
187
+ before do
188
+ class User
189
+ include Mongoid::Document
190
+ end
191
+
192
+ class Unit
193
+ include Mongoid::Document
194
+ end
195
+
196
+ class Booking
197
+ include Mongoid::Document
198
+ belongs_to :unit
199
+ has_many :vouchers
200
+ end
201
+
202
+ class Voucher
203
+ include Mongoid::Document
204
+ belongs_to :booking
205
+ belongs_to :created_by, class_name: 'User'
206
+ end
207
+ end
208
+
209
+ it 'successfully loads all relations' do
210
+ user = User.create
211
+ unit = Unit.create
212
+ booking = Booking.create(unit: unit)
213
+ Voucher.create(booking: booking, created_by: user)
214
+
215
+ vouchers = Voucher.includes(:created_by, booking: [:unit])
216
+
217
+ vouchers.each do |voucher|
218
+ expect(voucher.created_by).to eql(user)
219
+ expect(voucher.booking).to eql(booking)
220
+ expect(voucher.booking.unit).to eql(unit)
221
+ end
222
+ end
223
+ end
184
224
  end
185
225
 
186
226
  context "when including two of the same relation type" do
@@ -879,6 +879,36 @@ describe Mongoid::Relations::Embedded::Many do
879
879
  Person.create
880
880
  end
881
881
 
882
+ context 'when a string is used to access an attribute' do
883
+
884
+ let!(:address) do
885
+ person.addresses.create(street: "one")
886
+ end
887
+
888
+ let(:document) do
889
+ person.reload.addresses.as_document.first
890
+ end
891
+
892
+ it "returns the attribute value" do
893
+ expect(document['street']).to eq('one')
894
+ end
895
+ end
896
+
897
+ context 'when a symbol is used to access an attribute' do
898
+
899
+ let!(:address) do
900
+ person.addresses.create(street: "one")
901
+ end
902
+
903
+ let(:document) do
904
+ person.reload.addresses.as_document.first
905
+ end
906
+
907
+ it "returns the attribute value" do
908
+ expect(document[:street]).to eq('one')
909
+ end
910
+ end
911
+
882
912
  context "when the relation has no default scope" do
883
913
 
884
914
  let!(:address) 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: 6.0.0.rc0
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -30,7 +30,7 @@ cert_chain:
30
30
  ZIvvwAhgCjVW5QCi2I1noxXLmtZ3XDawWu8kaGtu8giHXcwL3941m8hvFZ/Wr9Yi
31
31
  JvcXJt2a4/JvwnIs2hmKuyfhZmB9HEE5wQQaCMnnC14=
32
32
  -----END CERTIFICATE-----
33
- date: 2016-08-09 00:00:00.000000000 Z
33
+ date: 2016-09-21 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: activemodel
@@ -52,14 +52,14 @@ dependencies:
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '2.2'
55
+ version: '2.3'
56
56
  type: :runtime
57
57
  prerelease: false
58
58
  version_requirements: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: '2.2'
62
+ version: '2.3'
63
63
  description: Mongoid is an ODM (Object Document Mapper) Framework for MongoDB, written
64
64
  in Ruby.
65
65
  email:
metadata.gz.sig CHANGED
Binary file