Method: Pureapi::Controller#merge_as_json

Defined in:
lib/pureapi/controller.rb

#merge_as_json(target, source) ⇒ Object

Begin feature records_json and record_json which render format json Merge target from request params, source from model

Arguments:

target: (Hash)
source: (Hash)


433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/pureapi/controller.rb', line 433

def merge_as_json(target ,source)
  result = {}

  if source[:only].is_a?(Array)
    result[:only] = target[:only].is_a?(Array) ? (target[:only] & source[:only]) : source[:only]
  end

  if source[:methods].is_a?(Array) && target[:only].is_a?(Array)
    result[:methods] = target[:only] & source[:methods]
  end

  if source[:include].is_a?(Hash) && target[:include].is_a?(Hash)
    result[:include] = {}

    (source[:include].keys & target[:include].keys).each do |k|
      result[:include][k] = merge_as_json(target[:include][k], source[:include][k])
    end
  end

  result.delete_if { |k, v| v.blank? }
end