mongoid-denormalize 0.1.0 → 0.2.0
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.
- checksums.yaml +4 -4
- data/README.md +1 -2
- data/lib/mongoid-denormalize/version.rb +1 -1
- data/lib/mongoid-denormalize.rb +18 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89273ea3cdebd9a638a98696f6ab82c479eee5c3
|
4
|
+
data.tar.gz: fde78762ab929df91264df24ed978c656e3deb23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3a815fc670066d01bad8a47ee1e37353d07c74d1c193d46c6439e53dc5fb2771706c421ff0d8752ebe8376d3fe39aa1ad1057c981ff937c79121b400f3e43c5
|
7
|
+
data.tar.gz: ef2a98a526e9f07d57e934906a224ec13761b016f26280e1acf10220974da66d01bf59285e15d7aced196227c753b7c9d7af4f36298a0079da92fca4c839a770
|
data/README.md
CHANGED
@@ -31,7 +31,7 @@ include Mongoid::Denormalize
|
|
31
31
|
denormalize :name, :email, from: :user
|
32
32
|
```
|
33
33
|
|
34
|
-
You need to add `inverse_of` to `belongs_to` side if
|
34
|
+
You need to add `inverse_of` to `belongs_to` side if relation name is diferent from model:
|
35
35
|
|
36
36
|
```ruby
|
37
37
|
class Club
|
@@ -52,7 +52,6 @@ end
|
|
52
52
|
```ruby
|
53
53
|
class User
|
54
54
|
include Mongoid::Document
|
55
|
-
include Mongoid::Denormalize
|
56
55
|
|
57
56
|
field :name
|
58
57
|
|
data/lib/mongoid-denormalize.rb
CHANGED
@@ -27,19 +27,31 @@ module Mongoid
|
|
27
27
|
end
|
28
28
|
|
29
29
|
from_class = (relations[from].class_name || relations[from].name.capitalize).constantize
|
30
|
-
|
31
|
-
|
30
|
+
child_model_name = model_name
|
31
|
+
child_inverse_of = relations[from].inverse_of
|
32
32
|
|
33
|
-
# When 'from' is updated, update
|
33
|
+
# When 'from' is updated, update child/childs
|
34
34
|
from_class.send(:after_update) do
|
35
35
|
attributes = {}
|
36
36
|
args.each { |field| attributes["#{from}_#{field}"] = send(field) }
|
37
37
|
|
38
|
-
|
39
|
-
|
38
|
+
relation = relations[child_inverse_of.to_s] ||
|
39
|
+
relations[child_model_name.plural] ||
|
40
|
+
relations[child_model_name.singular]
|
41
|
+
|
42
|
+
unless relation
|
43
|
+
raise "Option :inverse_of is needed for 'belongs_to :#{from}' into #{child_model_name.name}."
|
40
44
|
end
|
41
45
|
|
42
|
-
|
46
|
+
case relation.relation.to_s
|
47
|
+
when 'Mongoid::Relations::Referenced::One'
|
48
|
+
document = send(relation.name)
|
49
|
+
document.collection.update_one({_id: document._id}, {'$set' => attributes}) if document
|
50
|
+
when 'Mongoid::Relations::Referenced::Many'
|
51
|
+
send(relation.name).update_all('$set' => attributes)
|
52
|
+
else
|
53
|
+
raise "Relation type unsupported: #{relation.relation}"
|
54
|
+
end
|
43
55
|
end
|
44
56
|
end
|
45
57
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-denormalize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rjurado01
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|