Sha256: 6a24cb75c27ebe291696b9dbd3312bc849e26b758efa8d9e5b12c65d0a1761d5

Contents?: true

Size: 1.9 KB

Versions: 26

Compression:

Stored size: 1.9 KB

Contents

module ActiveRecord
  module Associations
    class Preloader
      module ThroughAssociation #:nodoc:

        def through_reflection
          reflection.through_reflection
        end

        def source_reflection
          reflection.source_reflection
        end

        def associated_records_by_owner
          through_records = through_records_by_owner

          ActiveRecord::Associations::Preloader.new(
            through_records.values.flatten,
            source_reflection.name, options
          ).run

          through_records.each do |owner, records|
            records.map! { |r| r.send(source_reflection.name) }.flatten!
            records.compact!
          end
        end

        private

        def through_records_by_owner
          ActiveRecord::Associations::Preloader.new(
            owners, through_reflection.name,
            through_options
          ).run

          Hash[owners.map do |owner|
            through_records = Array.wrap(owner.send(through_reflection.name))

            # Dont cache the association - we would only be caching a subset
            if (preload_options != through_options) ||
               (reflection.options[:source_type] && through_reflection.collection?)
              owner.association(through_reflection.name).reset
            end

            [owner, through_records]
          end]
        end

        def through_options
          through_options = {}

          if options[:source_type]
            through_options[:conditions] = { reflection.foreign_type => options[:source_type] }
          else
            if options[:conditions]
              through_options[:include]    = options[:include] || options[:source]
              through_options[:conditions] = options[:conditions]
            end
            through_options[:order] = options[:order] if options.has_key?(:order)
          end

          through_options
        end
      end
    end
  end
end

Version data entries

26 entries across 21 versions & 3 rubygems

Version Path
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/activerecord-3.2.22.5/lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.22.5 lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.22.4 lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.22.3 lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.22.2 lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.22.1 lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.22 lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.21 lib/active_record/associations/preloader/through_association.rb
apl-library-0.0.90 vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/preloader/through_association.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/associations/preloader/through_association.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/associations/preloader/through_association.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/preloader/through_association.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/associations/preloader/through_association.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.20 lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.19 lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.18 lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.17 lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.16 lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.15 lib/active_record/associations/preloader/through_association.rb