mongoid_relations_dirty_tracking 0.2.1 → 0.2.2

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: a9837bae013e0fe89a8bb531757c816d1dc38b7e31403f9643586118b5521aaa
4
- data.tar.gz: 9647a270f2a5f0ffd21de9ea56fcc731c9dc2a6a82fd926480f4501dd63757fa
3
+ metadata.gz: 44359853dbe1f4805290ec30672bcf1c77fa3a8b375a68d498e357da29ba4ecb
4
+ data.tar.gz: 28b569c12cee5b59c80ae45357ee7163f2f31cc2cb38c2ab1ca381537b721ce1
5
5
  SHA512:
6
- metadata.gz: 8ec976a88682181f89c9530a5b52e58a0a35458353251d6fc696d15c05130c416a56683345511b81d6d7f34f69c4f76f7451ffec5dd5e83eb0f61118bcbc4ecc
7
- data.tar.gz: 106958a9cebef15a89312a5aab3d580f7f0131145b9968507dfa3471eb8d52fa50b191af03609b78e0c6c3d6b23da80e8bb21c32385b915f60ed22abf2785915
6
+ metadata.gz: c82ea278398bbce4402d08e8c87c2dc9f8afe5e253427a09afaa6604fc3888ac9ae2ab309f329d61c58bdb1931f91430abe2bc3b72ca62cd204901dcd7732fc3
7
+ data.tar.gz: 79dbc5a60ddcf8223b8623b1953483695cfef6920d6cb5c03042590e92a93aceb8fc792955f472904537650c38748d59467ca01ec72cb1131e7ced408fc1bbec
data/README.md CHANGED
@@ -47,11 +47,34 @@ doc.changed_with_relations? # => false
47
47
  doc.changes_with_relations # => {}
48
48
  ```
49
49
 
50
- ### Global Disable
50
+ ## Disablement
51
51
 
52
- Relations dirty tracking can be resource intensive.
53
- You may disable it for a block scope. This is thread-safe
54
- and will use the [RequestStore gem](https://github.com/steveklabnik/request_store)
52
+ Relations dirty tracking can be resource intensive,
53
+ hence you may wish to disable it dynamically.
54
+
55
+ #### When using .only?
56
+
57
+ If you query objects with the `.only?(*fields)` modifier, Mongoid
58
+ itself marks the objects as `readonly` and attempts to save them will fail.
59
+ In this case, relations dirty tracking is automatically disabled.
60
+
61
+ ```ruby
62
+ Mongoid::RelationsDirtyTracking.disable do
63
+ doc = SampleDocument.all.only(:title).first
64
+ doc.foo = Foo.new(title: 'foo')
65
+ doc.bars << Bar.new(title: 'bar')
66
+
67
+ doc.relations_changed? # => false
68
+ doc.relation_changes # => {}
69
+
70
+ doc.save! #=> raises Mongoid::Errors::ReadonlyDocument
71
+ end
72
+ ```
73
+
74
+ #### Within a block scope
75
+
76
+ You may disable relations dirty tracking for a given block.
77
+ This is thread-safe and will use the [RequestStore gem](https://github.com/steveklabnik/request_store)
55
78
  if it is included in your project.
56
79
 
57
80
  ```ruby
@@ -60,8 +83,8 @@ Mongoid::RelationsDirtyTracking.disable do
60
83
  doc.foo = Foo.new(title: 'foo')
61
84
  doc.bars << Bar.new(title: 'bar')
62
85
 
63
- doc.relations_changed? # => false
64
- doc.relation_changes # => {}
86
+ doc.relations_changed? # => false
87
+ doc.relation_changes # => {}
65
88
  end
66
89
  ```
67
90
 
@@ -49,6 +49,7 @@ module Mongoid
49
49
  Mongoid::Association::Referenced::HasMany::Proxy,
50
50
  Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy,
51
51
  Mongoid::Association::Referenced::BelongsTo::Proxy]
52
+
52
53
  to_track && trackables.include?(relations[rel_name].try(:relation))
53
54
  end
54
55
 
@@ -66,14 +67,14 @@ module Mongoid
66
67
 
67
68
  def store_relations_shadow
68
69
  @relations_shadow = {}
69
- return unless Mongoid::RelationsDirtyTracking.enabled?
70
+ return if readonly? || !Mongoid::RelationsDirtyTracking.enabled?
70
71
  self.class.tracked_relations.each do |rel_name|
71
72
  @relations_shadow[rel_name] = tracked_relation_attributes(rel_name)
72
73
  end
73
74
  end
74
75
 
75
76
  def relation_changes
76
- return {} unless Mongoid::RelationsDirtyTracking.enabled?
77
+ return {} if readonly? || !Mongoid::RelationsDirtyTracking.enabled?
77
78
  changes = {}
78
79
  @relations_shadow.each_pair do |rel_name, shadow_values|
79
80
  current_values = tracked_relation_attributes(rel_name)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Mongoid
4
4
  module RelationsDirtyTracking
5
- VERSION = '0.2.1'
5
+ VERSION = '0.2.2'
6
6
  end
7
7
  end
@@ -3,7 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Mongoid::RelationsDirtyTracking do
6
- subject { TestDocument.create }
6
+ subject(:doc) { TestDocument.create }
7
7
 
8
8
  its(:changed?) { is_expected.to eq false }
9
9
  its(:children_changed?) { is_expected.to eq false }
@@ -372,26 +372,49 @@ describe Mongoid::RelationsDirtyTracking do
372
372
  end
373
373
  end
374
374
 
375
+ describe 'disable if readonly?' do
376
+ subject do
377
+ doc
378
+ TestDocument.all.only(:one_related_id).first
379
+ end
380
+
381
+ before :each do
382
+ @embedded_doc = TestEmbeddedDocument.new
383
+
384
+ subject.many_documents << @embedded_doc
385
+ end
386
+
387
+ its(:changed?) { is_expected.to eq false }
388
+ its(:children_changed?) { is_expected.to eq false }
389
+ its(:relations_changed?) { is_expected.to eq false }
390
+ its(:changed_with_relations?) { is_expected.to eq false }
391
+ its(:changes_with_relations) { is_expected.to_not include(subject.relation_changes) }
392
+
393
+ describe '#relation_changes' do
394
+ it 'returns array with differences' do
395
+ expect(subject.relation_changes['many_documents']).to eq(nil)
396
+ end
397
+ end
398
+ end
399
+
375
400
  describe 'global disablement' do
376
- context 'when adding document' do
377
- before :each do
378
- @embedded_doc = TestEmbeddedDocument.new
401
+ before :each do
402
+ @embedded_doc = TestEmbeddedDocument.new
379
403
 
380
- described_class.disable do
381
- subject.many_documents << @embedded_doc
382
- end
404
+ described_class.disable do
405
+ subject.many_documents << @embedded_doc
383
406
  end
407
+ end
384
408
 
385
- its(:changed?) { is_expected.to eq false }
386
- its(:children_changed?) { is_expected.to eq false }
387
- its(:relations_changed?) { is_expected.to eq false }
388
- its(:changed_with_relations?) { is_expected.to eq false }
389
- its(:changes_with_relations) { is_expected.to_not include(subject.relation_changes) }
409
+ its(:changed?) { is_expected.to eq false }
410
+ its(:children_changed?) { is_expected.to eq false }
411
+ its(:relations_changed?) { is_expected.to eq false }
412
+ its(:changed_with_relations?) { is_expected.to eq false }
413
+ its(:changes_with_relations) { is_expected.to_not include(subject.relation_changes) }
390
414
 
391
- describe '#relation_changes' do
392
- it 'returns array with differences' do
393
- expect(subject.relation_changes['many_documents']).to eq(nil)
394
- end
415
+ describe '#relation_changes' do
416
+ it 'returns array with differences' do
417
+ expect(subject.relation_changes['many_documents']).to eq(nil)
395
418
  end
396
419
  end
397
420
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_relations_dirty_tracking
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Sevcik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-11 00:00:00.000000000 Z
11
+ date: 2020-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport