<commit_before>module ActiveRecord
  module Associations
    class Preloader
      module ThroughAssociation #:nodoc:
        def initialize(klass, owners, reflection, preload_scope)
          super
        end

        def through_reflection
          reflection.through_reflection
        end

        def source_reflection
          reflection.source_reflection
        end

        def associated_records_by_owner(preloader)
          preloader.preload(owners,
                            through_reflection.name,
                            through_scope)

          should_reset = (through_scope != through_reflection.klass.unscoped) ||
             (reflection.options[:source_type] && through_reflection.collection?)

          through_records = owners.map do |owner, h|
            association = owner.association through_reflection.name

            [owner, Array(association.reader), association]
          end

          # Dont cache the association - we would only be caching a subset
          if should_reset
            through_records.each { |(_,_,assoc)| assoc.reset }
          end

          middle_records = through_records.map { |(_,rec,_)| rec }.flatten

          preloaders = preloader.preload(middle_records,
                                         source_reflection.name,
                                         reflection_scope)

          middle_to_pl = preloaders.each_with_object({}) do |pl,h|
            pl.owners.each { |middle|
              h[middle] = pl
            }
          end

          through_records.each_with_object({}) { |(lhs,center),records_by_owner|
            pl_to_middle = center.group_by { |record| middle_to_pl[record] }

            records_by_owner[lhs] = pl_to_middle.flat_map do |pl, middles|
              rhs_records = middles.flat_map { |r|
                r.send(source_reflection.name)
              }.compact

              loaded_records = pl.preloaded_records
              i = 0
              record_index = loaded_records.each_with_object({}) { |r,indexes|
                indexes[r] = i
                i += 1
              }
              records = rhs_records.sort_by { |rhs| record_index[rhs] }
              @preloaded_records.concat rhs_records
              records
            end
          }
        end

        private

        def through_scope<commit_msg>extract association resetting to a method<commit_after>module ActiveRecord
  module Associations
    class Preloader
      module ThroughAssociation #:nodoc:
        def initialize(klass, owners, reflection, preload_scope)
          super
        end

        def through_reflection
          reflection.through_reflection
        end

        def source_reflection
          reflection.source_reflection
        end

        def associated_records_by_owner(preloader)
          preloader.preload(owners,
                            through_reflection.name,
                            through_scope)

          through_records = owners.map do |owner, h|
            association = owner.association through_reflection.name

            [owner, Array(association.reader)]
          end

          reset_association owners, through_reflection.name

          middle_records = through_records.map { |(_,rec)| rec }.flatten

          preloaders = preloader.preload(middle_records,
                                         source_reflection.name,
                                         reflection_scope)

          middle_to_pl = preloaders.each_with_object({}) do |pl,h|
            pl.owners.each { |middle|
              h[middle] = pl
            }
          end

          through_records.each_with_object({}) { |(lhs,center),records_by_owner|
            pl_to_middle = center.group_by { |record| middle_to_pl[record] }

            records_by_owner[lhs] = pl_to_middle.flat_map do |pl, middles|
              rhs_records = middles.flat_map { |r|
                r.send(source_reflection.name)
              }.compact

              loaded_records = pl.preloaded_records
              i = 0
              record_index = loaded_records.each_with_object({}) { |r,indexes|
                indexes[r] = i
                i += 1
              }
              records = rhs_records.sort_by { |rhs| record_index[rhs] }
              @preloaded_records.concat rhs_records
              records
            end
          }
        end

        private

        def reset_association(owners, association_name)
          should_reset = (through_scope != through_reflection.klass.unscoped) ||
             (reflection.options[:source_type] && through_reflection.collection?)

          # Dont cache the association - we would only be caching a subset
          if should_reset
            owners.each { |owner|
              owner.association(association_name).reset
            }
          end
        end


        def through_scope
{'fraction_non_alphanumeric': 0.07066557107641742, 'fraction_numerical': 0.0008216926869350862, 'mean_word_length': 1.6375947995666305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6649958', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1255, 'n_words': 250}