Method: ActiveRecord::ClassMethods#_react_param_conversion

Defined in:
lib/reactive_record/active_record/class_methods.rb

#_react_param_conversion(param, opt = nil) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/reactive_record/active_record/class_methods.rb', line 357

def _react_param_conversion(param, opt = nil)
  param = Native(param)
  param = JSON.from_object(param.to_n) if param.is_a? Native::Object
  result =
    if param.is_a? self
      param
    elsif param.is_a? Hash
      if opt == :validate_only
        klass = ReactiveRecord::Base.infer_type_from_hash(self, param)
        klass == self || klass < self
      else
        # TODO: investigate saving .changes here and then replacing the
        # TODO: changes after the load is complete.  In other words preserve the
        # TODO: changed values as changes while just updating the synced values.
        target =
          if param[primary_key]
            find(param[primary_key])
          else
            new
          end

        associations = reflect_on_all_associations

        param = param.collect do |key, value|
          assoc = associations.detect do |association|
            association.association_foreign_key == key
          end

          if assoc
            if value
              [assoc.attribute, { id: [value] }]
            else
              [assoc.attribute, [nil]]
            end
          else
            [key, [value]]
          end
        end
        # TODO: verify wrapping with load_data was added so broadcasting works in 1.0.0.lap28
        ReactiveRecord::Base.load_data do
          ReactiveRecord::ServerDataCache.load_from_json(Hash[param], target)
        end
        target.cast_to_current_sti_type
      end
    end

  result
end