Module: Associationist::ActiveRecordPreloaderBatchPatch

Defined in:
lib/associationist/associations/preloader/singular_association.rb

Instance Method Summary collapse

Instance Method Details

#callObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/associationist/associations/preloader/singular_association.rb', line 98

def call
  branches = @preloaders.flat_map(&:branches)
  until branches.empty?
    loaders = branches.flat_map(&:runnable_loaders)
    loaders.each do |loader|
      if loader.is_a? Associationist::Associations::Preloader::SingularAssociation
      else
        loader.associate_records_from_unscoped(@available_records[loader.klass.base_class])
      end
    end

    if loaders.any?
      future_tables = branches.flat_map do |branch|
        branch.future_classes - branch.runnable_loaders.map(&:klass)
      end.map(&:table_name).uniq

      target_loaders = loaders.reject { |l| future_tables.include?(l.table_name)  }
      target_loaders = loaders if target_loaders.empty?

      non_associationist_loaders = target_loaders.reject{|x| x.is_a? Associationist::Associations::Preloader::SingularAssociation}
      group_and_load_similar(non_associationist_loaders)
      target_loaders.each(&:run)
    end

    finished, in_progress = branches.partition(&:done?)

    branches = in_progress + finished.flat_map(&:children)
  end
end