mongoid-denormalize 0.1.0 → 0.2.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: cecaa0d338d86451e2d2808712767f0e6862aed4
4
- data.tar.gz: 53e285e91e43bd2cedce3430fcf49fed0f953a63
3
+ metadata.gz: 89273ea3cdebd9a638a98696f6ab82c479eee5c3
4
+ data.tar.gz: fde78762ab929df91264df24ed978c656e3deb23
5
5
  SHA512:
6
- metadata.gz: 4de17b1f7116ea41d2593dafa284e11a3c41058a8b35ea8a3dcbee2cf73f23a3732028f50764dd2703ac485165842de14634eab8cb14fb89a485dd538a680e5f
7
- data.tar.gz: 6e944fc4822da56b58d2e7a582750e2a980d67dad51a194cc1898eb644fe336792ae49b9385b2d60b7d2ff96d6a513d27123fa879875e75c730802e6a6771a59
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 `has_many` target name is diferent from model:
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
 
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Denormalize
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -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
- model_class = model_name.name
31
- inverse_of = relations[from].inverse_of || model_name.route_key.pluralize
30
+ child_model_name = model_name
31
+ child_inverse_of = relations[from].inverse_of
32
32
 
33
- # When 'from' is updated, update all childs
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
- unless relations[inverse_of.to_s]
39
- raise "Option :inverse_of is needed for 'belongs_to :#{from}' into #{model_class}."
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
- send(inverse_of).update_all('$set' => attributes)
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.1.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-10-15 00:00:00.000000000 Z
11
+ date: 2017-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler