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: 71c7a7abb7ea51cdb5f5139725724b0a1cd773fd
4
- data.tar.gz: c1d222084866c7aa0f8b3f77d495825b504f3e35
3
+ metadata.gz: b36c2c16110af0053cb7aae2d944d968f53874f7
4
+ data.tar.gz: 2d2c5b526e8f1fa9e95573abcd582c841141071e
5
5
  SHA512:
6
- metadata.gz: 0332dfa333ea3344e970d4b37c24a1ff9703823c5f53c645754de0b815a4254655f5da7dc15b3c0dbbd33b8adc522815395e6871d4eb61b826f9d16d5034125a
7
- data.tar.gz: 1262dc1375cbbc0f423dde4d4e181bc69b2c44acad615944fd8833cd29efac2f6cb5d2338daa6f8b12b017cac75f4a93dd5b78cebdf0c032b5ec382c4e84177b
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
- ReactiveRecord::Pry.rescue do
376
- begin
377
-
378
- reactive_records = {}
379
- vectors = {}
380
- new_models = []
381
- saved_models = []
382
- dont_save_list = []
383
-
384
- models.each do |model_to_save|
385
- attributes = model_to_save[:attributes]
386
- model = Object.const_get(model_to_save[:model])
387
- id = attributes.delete(model.primary_key) if model.respond_to? :primary_key # if we are saving existing model primary key value will be present
388
- vector = model_to_save[:vector]
389
- vector = [vector[0].constantize] + vector[1..-1].collect do |method|
390
- if method.is_a?(Array) and method.first == "find_by_id"
391
- ["find", method.last]
392
- else
393
- method
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
- reactive_records[model_to_save[:id]] = vectors[vector] = record = find_record(model, id, vector, save)
397
- if record and record.respond_to?(:id) and record.id
398
- # we have an already exising activerecord model
399
- keys = record.attributes.keys
400
- attributes.each do |key, value|
401
- if keys.include? key
402
- record[key] = value
403
- elsif !value.nil? and aggregation = record.class.reflect_on_aggregation(key.to_sym) and !(aggregation.klass < ActiveRecord::Base)
404
- aggregation.mapping.each_with_index do |pair, i|
405
- record[pair.first] = value[i]
406
- end
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
- new_models << record
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
- #puts "!!!!!!!!!!!!!!attributes updated"
435
- ActiveRecord::Base.transaction do
436
- associations.each do |association|
437
- parent = reactive_records[association[:parent_id]]
438
- next unless parent
439
- #parent.instance_variable_set("@reactive_record_#{association[:attribute]}_changed", true) remove this????
440
- if parent.class.reflect_on_aggregation(association[:attribute].to_sym)
441
- #puts ">>>>>>AGGREGATE>>>> #{parent.class.name}.send('#{association[:attribute]}=', #{reactive_records[association[:child_id]]})"
442
- aggregate = reactive_records[association[:child_id]]
443
- dont_save_list << aggregate
444
- current_attributes = parent.send(association[:attribute]).attributes
445
- #puts "current parent attributes = #{current_attributes}"
446
- new_attributes = aggregate.attributes
447
- #puts "current child attributes = #{new_attributes}"
448
- merged_attributes = current_attributes.merge(new_attributes) { |k, current_attr, new_attr| aggregate.send("#{k}_changed?") ? new_attr : current_attr}
449
- #puts "merged attributes = #{merged_attributes}"
450
- aggregate.assign_attributes(merged_attributes)
451
- #puts "aggregate attributes after merge = #{aggregate.attributes}"
452
- parent.send("#{association[:attribute]}=", aggregate)
453
- #puts "updated is frozen? #{aggregate.frozen?}, parent attributes = #{parent.send(association[:attribute]).attributes}"
454
- elsif parent.class.reflect_on_association(association[:attribute].to_sym).nil?
455
- raise "Missing association :#{association[:attribute]} for #{parent.class.name}. Was association defined on opal side only?"
456
- elsif parent.class.reflect_on_association(association[:attribute].to_sym).collection?
457
- #puts ">>>>>>>>>> #{parent.class.name}.send('#{association[:attribute]}') << #{reactive_records[association[:child_id]]})"
458
- dont_save_list.delete(parent)
459
- if false and parent.new?
460
- parent.send("#{association[:attribute]}") << reactive_records[association[:child_id]] if parent.new?
461
- #puts "updated"
462
- else
463
- #puts "skipped"
464
- end
465
- else
466
- #puts ">>>>ASSOCIATION>>>> #{parent.class.name}.send('#{association[:attribute]}=', #{reactive_records[association[:child_id]]})"
467
- parent.send("#{association[:attribute]}=", reactive_records[association[:child_id]])
468
- dont_save_list.delete(parent)
469
- #puts "updated"
470
- end
471
- end if associations
472
-
473
- #puts "!!!!!!!!!!!!associations updated"
474
-
475
- has_errors = false
476
-
477
- #puts "ready to start saving... dont_save_list = #{dont_save_list}"
478
-
479
- saved_models = reactive_records.collect do |reactive_record_id, model|
480
- #puts "saving rr_id: #{reactive_record_id} model.object_id: #{model.object_id} frozen? <#{model.frozen?}>"
481
- if model and (model.frozen? or dont_save_list.include?(model) or model.changed.include?(model.class.primary_key))
482
- # the above check for changed including the private key happens if you have an aggregate that includes its own id
483
- #puts "validating frozen model #{model.class.name} #{model} (reactive_record_id = #{reactive_record_id})"
484
- valid = model.valid?
485
- #puts "has_errors before = #{has_errors}, validate= #{validate}, !valid= #{!valid} (validate and !valid) #{validate and !valid}"
486
- has_errors ||= (validate and !valid)
487
- #puts "validation complete errors = <#{!valid}>, #{model.errors.messages} has_errors #{has_errors}"
488
- [reactive_record_id, model.class.name, model.attributes, (valid ? nil : model.errors.messages)]
489
- elsif model and (!model.id or model.changed?)
490
- #puts "saving #{model.class.name} #{model} (reactive_record_id = #{reactive_record_id})"
491
- saved = model.check_permission_with_acting_user(acting_user, new_models.include?(model) ? :create_permitted? : :update_permitted?).save(validate: validate)
492
- has_errors ||= !saved
493
- messages = model.errors.messages if (validate and !saved) or (!validate and !model.valid?)
494
- #puts "saved complete errors = <#{!saved}>, #{messages} has_errors #{has_errors}"
495
- [reactive_record_id, model.class.name, model.attributes, messages]
496
- end
497
- end.compact
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
- raise "Could not save all models" if has_errors
495
+ raise "Could not save all models" if has_errors
500
496
 
501
- if save
497
+ if save
502
498
 
503
- {success: true, saved_models: saved_models }
499
+ {success: true, saved_models: saved_models }
504
500
 
505
- else
501
+ else
506
502
 
507
- vectors.each { |vector, model| model.reload unless model.nil? or model.new_record? or model.frozen? }
508
- vectors
503
+ vectors.each { |vector, model| model.reload unless model.nil? or model.new_record? or model.frozen? }
504
+ vectors
509
505
 
510
- end
506
+ end
511
507
 
512
- end
508
+ end
513
509
 
514
- rescue Exception => e
515
- ReactiveRecord::Pry.rescued(e)
516
- if save
517
- {success: false, saved_models: saved_models, message: e}
518
- else
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
- ReactiveRecord::Pry.rescue do
572
- begin
573
- model = Object.const_get(model)
574
- record = if id
575
- model.find(id)
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
- record.check_permission_with_acting_user(acting_user, :destroy_permitted?).destroy
582
- {success: true, attributes: {}}
572
+ record.check_permission_with_acting_user(acting_user, :destroy_permitted?).destroy
573
+ {success: true, attributes: {}}
583
574
 
584
- rescue Exception => e
585
- ReactiveRecord::Pry.rescued(e)
586
- {success: false, record: record, message: e}
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
@@ -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
- ::Pry::rescued(e) if defined?(PryRescue) && !e.is_a?(ReactiveRecord::AccessViolation)
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
@@ -1,3 +1,3 @@
1
1
  module ReactiveRecord
2
- VERSION = "0.7.36"
2
+ VERSION = "0.7.37"
3
3
  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.36
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-16 00:00:00.000000000 Z
11
+ date: 2016-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails