module ActiveLoaders::Adapters::ActiveModelSerializers::ArraySerializer

Public Instance Methods

initialize_with_loaders(objects, options = {}) click to toggle source
# File lib/active_loaders/datasource_adapter.rb, line 8
def initialize_with_loaders(objects, options = {})
  datasource_class = options.delete(:datasource)
  adapter = Datasource.orm_adapters.find { |a| a.is_scope?(objects) }
  if adapter && !adapter.scope_loaded?(objects)
    scope = begin
      objects
      .for_serializer(options[:serializer])
      .datasource_params(*[options[:loader_params]].compact)
    rescue NameError
      if options[:serializer].nil?
        return initialize_without_loaders(objects, options)
      else
        raise
      end
    end

    if datasource_class
      scope = scope.with_datasource(datasource_class)
    end

    records = adapter.scope_to_records(scope)

    # if we are loading an association proxy, we should set the target
    # especially because AMS will resolve it twice, which would do 2 queries
    if objects.respond_to?(:proxy_association)
      objects.proxy_association.target = records
    end

    initialize_without_loaders(records, options)
  else
    initialize_without_loaders(objects, options)
  end
end