Method: ActiveRecord::ClassMethods#_react_param_conversion

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

#_react_param_conversion(param, opt = nil) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/reactive_record/active_record/class_methods.rb', line 248

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 or klass < self
    else
      if param[primary_key]
        target = find(param[primary_key])
      else
        target = new
      end
      associations = reflect_on_all_associations
      param = param.collect do |key, value|
        assoc = reflect_on_all_associations.detect do |assoc|
          assoc.association_foreign_key == key
        end
        if assoc
          if value
            [assoc.attribute, {id: [value], type: [nil]}]
          else
            [assoc.attribute, [nil]]
          end
        else
          [key, [value]]
        end
      end

      # We do want to be doing something like this, but this breaks other stuff...
      #
      # ReactiveRecord::Base.load_data do
      #   ReactiveRecord::ServerDataCache.load_from_json(Hash[param], target)
      # end

      ReactiveRecord::ServerDataCache.load_from_json(Hash[param], target)
      target
    end
  else
    nil
  end
  result
end