mongoid 7.0.0 → 7.0.1

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
  SHA256:
3
- metadata.gz: 8d7943ffc77e9a6990cb063c1301476cd1d5b10cd8543f0e9861e96228ff8027
4
- data.tar.gz: bc71866e339c29688dae94bdd8b4590b198ab25b503bc321802d6acea4af5c25
3
+ metadata.gz: 24a3c4aba84097db1be0810c29d175dfa6e69f6346efa857ac124aeb77bec1b1
4
+ data.tar.gz: b181a4840f966322895bf4dfe0b64cb136513412d8a3ca89b9aa28a342d27054
5
5
  SHA512:
6
- metadata.gz: 51e00c706c33a552c148b768bf851042a2f5c46aecacd3aa6bed5105c8c9b002bf84f317b1d5d65ee209e153537af5fb7360560758aef34ca4cbc39ffa11b489
7
- data.tar.gz: 81a21a9ee3e6aa381bb495eb098b7faba65f2fa32c304460ecb481356ab505bb1189c4850cf218680d93c269cb656360727d3ed3fd967de5525ff92bf510425d
6
+ metadata.gz: 12979183bf4d077b673beb7a57632fc73b7f1672be666401c68969ae675164b0d0641881cdcb8fb43bec283c4b4ca4d946f63a0c988bb58ac4065e677a5278c6
7
+ data.tar.gz: 3ae9210672c5c5dc3f8536983e814840fc823668373c9594d9a7cb834b533e945eca850c5840dbf0f90e7f707318c0a16823c0a54599d29b5f3a6308f5bf2dd1
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -39,7 +39,7 @@ module Mongoid
39
39
  unless docs.empty?
40
40
  collection.find(selector).update_one(
41
41
  positionally(selector, "$unset" => { path => true }),
42
- session: session
42
+ session: _session
43
43
  )
44
44
  post_process_batch_remove(docs, :delete)
45
45
  end
@@ -60,7 +60,7 @@ module Mongoid
60
60
  if !docs.empty?
61
61
  collection.find(selector).update_one(
62
62
  positionally(selector, "$pullAll" => { path => removals }),
63
- session: session
63
+ session: _session
64
64
  )
65
65
  post_process_batch_remove(docs, method)
66
66
  end
@@ -136,7 +136,7 @@ module Mongoid
136
136
  if insertable?
137
137
  collection.find(selector).update_one(
138
138
  positionally(selector, '$set' => { path => inserts }),
139
- session: session
139
+ session: _session
140
140
  )
141
141
  post_process_batch_insert(docs)
142
142
  end
@@ -161,7 +161,7 @@ module Mongoid
161
161
  if insertable?
162
162
  collection.find(selector).update_one(
163
163
  positionally(selector, '$push' => { path => { '$each' => pushes } }),
164
- session: session
164
+ session: _session
165
165
  )
166
166
  post_process_batch_insert(docs)
167
167
  end
@@ -271,6 +271,29 @@ module Mongoid
271
271
  end
272
272
  end
273
273
 
274
+ # Shift documents off the relation. This can be a single document or
275
+ # multiples, and will automatically persist the changes.
276
+ #
277
+ # @example Shift a single document.
278
+ # relation.shift
279
+ #
280
+ # @example Shift multiple documents.
281
+ # relation.shift(3)
282
+ #
283
+ # @param [ Integer ] count The number of documents to shift, or 1 if not
284
+ # provided.
285
+ #
286
+ # @return [ Document, Array<Document> ] The shifted document(s).
287
+ def shift(count = nil)
288
+ if count
289
+ if _target.size > 0 && docs = _target[0, count]
290
+ docs.each { |doc| delete(doc) }
291
+ end
292
+ else
293
+ delete(_target[0])
294
+ end
295
+ end
296
+
274
297
  # Substitutes the supplied target documents for the existing documents
275
298
  # in the relation.
276
299
  #
@@ -526,4 +549,4 @@ module Mongoid
526
549
  end
527
550
  end
528
551
  end
529
- end
552
+ end
@@ -189,8 +189,8 @@ module Mongoid
189
189
 
190
190
  private
191
191
 
192
- def session
193
- _base.send(:session)
192
+ def _session
193
+ _base.send(:_session)
194
194
  end
195
195
 
196
196
  # Find the first object given the supplied attributes or create/initialize it.
@@ -456,7 +456,7 @@ module Mongoid
456
456
  # @since 3.0.0
457
457
  def persist_delayed(docs, inserts)
458
458
  unless docs.empty?
459
- collection.insert_many(inserts, session: session)
459
+ collection.insert_many(inserts, session: _session)
460
460
  docs.each do |doc|
461
461
  doc.new_record = false
462
462
  doc.run_after_callbacks(:create, :save)
@@ -32,7 +32,7 @@ module Mongoid
32
32
  touches = touch_atomic_updates(field)
33
33
  unless touches["$set"].blank?
34
34
  selector = atomic_selector
35
- _root.collection.find(selector).update_one(positionally(selector, touches), session: session)
35
+ _root.collection.find(selector).update_one(positionally(selector, touches), session: _session)
36
36
  end
37
37
  run_callbacks(:touch)
38
38
  true
@@ -53,7 +53,7 @@ module Mongoid
53
53
 
54
54
  private
55
55
 
56
- def session
56
+ def _session
57
57
  Threaded.get_session
58
58
  end
59
59
 
@@ -104,7 +104,7 @@ module Mongoid
104
104
 
105
105
  private
106
106
 
107
- def session
107
+ def _session
108
108
  Threaded.get_session
109
109
  end
110
110
  end
@@ -23,7 +23,7 @@ module Mongoid
23
23
  #
24
24
  # @since 3.0.0
25
25
  def aggregates(field)
26
- result = collection.find.aggregate(pipeline(field), session: session).to_a
26
+ result = collection.find.aggregate(pipeline(field), session: _session).to_a
27
27
  if result.empty?
28
28
  { "count" => 0, "sum" => nil, "avg" => nil, "min" => nil, "max" => nil }
29
29
  else
@@ -166,7 +166,7 @@ module Mongoid
166
166
  validate_out!
167
167
  cmd = command
168
168
  opts = { read: cmd.delete(:read).options } if cmd[:read]
169
- @map_reduce.database.command(cmd, (opts || {}).merge(session: session)).first
169
+ @map_reduce.database.command(cmd, (opts || {}).merge(session: _session)).first
170
170
  end
171
171
  alias :results :raw
172
172
 
@@ -250,8 +250,8 @@ module Mongoid
250
250
  raise Errors::NoMapReduceOutput.new({}) unless @map_reduce.out
251
251
  end
252
252
 
253
- def session
254
- criteria.send(:session)
253
+ def _session
254
+ criteria.send(:_session)
255
255
  end
256
256
  end
257
257
  end
@@ -49,7 +49,7 @@ module Mongoid
49
49
  unless removed.empty?
50
50
  collection.find(selector).update_one(
51
51
  positionally(selector, "$pullAll" => { path => removed }),
52
- session: session
52
+ session: _session
53
53
  )
54
54
  end
55
55
  deleted
@@ -320,7 +320,7 @@ module Mongoid
320
320
  updates["$set"].merge!(doc.atomic_updates["$set"] || {})
321
321
  doc.move_changes
322
322
  end
323
- collection.find(selector).update_one(updates, session: session) unless updates["$set"].empty?
323
+ collection.find(selector).update_one(updates, session: _session) unless updates["$set"].empty?
324
324
  end
325
325
 
326
326
  # Get the limiting value.
@@ -464,8 +464,8 @@ module Mongoid
464
464
 
465
465
  private
466
466
 
467
- def session
468
- @criteria.send(:session)
467
+ def _session
468
+ @criteria.send(:_session)
469
469
  end
470
470
  end
471
471
  end
@@ -341,7 +341,7 @@ module Mongoid
341
341
  @criteria, @klass, @cache = criteria, criteria.klass, criteria.options[:cache]
342
342
  @collection = @klass.collection
343
343
  criteria.send(:merge_type_selection)
344
- @view = collection.find(criteria.selector, session: session)
344
+ @view = collection.find(criteria.selector, session: _session)
345
345
  apply_options
346
346
  end
347
347
 
@@ -708,8 +708,8 @@ module Mongoid
708
708
 
709
709
  private
710
710
 
711
- def session
712
- @criteria.send(:session)
711
+ def _session
712
+ @criteria.send(:_session)
713
713
  end
714
714
 
715
715
  def acknowledged_write?
@@ -35,10 +35,10 @@ module Mongoid
35
35
  key, options = spec.key, default_options.merge(spec.options)
36
36
  if database = options[:database]
37
37
  with(database: database) do |klass|
38
- klass.collection.indexes(session: session).create_one(key, options.except(:database))
38
+ klass.collection.indexes(session: _session).create_one(key, options.except(:database))
39
39
  end
40
40
  else
41
- collection.indexes(session: session).create_one(key, options)
41
+ collection.indexes(session: _session).create_one(key, options)
42
42
  end
43
43
  end and true
44
44
  end
@@ -56,9 +56,9 @@ module Mongoid
56
56
  indexed_database_names.each do |database|
57
57
  with(database: database) do |klass|
58
58
  begin
59
- klass.collection.indexes(session: session).each do |spec|
59
+ klass.collection.indexes(session: _session).each do |spec|
60
60
  unless spec["name"] == "_id_"
61
- klass.collection.indexes(session: session).drop_one(spec["key"])
61
+ klass.collection.indexes(session: _session).drop_one(spec["key"])
62
62
  logger.info(
63
63
  "MONGOID: Removed index '#{spec["name"]}' on collection " +
64
64
  "'#{klass.collection.name}' in database '#{database}'."
@@ -203,7 +203,7 @@ module Mongoid
203
203
  def persist_atomic_operations(operations)
204
204
  if persisted? && operations && !operations.empty?
205
205
  selector = atomic_selector
206
- _root.collection.find(selector).update_one(positionally(selector, operations), session: session)
206
+ _root.collection.find(selector).update_one(positionally(selector, operations), session: _session)
207
207
  end
208
208
  end
209
209
  end
@@ -63,7 +63,7 @@ module Mongoid
63
63
  selector = _parent.atomic_selector
64
64
  _root.collection.find(selector).update_one(
65
65
  positionally(selector, atomic_inserts),
66
- session: session)
66
+ session: _session)
67
67
  end
68
68
  end
69
69
 
@@ -78,7 +78,7 @@ module Mongoid
78
78
  #
79
79
  # @since 4.0.0
80
80
  def insert_as_root
81
- collection.insert_one(as_attributes, session: session)
81
+ collection.insert_one(as_attributes, session: _session)
82
82
  end
83
83
 
84
84
  # Post process an insert, which sets the new record attribute to false
@@ -66,7 +66,7 @@ module Mongoid
66
66
  selector = _parent.atomic_selector
67
67
  _root.collection.find(selector).update_one(
68
68
  positionally(selector, atomic_deletes),
69
- session: session)
69
+ session: _session)
70
70
  end
71
71
  true
72
72
  end
@@ -82,7 +82,7 @@ module Mongoid
82
82
  #
83
83
  # @since 4.0.0
84
84
  def delete_as_root
85
- collection.find(atomic_selector).delete_one(session: session)
85
+ collection.find(atomic_selector).delete_one(session: _session)
86
86
  true
87
87
  end
88
88
 
@@ -134,9 +134,9 @@ module Mongoid
134
134
  unless updates.empty?
135
135
  coll = collection(_root)
136
136
  selector = atomic_selector
137
- coll.find(selector).update_one(positionally(selector, updates), session: session)
137
+ coll.find(selector).update_one(positionally(selector, updates), session: _session)
138
138
  conflicts.each_pair do |key, value|
139
- coll.find(selector).update_one(positionally(selector, { key => value }), session: session)
139
+ coll.find(selector).update_one(positionally(selector, { key => value }), session: _session)
140
140
  end
141
141
  end
142
142
  end
@@ -22,7 +22,7 @@ module Mongoid
22
22
  def upsert(options = {})
23
23
  prepare_upsert(options) do
24
24
  collection.find(atomic_selector).update_one(
25
- as_attributes, upsert: true, session: session)
25
+ as_attributes, upsert: true, session: _session)
26
26
  end
27
27
  end
28
28
 
@@ -58,7 +58,7 @@ module Mongoid
58
58
  #
59
59
  # @since 2.3.2
60
60
  def reload_root_document
61
- {}.merge(collection.find({ _id: _id }, session: session).read(mode: :primary).first || {})
61
+ {}.merge(collection.find({ _id: _id }, session: _session).read(mode: :primary).first || {})
62
62
  end
63
63
 
64
64
  # Reload the embedded document.
@@ -44,7 +44,7 @@ module Mongoid
44
44
  models.each do |model|
45
45
  unless model.embedded?
46
46
  begin
47
- model.collection.indexes(session: model.send(:session)).each do |index|
47
+ model.collection.indexes(session: model.send(:_session)).each do |index|
48
48
  # ignore default index
49
49
  unless index['name'] == '_id_'
50
50
  key = index['key'].symbolize_keys
@@ -77,7 +77,7 @@ module Mongoid
77
77
  indexes.each do |index|
78
78
  key = index['key'].symbolize_keys
79
79
  collection = model.collection
80
- collection.indexes(session: model.send(:session)).drop_one(key)
80
+ collection.indexes(session: model.send(:_session)).drop_one(key)
81
81
  logger.info(
82
82
  "MONGOID: Removed index '#{index['name']}' on collection " +
83
83
  "'#{collection.name}' in database '#{collection.database.name}'."
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "7.0.0"
3
+ VERSION = "7.0.1"
4
4
  end
@@ -2544,6 +2544,25 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
2544
2544
  person.addresses.create(street: "hermannstr")
2545
2545
  end
2546
2546
 
2547
+ context "when the number is zero" do
2548
+
2549
+ let!(:popped) do
2550
+ person.addresses.pop(0)
2551
+ end
2552
+
2553
+ it "returns an empty array" do
2554
+ expect(popped).to eq([])
2555
+ end
2556
+
2557
+ it "does not remove the document from the relation" do
2558
+ expect(person.addresses).to eq([ address_one, address_two ])
2559
+ end
2560
+
2561
+ it "does not persist the pop" do
2562
+ expect(person.reload.addresses).to eq([ address_one, address_two ])
2563
+ end
2564
+ end
2565
+
2547
2566
  context "when the number is not larger than the relation" do
2548
2567
 
2549
2568
  let!(:popped) do
@@ -2601,6 +2620,125 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
2601
2620
  end
2602
2621
  end
2603
2622
 
2623
+ describe "#shift" do
2624
+
2625
+ let(:person) do
2626
+ Person.create
2627
+ end
2628
+
2629
+ context "when no argument is provided" do
2630
+
2631
+ let!(:address_one) do
2632
+ person.addresses.create(street: "sonnenallee")
2633
+ end
2634
+
2635
+ let!(:address_two) do
2636
+ person.addresses.create(street: "hermannstr")
2637
+ end
2638
+
2639
+ let!(:shifted) do
2640
+ person.addresses.shift
2641
+ end
2642
+
2643
+ it "returns the shifted document" do
2644
+ expect(shifted).to eq(address_one)
2645
+ end
2646
+
2647
+ it "removes the document from the relation" do
2648
+ expect(person.addresses).to eq([ address_two ])
2649
+ end
2650
+
2651
+ it "persists the shift" do
2652
+ expect(person.reload.addresses).to eq([ address_two ])
2653
+ end
2654
+ end
2655
+
2656
+ context "when an integer is provided" do
2657
+
2658
+ let!(:address_one) do
2659
+ person.addresses.create(street: "sonnenallee")
2660
+ end
2661
+
2662
+ let!(:address_two) do
2663
+ person.addresses.create(street: "hermannstr")
2664
+ end
2665
+
2666
+ context "when the number is zero" do
2667
+
2668
+ let!(:shifted) do
2669
+ person.addresses.shift(0)
2670
+ end
2671
+
2672
+ it "returns an empty array" do
2673
+ expect(shifted).to eq([])
2674
+ end
2675
+
2676
+ it "does not remove the document from the relation" do
2677
+ expect(person.addresses).to eq([ address_one, address_two ])
2678
+ end
2679
+
2680
+ it "does not persist the shift" do
2681
+ expect(person.reload.addresses).to eq([ address_one, address_two ])
2682
+ end
2683
+ end
2684
+
2685
+ context "when the number is not larger than the relation" do
2686
+
2687
+ let!(:shifted) do
2688
+ person.addresses.shift(2)
2689
+ end
2690
+
2691
+ it "returns the shifted documents" do
2692
+ expect(shifted).to eq([ address_one, address_two ])
2693
+ end
2694
+
2695
+ it "removes the document from the relation" do
2696
+ expect(person.addresses).to be_empty
2697
+ end
2698
+
2699
+ it "persists the shift" do
2700
+ expect(person.reload.addresses).to be_empty
2701
+ end
2702
+ end
2703
+
2704
+ context "when the number is larger than the relation" do
2705
+
2706
+ let!(:shifted) do
2707
+ person.addresses.shift(4)
2708
+ end
2709
+
2710
+ it "returns the shifted documents" do
2711
+ expect(shifted).to eq([ address_one, address_two ])
2712
+ end
2713
+
2714
+ it "removes the document from the relation" do
2715
+ expect(person.addresses).to be_empty
2716
+ end
2717
+
2718
+ it "persists the shift" do
2719
+ expect(person.reload.addresses).to be_empty
2720
+ end
2721
+ end
2722
+ end
2723
+
2724
+ context "when the relation is empty" do
2725
+
2726
+ context "when providing no number" do
2727
+
2728
+ it "returns nil" do
2729
+ expect(person.addresses.shift).to be_nil
2730
+ end
2731
+ end
2732
+
2733
+ context "when providing a number" do
2734
+
2735
+ it "returns nil" do
2736
+ expect(person.addresses.shift(2)).to be_nil
2737
+ end
2738
+ end
2739
+ end
2740
+ end
2741
+
2604
2742
  describe "#scoped" do
2605
2743
 
2606
2744
  let(:person) do
@@ -3788,7 +3926,7 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
3788
3926
  end
3789
3927
  end
3790
3928
 
3791
- context "#delete or #clear with before_remove callback" do
3929
+ context "#delete, or #clear, or #pop, or #shift with before_remove callback" do
3792
3930
 
3793
3931
  let(:artist) do
3794
3932
  Artist.new
@@ -3834,35 +3972,81 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
3834
3972
  end
3835
3973
  end
3836
3974
 
3837
- context "when errors are raised" do
3975
+ describe "#pop" do
3838
3976
 
3839
3977
  before do
3840
- expect(artist).to receive(:before_remove_song).and_raise
3978
+ artist.songs.pop
3841
3979
  end
3842
3980
 
3843
- describe "#delete" do
3981
+ it "executes the callback" do
3982
+ artist.songs.pop
3983
+ expect(artist.before_remove_embedded_called).to be true
3984
+ end
3985
+ end
3844
3986
 
3845
- it "does not remove the document from the relation" do
3846
- begin; artist.songs.delete(song); rescue; end
3847
- expect(artist.songs).to eq([ song ])
3848
- end
3987
+ describe "#shift" do
3988
+
3989
+ before do
3990
+ artist.songs.shift
3849
3991
  end
3850
3992
 
3851
- describe "#clear" do
3993
+ it "executes the callback" do
3994
+ artist.songs.shift
3995
+ expect(artist.before_remove_embedded_called).to be true
3996
+ end
3997
+ end
3998
+ end
3852
3999
 
3853
- before do
3854
- begin; artist.songs.clear; rescue; end
3855
- end
4000
+ context "when errors are raised" do
3856
4001
 
3857
- it "removes the documents from the relation" do
3858
- expect(artist.songs).to eq([ song ])
3859
- end
4002
+ before do
4003
+ expect(artist).to receive(:before_remove_song).and_raise
4004
+ end
4005
+
4006
+ describe "#delete" do
4007
+
4008
+ it "does not remove the document from the relation" do
4009
+ begin; artist.songs.delete(song); rescue; end
4010
+ expect(artist.songs).to eq([ song ])
4011
+ end
4012
+ end
4013
+
4014
+ describe "#clear" do
4015
+
4016
+ before do
4017
+ begin; artist.songs.clear; rescue; end
4018
+ end
4019
+
4020
+ it "removes the documents from the relation" do
4021
+ expect(artist.songs).to eq([ song ])
4022
+ end
4023
+ end
4024
+
4025
+ describe "#pop" do
4026
+
4027
+ before do
4028
+ begin; artist.songs.pop; rescue; end
4029
+ end
4030
+
4031
+ it "should remove from collection" do
4032
+ expect(artist.songs).to eq([ song ])
4033
+ end
4034
+ end
4035
+
4036
+ describe "#shift" do
4037
+
4038
+ before do
4039
+ begin; artist.songs.shift; rescue; end
4040
+ end
4041
+
4042
+ it "should remove from collection" do
4043
+ expect(artist.songs).to eq([ song ])
3860
4044
  end
3861
4045
  end
3862
4046
  end
3863
4047
  end
3864
4048
 
3865
- context "#delete or #clear with after_remove callback" do
4049
+ context "#delete, or #clear, or #pop, or #shift with after_remove callback" do
3866
4050
 
3867
4051
  let(:artist) do
3868
4052
  Artist.new
@@ -3899,6 +4083,30 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
3899
4083
  expect(artist.after_remove_embedded_called).to be true
3900
4084
  end
3901
4085
  end
4086
+
4087
+ describe "#pop" do
4088
+
4089
+ before do
4090
+ artist.labels.pop
4091
+ end
4092
+
4093
+ it "executes the callback" do
4094
+ artist.labels.pop
4095
+ expect(artist.after_remove_embedded_called).to be true
4096
+ end
4097
+ end
4098
+
4099
+ describe "#shift" do
4100
+
4101
+ before do
4102
+ artist.labels.shift
4103
+ end
4104
+
4105
+ it "executes the callback" do
4106
+ artist.labels.shift
4107
+ expect(artist.after_remove_embedded_called).to be true
4108
+ end
4109
+ end
3902
4110
  end
3903
4111
 
3904
4112
  context "when errors are raised" do
@@ -3928,6 +4136,28 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
3928
4136
  expect(artist.labels).to be_empty
3929
4137
  end
3930
4138
  end
4139
+
4140
+ describe "#pop" do
4141
+
4142
+ before do
4143
+ begin; artist.labels.pop; rescue; end
4144
+ end
4145
+
4146
+ it "should remove from collection" do
4147
+ expect(artist.labels).to be_empty
4148
+ end
4149
+ end
4150
+
4151
+ describe "#shift" do
4152
+
4153
+ before do
4154
+ begin; artist.labels.shift; rescue; end
4155
+ end
4156
+
4157
+ it "should remove from collection" do
4158
+ expect(artist.labels).to be_empty
4159
+ end
4160
+ end
3931
4161
  end
3932
4162
  end
3933
4163
 
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: 7.0.0
4
+ version: 7.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -30,7 +30,7 @@ cert_chain:
30
30
  fxN/GvhskJEgmdQToxEBRLOu5/udtPpVe/hb3gk5hzsxcWuKN/VTi4SbtFQdz9cq
31
31
  fqd6ctFDOqcJmOYdlSRgb9g8zm4BiNgFWPBSk8NsP7c=
32
32
  -----END CERTIFICATE-----
33
- date: 2018-03-12 00:00:00.000000000 Z
33
+ date: 2018-05-04 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: activemodel
metadata.gz.sig CHANGED
Binary file