Skip to content

Commit 6eb9163

Browse files
committed
continuing to work correct solution to #173
1 parent 7eba37b commit 6eb9163

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/base.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ def saving!
310310
@saving = true
311311
end
312312

313-
def errors!(hash)
313+
def errors!(hash, saving)
314+
@errors_at_last_sync = hash if saving
314315
notify_waiting_for_save
315316
errors.clear && return unless hash
316317
hash.each do |attribute, messages|
@@ -320,6 +321,11 @@ def errors!(hash)
320321
end
321322
end
322323

324+
def revert_errors!
325+
puts "#{inspect}.revert_errors! @errors_at_last_sync: #{@errors_at_last_sync}"
326+
errors!(@errors_at_last_sync)
327+
end
328+
323329
def saved!(save_only = nil) # sets saving to false AND notifies
324330
notify_waiting_for_save
325331
return self if save_only

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/isomorphic_base.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,17 @@ def save_or_validate(save, validate, force, &block)
264264
HyperMesh.load do
265265
ReactiveRecord.loads_pending! unless self.class.pending_fetches.empty?
266266
end.then { send_save_to_server(save, validate, force, &block) }
267-
#save_to_server(validate, force, &block)
268267
else
268+
if validate
269+
# Handles the case where a model is valid, then some attribute is
270+
# updated, and model.validate is called updating the error data.
271+
# Now lets say the attribute changes back to the last synced value. In
272+
# this case we need to revert the error records.
273+
models, _, backing_records = self.class.gather_records([self], true, self)
274+
models.each do |item|
275+
backing_records[item[:id]].revert_errors!
276+
end
277+
end
269278
promise = Promise.new
270279
yield true, nil, [] if block
271280
promise.resolve({success: true})
@@ -302,7 +311,7 @@ def send_save_to_server(save, validate, force, &block)
302311

303312
response[:saved_models].each do | item |
304313
backing_records[item[0]].sync_unscoped_collection! if save
305-
backing_records[item[0]].errors! item[3]
314+
backing_records[item[0]].errors! item[3], save
306315
end
307316

308317
yield response[:success], response[:message], response[:models] if block

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/setters.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ def change_status_and_notify_helper(attr, changed)
120120
# || data_loading? added so that model.new can be wrapped in a ReactiveRecord.load_data
121121
if !changed || data_loading?
122122
changed_attributes.delete(attr)
123-
else
124-
changed_attributes << attr if !changed_attributes.include?(attr)
125-
synced_attributes.delete(attr)
123+
elsif !changed_attributes.include?(attr)
124+
changed_attributes << attr
126125
end
127126
yield @attributes.key?(attr), @attributes[attr]
128127
return unless empty_before != changed_attributes.empty?

0 commit comments

Comments
 (0)