reactive-record 0.7.36 → 0.7.37
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b36c2c16110af0053cb7aae2d944d968f53874f7
|
4
|
+
data.tar.gz: 2d2c5b526e8f1fa9e95573abcd582c841141071e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3efaf62ea05058f8324d078aadc0f72ab40e8da49574767321705458337cb46307cd89117352d454bbc18d3d28c881f96cc3569d59f2dc3b469ef338e76ff53f
|
7
|
+
data.tar.gz: 4f3ee324e78faad0e77b5d2647a8025267608784a79e13cc85b81b5b21fb13a85e714115933f71dc58abdf1388684410e24ef0d90e33f73c5c5e3aa43edd76f1
|
@@ -371,154 +371,148 @@ module ReactiveRecord
|
|
371
371
|
end
|
372
372
|
|
373
373
|
def self.save_records(models, associations, acting_user, validate, save)
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
374
|
+
reactive_records = {}
|
375
|
+
vectors = {}
|
376
|
+
new_models = []
|
377
|
+
saved_models = []
|
378
|
+
dont_save_list = []
|
379
|
+
|
380
|
+
models.each do |model_to_save|
|
381
|
+
attributes = model_to_save[:attributes]
|
382
|
+
model = Object.const_get(model_to_save[:model])
|
383
|
+
id = attributes.delete(model.primary_key) if model.respond_to? :primary_key # if we are saving existing model primary key value will be present
|
384
|
+
vector = model_to_save[:vector]
|
385
|
+
vector = [vector[0].constantize] + vector[1..-1].collect do |method|
|
386
|
+
if method.is_a?(Array) and method.first == "find_by_id"
|
387
|
+
["find", method.last]
|
388
|
+
else
|
389
|
+
method
|
390
|
+
end
|
391
|
+
end
|
392
|
+
reactive_records[model_to_save[:id]] = vectors[vector] = record = find_record(model, id, vector, save)
|
393
|
+
if record and record.respond_to?(:id) and record.id
|
394
|
+
# we have an already exising activerecord model
|
395
|
+
keys = record.attributes.keys
|
396
|
+
attributes.each do |key, value|
|
397
|
+
if keys.include? key
|
398
|
+
record[key] = value
|
399
|
+
elsif !value.nil? and aggregation = record.class.reflect_on_aggregation(key.to_sym) and !(aggregation.klass < ActiveRecord::Base)
|
400
|
+
aggregation.mapping.each_with_index do |pair, i|
|
401
|
+
record[pair.first] = value[i]
|
394
402
|
end
|
403
|
+
elsif record.respond_to? "#{key}="
|
404
|
+
record.send("#{key}=",value)
|
405
|
+
else
|
406
|
+
# TODO once reading schema.rb on client is implemented throw an error here
|
395
407
|
end
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
elsif record.respond_to? "#{key}="
|
408
|
-
record.send("#{key}=",value)
|
409
|
-
else
|
410
|
-
# TODO once reading schema.rb on client is implemented throw an error here
|
411
|
-
end
|
412
|
-
end
|
413
|
-
elsif record
|
414
|
-
# either the model is new, or its not even an active record model
|
415
|
-
dont_save_list << record unless save
|
416
|
-
keys = record.attributes.keys
|
417
|
-
attributes.each do |key, value|
|
418
|
-
if keys.include? key
|
419
|
-
record[key] = value
|
420
|
-
elsif !value.nil? and aggregation = record.class.reflect_on_aggregation(key) and !(aggregation.klass < ActiveRecord::Base)
|
421
|
-
aggregation.mapping.each_with_index do |pair, i|
|
422
|
-
record[pair.first] = value[i]
|
423
|
-
end
|
424
|
-
elsif key.to_s != "id" and record.respond_to?("#{key}=") # server side methods can get included and we won't be able to write them...
|
425
|
-
# for example if you have a server side method foo, that you "get" on a new record, then later that value will get sent to the server
|
426
|
-
# we should track better these server side methods so this does not happen
|
427
|
-
record.send("#{key}=",value)
|
428
|
-
end
|
408
|
+
end
|
409
|
+
elsif record
|
410
|
+
# either the model is new, or its not even an active record model
|
411
|
+
dont_save_list << record unless save
|
412
|
+
keys = record.attributes.keys
|
413
|
+
attributes.each do |key, value|
|
414
|
+
if keys.include? key
|
415
|
+
record[key] = value
|
416
|
+
elsif !value.nil? and aggregation = record.class.reflect_on_aggregation(key) and !(aggregation.klass < ActiveRecord::Base)
|
417
|
+
aggregation.mapping.each_with_index do |pair, i|
|
418
|
+
record[pair.first] = value[i]
|
429
419
|
end
|
430
|
-
|
420
|
+
elsif key.to_s != "id" and record.respond_to?("#{key}=") # server side methods can get included and we won't be able to write them...
|
421
|
+
# for example if you have a server side method foo, that you "get" on a new record, then later that value will get sent to the server
|
422
|
+
# we should track better these server side methods so this does not happen
|
423
|
+
record.send("#{key}=",value)
|
431
424
|
end
|
432
425
|
end
|
426
|
+
new_models << record
|
427
|
+
end
|
428
|
+
end
|
433
429
|
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
430
|
+
#puts "!!!!!!!!!!!!!!attributes updated"
|
431
|
+
ActiveRecord::Base.transaction do
|
432
|
+
associations.each do |association|
|
433
|
+
parent = reactive_records[association[:parent_id]]
|
434
|
+
next unless parent
|
435
|
+
#parent.instance_variable_set("@reactive_record_#{association[:attribute]}_changed", true) remove this????
|
436
|
+
if parent.class.reflect_on_aggregation(association[:attribute].to_sym)
|
437
|
+
#puts ">>>>>>AGGREGATE>>>> #{parent.class.name}.send('#{association[:attribute]}=', #{reactive_records[association[:child_id]]})"
|
438
|
+
aggregate = reactive_records[association[:child_id]]
|
439
|
+
dont_save_list << aggregate
|
440
|
+
current_attributes = parent.send(association[:attribute]).attributes
|
441
|
+
#puts "current parent attributes = #{current_attributes}"
|
442
|
+
new_attributes = aggregate.attributes
|
443
|
+
#puts "current child attributes = #{new_attributes}"
|
444
|
+
merged_attributes = current_attributes.merge(new_attributes) { |k, current_attr, new_attr| aggregate.send("#{k}_changed?") ? new_attr : current_attr}
|
445
|
+
#puts "merged attributes = #{merged_attributes}"
|
446
|
+
aggregate.assign_attributes(merged_attributes)
|
447
|
+
#puts "aggregate attributes after merge = #{aggregate.attributes}"
|
448
|
+
parent.send("#{association[:attribute]}=", aggregate)
|
449
|
+
#puts "updated is frozen? #{aggregate.frozen?}, parent attributes = #{parent.send(association[:attribute]).attributes}"
|
450
|
+
elsif parent.class.reflect_on_association(association[:attribute].to_sym).nil?
|
451
|
+
raise "Missing association :#{association[:attribute]} for #{parent.class.name}. Was association defined on opal side only?"
|
452
|
+
elsif parent.class.reflect_on_association(association[:attribute].to_sym).collection?
|
453
|
+
#puts ">>>>>>>>>> #{parent.class.name}.send('#{association[:attribute]}') << #{reactive_records[association[:child_id]]})"
|
454
|
+
dont_save_list.delete(parent)
|
455
|
+
if false and parent.new?
|
456
|
+
parent.send("#{association[:attribute]}") << reactive_records[association[:child_id]] if parent.new?
|
457
|
+
#puts "updated"
|
458
|
+
else
|
459
|
+
#puts "skipped"
|
460
|
+
end
|
461
|
+
else
|
462
|
+
#puts ">>>>ASSOCIATION>>>> #{parent.class.name}.send('#{association[:attribute]}=', #{reactive_records[association[:child_id]]})"
|
463
|
+
parent.send("#{association[:attribute]}=", reactive_records[association[:child_id]])
|
464
|
+
dont_save_list.delete(parent)
|
465
|
+
#puts "updated"
|
466
|
+
end
|
467
|
+
end if associations
|
468
|
+
|
469
|
+
#puts "!!!!!!!!!!!!associations updated"
|
470
|
+
|
471
|
+
has_errors = false
|
472
|
+
|
473
|
+
#puts "ready to start saving... dont_save_list = #{dont_save_list}"
|
474
|
+
|
475
|
+
saved_models = reactive_records.collect do |reactive_record_id, model|
|
476
|
+
#puts "saving rr_id: #{reactive_record_id} model.object_id: #{model.object_id} frozen? <#{model.frozen?}>"
|
477
|
+
if model and (model.frozen? or dont_save_list.include?(model) or model.changed.include?(model.class.primary_key))
|
478
|
+
# the above check for changed including the private key happens if you have an aggregate that includes its own id
|
479
|
+
#puts "validating frozen model #{model.class.name} #{model} (reactive_record_id = #{reactive_record_id})"
|
480
|
+
valid = model.valid?
|
481
|
+
#puts "has_errors before = #{has_errors}, validate= #{validate}, !valid= #{!valid} (validate and !valid) #{validate and !valid}"
|
482
|
+
has_errors ||= (validate and !valid)
|
483
|
+
#puts "validation complete errors = <#{!valid}>, #{model.errors.messages} has_errors #{has_errors}"
|
484
|
+
[reactive_record_id, model.class.name, model.attributes, (valid ? nil : model.errors.messages)]
|
485
|
+
elsif model and (!model.id or model.changed?)
|
486
|
+
#puts "saving #{model.class.name} #{model} (reactive_record_id = #{reactive_record_id})"
|
487
|
+
saved = model.check_permission_with_acting_user(acting_user, new_models.include?(model) ? :create_permitted? : :update_permitted?).save(validate: validate)
|
488
|
+
has_errors ||= !saved
|
489
|
+
messages = model.errors.messages if (validate and !saved) or (!validate and !model.valid?)
|
490
|
+
#puts "saved complete errors = <#{!saved}>, #{messages} has_errors #{has_errors}"
|
491
|
+
[reactive_record_id, model.class.name, model.attributes, messages]
|
492
|
+
end
|
493
|
+
end.compact
|
498
494
|
|
499
|
-
|
495
|
+
raise "Could not save all models" if has_errors
|
500
496
|
|
501
|
-
|
497
|
+
if save
|
502
498
|
|
503
|
-
|
499
|
+
{success: true, saved_models: saved_models }
|
504
500
|
|
505
|
-
|
501
|
+
else
|
506
502
|
|
507
|
-
|
508
|
-
|
503
|
+
vectors.each { |vector, model| model.reload unless model.nil? or model.new_record? or model.frozen? }
|
504
|
+
vectors
|
509
505
|
|
510
|
-
|
506
|
+
end
|
511
507
|
|
512
|
-
|
508
|
+
end
|
513
509
|
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
end
|
521
|
-
end
|
510
|
+
rescue Exception => e
|
511
|
+
ReactiveRecord::Pry.rescued(e)
|
512
|
+
if save
|
513
|
+
{success: false, saved_models: saved_models, message: e}
|
514
|
+
else
|
515
|
+
{}
|
522
516
|
end
|
523
517
|
end
|
524
518
|
|
@@ -567,25 +561,20 @@ module ReactiveRecord
|
|
567
561
|
else
|
568
562
|
|
569
563
|
def self.destroy_record(model, id, vector, acting_user)
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
else
|
577
|
-
ServerDataCache.new(acting_user, {})[*vector]
|
578
|
-
end
|
564
|
+
model = Object.const_get(model)
|
565
|
+
record = if id
|
566
|
+
model.find(id)
|
567
|
+
else
|
568
|
+
ServerDataCache.new(acting_user, {})[*vector]
|
569
|
+
end
|
579
570
|
|
580
571
|
|
581
|
-
|
582
|
-
|
572
|
+
record.check_permission_with_acting_user(acting_user, :destroy_permitted?).destroy
|
573
|
+
{success: true, attributes: {}}
|
583
574
|
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
end
|
588
|
-
end
|
575
|
+
rescue Exception => e
|
576
|
+
ReactiveRecord::Pry.rescued(e)
|
577
|
+
{success: false, record: record, message: e}
|
589
578
|
end
|
590
579
|
end
|
591
580
|
end
|
data/lib/reactive_record/pry.rb
CHANGED
@@ -2,16 +2,10 @@ module ReactiveRecord
|
|
2
2
|
|
3
3
|
module Pry
|
4
4
|
|
5
|
-
def self.rescue(&block)
|
6
|
-
if defined? PryRescue
|
7
|
-
::Pry::rescue &block
|
8
|
-
else
|
9
|
-
block.call
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
5
|
def self.rescued(e)
|
14
|
-
|
6
|
+
if defined?(PryRescue) && e.instance_variable_defined?(:@rescue_bindings) && !e.is_a?(ReactiveRecord::AccessViolation)
|
7
|
+
::Pry::rescued(e)
|
8
|
+
end
|
15
9
|
end
|
16
10
|
|
17
11
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reactive-record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.37
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mitch VanDuyn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|