through_association.rb 3.0 KB
Newer Older
J
Jon Leighton 已提交
1 2 3 4 5 6 7 8 9 10 11 12
module ActiveRecord
  module Associations
    class Preloader
      module ThroughAssociation #:nodoc:
        def through_reflection
          reflection.through_reflection
        end

        def source_reflection
          reflection.source_reflection
        end

13
        def associated_records_by_owner(preloader)
14 15 16
          preloader.preload(owners,
                            through_reflection.name,
                            through_scope)
17

V
Vipul A M 已提交
18
          through_records = owners.map do |owner|
19 20
            association = owner.association through_reflection.name

21
            [owner, Array(association.reader)]
22
          end
23

24
          reset_association owners, through_reflection.name
J
Jon Leighton 已提交
25

26
          middle_records = through_records.flat_map { |(_,rec)| rec }
A
Aaron Patterson 已提交
27

28
          preloaders = preloader.preload(middle_records,
29 30
                                         source_reflection.name,
                                         reflection_scope)
31

32
          @preloaded_records = preloaders.flat_map(&:preloaded_records)
33

34
          middle_to_pl = preloaders.each_with_object({}) do |pl,h|
35 36 37 38 39
            pl.owners.each { |middle|
              h[middle] = pl
            }
          end

40 41 42
          record_offset = {}
          @preloaded_records.each_with_index do |record,i|
            record_offset[record] = i
43 44
          end

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

48
            records_by_owner[lhs] = pl_to_middle.flat_map do |pl, middles|
49
              rhs_records = middles.flat_map { |r|
50 51 52
                association = r.association source_reflection.name

                association.reader
53 54
              }.compact

55
              rhs_records.sort_by { |rhs| record_offset[rhs] }
56
            end
A
Aaron Patterson 已提交
57
          }
J
Jon Leighton 已提交
58 59 60 61
        end

        private

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

J
jbsmith86 已提交
66
          # Don't cache the association - we would only be caching a subset
67 68 69 70 71 72 73 74
          if should_reset
            owners.each { |owner|
              owner.association(association_name).reset
            }
          end
        end


75
        def through_scope
76
          scope = through_reflection.klass.unscoped
J
Jon Leighton 已提交
77 78

          if options[:source_type]
79
            scope.where! reflection.foreign_type => options[:source_type]
J
Jon Leighton 已提交
80
          else
81
            unless reflection_scope.where_values.empty?
82 83
              scope.includes_values = Array(reflection_scope.values[:includes] || options[:source])
              scope.where_values    = reflection_scope.values[:where]
84
              scope.bind_values     = reflection_scope.bind_values
J
Jon Leighton 已提交
85 86
            end

87
            scope.references! reflection_scope.values[:references]
88
            scope = scope.order reflection_scope.values[:order] if scope.eager_loading?
J
Jon Leighton 已提交
89 90
          end

91
          scope
J
Jon Leighton 已提交
92 93 94 95 96
        end
      end
    end
  end
end