Class: GraphitiGql::Loaders::PolymorphicHasMany

Inherits:
Many
  • Object
show all
Defined in:
lib/graphiti_gql/loaders/polymorphic_has_many.rb

Instance Method Summary collapse

Methods inherited from Many

factory, #initialize, #perform

Constructor Details

This class inherits a constructor from GraphitiGql::Loaders::Many

Instance Method Details

#assign(parent_records, proxy) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/graphiti_gql/loaders/polymorphic_has_many.rb', line 4

def assign(parent_records, proxy)
  records = proxy.data
  parent_records.each do |pr|
    corresponding = records.select do |record|
      child_ft = record.send("#{@sideload.polymorphic_as}_type")
      child_fk = record.send(@sideload.foreign_key)
      parent_ft = pr.class.name
      parent_fk = pr.send(@sideload.primary_key)
      child_ft == parent_ft && child_fk == parent_fk
    end
    data = [corresponding || [], proxy]
    fulfill(pr, data)
  end
end