mongoid_monkey 0.2.4 → 0.2.5

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: 5ca29354de5f6ae28af0f6d5ae2f4eb72ed6fdc5
4
- data.tar.gz: 8f1d232f62817858c7a6a6631eb9371cb15bc580
3
+ metadata.gz: 3993c5f5fbdf5f1f30b9a824ffbd3447772bf68d
4
+ data.tar.gz: 77026f83baf29fd6ebbcc63c7487818d3b0e081c
5
5
  SHA512:
6
- metadata.gz: 6fd84e25a8a1bce569497e2cbef12225c6c4d0a86ce90f8a3cf2be81c0c20e6a99efea9dcb3abf7ff18c502da4f83eae57d5ab06a49640a5e866b9a688cf3dbf
7
- data.tar.gz: cad54a66f5a126c469a26330d8415a5d6cf8b0da0b3ee13b6bbb4217ad43827a0e388256146e94ae147584875f3675b199928e003b0e264dd5517ec52b7eca3a
6
+ metadata.gz: a64eee10db5554f9f068522d8d571a7d42ffeabf1b14b84df8b8052eb1730311cc6f4964dd4bbfead35bbd24f078f68f8cd597a56d328993e26f11563a01bb6f
7
+ data.tar.gz: 935a41fc23448476f511e49fba9fea2fd7e546625758bb26e78c6f7f2c784fedaae1392158dd2417701e0afd7551fa535264ff7d84d425e478b8d0ac9cebd77a
data/README.md CHANGED
@@ -37,7 +37,8 @@ If you would only like some of the patches, please copy and paste the code to yo
37
37
  | `instrument.rb` | Backport instrumentation change to Moped 1. | ● | ○ | ○ |
38
38
  | `reorder.rb` | Backport `Criteria#reorder` method from Mongoid 4. | ● | ○ | ○ |
39
39
  | `only_pluck_localized.rb` | Backport [PR #4299](https://github.com/mongodb/mongoid/pull/4299) from Mongoid 6 which fixes `#only`, `#without`, and `#pluck` with localized fields. | ● | × | × |
40
- | `embedded_touch.rb` | Backport [Issue #3310](https://github.com/mongodb/mongoid/commit/a94c2f43573e58f973913c881ad9d11d62bf857c) from Mongoid 4 to add `:touch` option to `embedded_in`. | ● | ○ | ○ |
40
+ | `embedded_touch.rb` (1) | Backport [Issue #3310](https://github.com/mongodb/mongoid/commit/a94c2f43573e58f973913c881ad9d11d62bf857c) from Mongoid 4 to add `:touch` option to `embedded_in`. | ● | ○ | ○ |
41
+ | `embedded_touch.rb` (2) | Backport [PR #4392](https://github.com/mongodb/mongoid/pull/4392) from Mongoid 6 to fix an infinite loop issue related to `:touch` callbacks. | ● | ● | ● |
41
42
  | `index_options.rb` | Backport latest index valid index options from Mongoid 6. | ● | ● | ○ |
42
43
 
43
44
  ### License
@@ -1,37 +1,96 @@
1
- # Backport Mongoid 4 :touch option for #embedded_in to Mongoid 3.
1
+ # Backport support #embedded_in :touch option from Mongoid 4 to Mongoid 3.
2
+ # Also support touch callback on update, and fix infinite loop issue.
2
3
 
3
4
  if Mongoid::VERSION =~ /\A3\./
4
5
 
5
6
  module Mongoid
6
- module Relations
7
- module Embedded
8
- class In < Relations::One
9
- class << self
10
- def valid_options
11
- [ :autobuild, :cyclic, :polymorphic, :touch ]
7
+ module Relations
8
+ module Embedded
9
+ class In < Relations::One
10
+ class << self
11
+ def valid_options
12
+ [ :autobuild, :cyclic, :polymorphic, :touch ]
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ module Macros
19
+ module ClassMethods
20
+
21
+ def embedded_in(name, options = {}, &block)
22
+ if ancestors.include?(Mongoid::Versioning)
23
+ raise Errors::VersioningNotOnRoot.new(self)
24
+ end
25
+ meta = characterize(name, Embedded::In, options, &block)
26
+ self.embedded = true
27
+ relate(name, meta)
28
+ builder(name, meta).creator(name, meta)
29
+ touchable(meta)
30
+ add_counter_cache_callbacks(meta) if meta.counter_cached?
31
+ meta
12
32
  end
13
33
  end
14
34
  end
15
- end
16
35
 
17
- module Macros
18
- module ClassMethods
36
+ module Touchable
37
+ module ClassMethods
19
38
 
20
- def embedded_in(name, options = {}, &block)
21
- if ancestors.include?(Mongoid::Versioning)
22
- raise Errors::VersioningNotOnRoot.new(self)
39
+ def touchable(metadata)
40
+ if metadata.touchable?
41
+ name = metadata.name
42
+ method_name = define_relation_touch_method(name)
43
+ after_save method_name
44
+ after_destroy method_name
45
+ after_touch method_name
46
+ end
47
+ self
23
48
  end
24
- meta = characterize(name, Embedded::In, options, &block)
25
- self.embedded = true
26
- relate(name, meta)
27
- builder(name, meta).creator(name, meta)
28
- touchable(meta)
29
- add_counter_cache_callbacks(meta) if meta.counter_cached?
30
- meta
31
49
  end
32
50
  end
33
51
  end
52
+
53
+ module Callbacks
54
+ def cascadable_children(kind, children = Set.new)
55
+ embedded_relations.each_pair do |name, metadata|
56
+ next unless metadata.cascading_callbacks?
57
+ without_autobuild do
58
+ delayed_pulls = delayed_atomic_pulls[name]
59
+ delayed_unsets = delayed_atomic_unsets[name]
60
+ children.merge(delayed_pulls) if delayed_pulls
61
+ children.merge(delayed_unsets) if delayed_unsets
62
+ relation = send(name)
63
+ Array.wrap(relation).each do |child|
64
+ next if children.include?(child)
65
+ children.add(child) if cascadable_child?(kind, child, metadata)
66
+ child.send(:cascadable_children, kind, children)
67
+ end
68
+ end
69
+ end
70
+ children.to_a
71
+ end
72
+
73
+ def cascadable_child?(kind, child, metadata)
74
+ return false if kind == :initialize || kind == :find || kind == :touch
75
+ return false if kind == :validate && metadata.validate?
76
+ child.callback_executable?(kind) ? child.in_callback_state?(kind) : false
77
+ end
78
+ end
34
79
  end
80
+
81
+ end
82
+
83
+ if Mongoid::VERSION =~ /\A[45]\./
84
+
85
+ module Mongoid
86
+
87
+ module Interceptable
88
+ def cascadable_child?(kind, child, metadata)
89
+ return false if kind == :initialize || kind == :find || kind == :touch
90
+ return false if kind == :validate && metadata.validate?
91
+ child.callback_executable?(kind) ? child.in_callback_state?(kind) : false
92
+ end
93
+ end
35
94
  end
36
95
 
37
96
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module MongoidMonkey
2
- VERSION = '0.2.4'
2
+ VERSION = '0.2.5'
3
3
  end
@@ -0,0 +1,6 @@
1
+ class Book
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+
5
+ embeds_many :pages, cascade_callbacks: true
6
+ end
@@ -0,0 +1,5 @@
1
+ class Edit
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps::Updated
4
+ embedded_in :wiki_page, touch: true
5
+ end
@@ -0,0 +1,6 @@
1
+ class Page
2
+ include Mongoid::Document
3
+
4
+ embedded_in :book, touch: true
5
+ field :content, :type => String
6
+ end
@@ -0,0 +1,8 @@
1
+ class WikiPage
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+
5
+ field :title, type: String
6
+
7
+ embeds_many :edits, validate: false
8
+ end
@@ -1,21 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- if Mongoid::VERSION =~ /\A3\./
4
-
5
- class Edit
6
- include Mongoid::Document
7
- include Mongoid::Timestamps::Updated
8
- embedded_in :wiki_page, touch: true
9
- end
10
-
11
- class WikiPage
12
- include Mongoid::Document
13
- include Mongoid::Timestamps
14
-
15
- field :title, type: String
16
-
17
- embeds_many :edits, validate: false
18
- end
3
+ if Mongoid::VERSION =~ /\A[345]\./
19
4
 
20
5
  describe Mongoid::Relations::Embedded::In do
21
6
 
@@ -48,5 +33,43 @@ if Mongoid::VERSION =~ /\A3\./
48
33
  expect(page.updated_at).to be_within(5).of(Time.now)
49
34
  end
50
35
  end
36
+
37
+ context "when the parent of embedded doc has cascade callbacks" do
38
+
39
+ let!(:book) do
40
+ Book.new
41
+ end
42
+
43
+ before do
44
+ book.pages.new
45
+ book.save
46
+ book.unset(:updated_at)
47
+ book.pages.first.touch
48
+ end
49
+
50
+ it "touches the parent document" do
51
+ expect(book.updated_at).to be_within(5).of(Time.now)
52
+ end
53
+ end
54
+
55
+ context "when multiple embedded docs with cascade callbacks" do
56
+
57
+ let!(:book) do
58
+ Book.new
59
+ end
60
+
61
+ before do
62
+ 2.times { book.pages.new }
63
+ book.save
64
+ book.unset(:updated_at)
65
+ book.pages.first.content = "foo"
66
+ book.pages.second.content = "bar"
67
+ book.pages.first.touch
68
+ end
69
+
70
+ it "touches the parent document" do
71
+ expect(book.updated_at).to be_within(5).of(Time.now)
72
+ end
73
+ end
51
74
  end
52
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_monkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - johnnyshields
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-21 00:00:00.000000000 Z
11
+ date: 2017-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -74,10 +74,14 @@ files:
74
74
  - lib/version.rb
75
75
  - spec/app/models/account.rb
76
76
  - spec/app/models/address.rb
77
+ - spec/app/models/book.rb
77
78
  - spec/app/models/draft.rb
79
+ - spec/app/models/edit.rb
78
80
  - spec/app/models/name.rb
81
+ - spec/app/models/page.rb
79
82
  - spec/app/models/person.rb
80
83
  - spec/app/models/user.rb
84
+ - spec/app/models/wiki_page.rb
81
85
  - spec/config/mongodb-mmapv1.conf
82
86
  - spec/config/mongodb-wiredtiger.conf
83
87
  - spec/gemfile/mongoid3.gemfile
@@ -144,10 +148,14 @@ summary: Monkey patches for Mongoid
144
148
  test_files:
145
149
  - spec/app/models/account.rb
146
150
  - spec/app/models/address.rb
151
+ - spec/app/models/book.rb
147
152
  - spec/app/models/draft.rb
153
+ - spec/app/models/edit.rb
148
154
  - spec/app/models/name.rb
155
+ - spec/app/models/page.rb
149
156
  - spec/app/models/person.rb
150
157
  - spec/app/models/user.rb
158
+ - spec/app/models/wiki_page.rb
151
159
  - spec/config/mongodb-mmapv1.conf
152
160
  - spec/config/mongodb-wiredtiger.conf
153
161
  - spec/gemfile/mongoid3.gemfile