Sha256: c465a00e5e1f3ed9bddd5db4a04027602288e96a8883f29f2f97fb96fe5dbb47

Contents?: true

Size: 1.82 KB

Versions: 156

Compression:

Stored size: 1.82 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 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]
          end

          through_options
        end
      end
    end
  end
end

Version data entries

156 entries across 131 versions & 11 rubygems

Version Path
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/preloader/through_association.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.12/lib/active_record/associations/preloader/through_association.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/preloader/through_association.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/preloader/through_association.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/preloader/through_association.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/preloader/through_association.rb
challah-1.0.0.beta vendor/bundle/gems/activerecord-3.2.11/lib/active_record/associations/preloader/through_association.rb
activerecord-3.1.12 lib/active_record/associations/preloader/through_association.rb
active_mailer-0.0.5 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/preloader/through_association.rb
active_mailer-0.0.4 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/preloader/through_association.rb
active_mailer-0.0.3 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.12 lib/active_record/associations/preloader/through_association.rb
activerecord-3.1.11 lib/active_record/associations/preloader/through_association.rb
challah-0.9.1.beta.3 vendor/bundle/gems/activerecord-3.2.11/lib/active_record/associations/preloader/through_association.rb
devise_sociable-0.1.0 vendor/bundle/gems/activerecord-3.2.11/lib/active_record/associations/preloader/through_association.rb
challah-0.9.1.beta vendor/bundle/gems/activerecord-3.2.11/lib/active_record/associations/preloader/through_association.rb
activerecord-3.2.11 lib/active_record/associations/preloader/through_association.rb
activerecord-3.1.10 lib/active_record/associations/preloader/through_association.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.10/lib/active_record/associations/preloader/through_association.rb
challah-0.9.0 vendor/bundle/gems/activerecord-3.2.9/lib/active_record/associations/preloader/through_association.rb