ar_sync 1.0.0 → 1.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: '01944ab2f90f657d412a17a52cb72eb249b5a3a96ca0531f273c1045900aae92'
4
- data.tar.gz: 5f64eb0b5c66b24f886b693b9bee94a4f4ba95661768af4526fc3eda9be63d07
3
+ metadata.gz: c79d18d97624ed5b34f6471838d6b96eb6cfb71bba00cee9344a370dbb309498
4
+ data.tar.gz: c967bed5ff59000fcc8c1de827e98f1b8caef20d71d67532d8203eff4d97724a
5
5
  SHA512:
6
- metadata.gz: 1305699ff98f51f6f8bb63bc8724c022c65475036b533b865fa48eabe4353c29805653c1204c81a1ee51a2e3d8f06de37b75eaabb3e7baa8b2f5dd0a9b04e08e
7
- data.tar.gz: 7a25e40259ee1ff0baa93f783dddb1a9af36cab92ecd8686b2f22885022004aaf716e18a004cb6a2c4b44e15ae3bb9216e1f7c7def0f59920eed77ef4e062f5a
6
+ metadata.gz: 5966a10dfdce14e70e2df1a0d722889db8ff4bece153bf4d1bd73a8148dd6f79cb4581e254a26deed31c2fe09acbdcd9333cdbf6616ec8d892bfc0758ccd44e3
7
+ data.tar.gz: 87b53c9634f5afd4b4a1a708ce128d094290d3e5db9a24b70146dc4fc96091760efcdb5b8edf851f3ce83afb13f85e464e556d2c00890c4b36fa852871908d7a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ar_sync (1.0.0)
4
+ ar_sync (1.0.1)
5
5
  activerecord
6
6
  ar_serializer
7
7
 
@@ -124,12 +124,12 @@ module ArSync::GraphSync::ClassMethods
124
124
  end
125
125
 
126
126
  def sync_has_many(name, **option, &data_block)
127
- _sync_children_info[name] = :many
127
+ _sync_children_info[name] = [:many, option, data_block]
128
128
  _sync_has_many name, option, &data_block
129
129
  end
130
130
 
131
131
  def sync_has_one(name, **option, &data_block)
132
- _sync_children_info[name] = :one
132
+ _sync_children_info[name] = [:one, option, data_block]
133
133
  _sync_define name, option, &data_block
134
134
  end
135
135
 
@@ -179,12 +179,13 @@ module ArSync::GraphSync::ClassMethods
179
179
  def write_attribute(attr_name, value)
180
180
  self.class.default_scoped.scoping do
181
181
  @_sync_parents_info_before_mutation ||= _sync_current_parents_info
182
+ @_sync_belongs_to_info_before_mutation ||= _sync_current_belongs_to_info
182
183
  end
183
184
  super attr_name, value
184
185
  end
185
186
  end
186
187
  prepend mod
187
- attr_reader :_sync_parents_info_before_mutation
188
+ attr_reader :_sync_parents_info_before_mutation, :_sync_belongs_to_info_before_mutation
188
189
 
189
190
  _sync_define :id
190
191
 
@@ -198,10 +199,12 @@ module ArSync::GraphSync::ClassMethods
198
199
 
199
200
  before_destroy do
200
201
  @_sync_parents_info_before_mutation ||= _sync_current_parents_info
202
+ @_sync_belongs_to_info_before_mutation ||= _sync_current_belongs_to_info
201
203
  end
202
204
 
203
205
  before_save on: :create do
204
206
  @_sync_parents_info_before_mutation ||= _sync_current_parents_info
207
+ @_sync_belongs_to_info_before_mutation ||= _sync_current_belongs_to_info
205
208
  end
206
209
 
207
210
  %i[create update destroy].each do |action|
@@ -209,6 +212,7 @@ module ArSync::GraphSync::ClassMethods
209
212
  next if ArSync.skip_notification?
210
213
  self.class.default_scoped.scoping { _sync_notify action }
211
214
  @_sync_parents_info_before_mutation = nil
215
+ @_sync_belongs_to_info_before_mutation = nil
212
216
  end
213
217
  end
214
218
  end
@@ -82,6 +82,35 @@ module ArSync::GraphSync::InstanceMethods
82
82
  parents
83
83
  end
84
84
 
85
+ def _serializer_field_value(name)
86
+ field = self.class._serializer_field_info name
87
+ preloadeds = field.preloaders.map do |preloader|
88
+ args = [[self], nil, {}]
89
+ preloader.call(*(preloader.arity < 0 ? args : args.take(preloader.arity)))
90
+ end
91
+ instance_exec(*preloadeds, nil, {}, &field.data_block)
92
+ end
93
+
94
+ def _sync_current_belongs_to_info
95
+ belongs = {}
96
+ self.class._each_sync_child do |name, (type, option, data_block)|
97
+ next unless type == :one
98
+ option ||= {}
99
+ association_name = (option[:association] || name).to_s.underscore
100
+ association = self.class.reflect_on_association association_name
101
+ next if association && !association.belongs_to?
102
+ if association && !option[:preload] && !data_block
103
+ belongs[name] = {
104
+ type: association.foreign_type && self[association.foreign_type],
105
+ id: self[association.foreign_key]
106
+ }
107
+ else
108
+ belongs[name] = { value: _serializer_field_value(name) }
109
+ end
110
+ end
111
+ belongs
112
+ end
113
+
85
114
  def _sync_notify_parent(action)
86
115
  if action == :create
87
116
  parents = _sync_current_parents_info
@@ -125,6 +154,14 @@ module ArSync::GraphSync::InstanceMethods
125
154
  end
126
155
 
127
156
  def _sync_notify_self
157
+ belongs_was = _sync_belongs_to_info_before_mutation
158
+ belongs = _sync_current_belongs_to_info
159
+ belongs.each do |name, info|
160
+ next if belongs_was[name] == info
161
+ value = info.key?(:value) ? info[:value] : _serializer_field_value(name)
162
+ _sync_notify_child_added value, name, nil, true if value.is_a? ArSerializer::Serializable
163
+ _sync_notify_child_removed value, name, nil, true if value.nil?
164
+ end
128
165
  ArSync.sync_graph_send(to: self, action: :update, model: self)
129
166
  end
130
167
  end
@@ -1,3 +1,3 @@
1
1
  module ArSync
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -209,7 +209,7 @@ class ArSyncRecord extends ArSyncContainerBase {
209
209
  collection.parentKey = aliasName
210
210
  }
211
211
  } else {
212
- if (subQuery.attributes) this.paths.push(key);
212
+ if (subQuery.attributes && Object.keys(subQuery.attributes).length > 0) this.paths.push(key);
213
213
  if (subData && subData.sync_keys) {
214
214
  if (this.children[aliasName]) {
215
215
  this.children[aliasName].replaceData(subData)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - tompng
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-12 00:00:00.000000000 Z
11
+ date: 2019-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord