Sha256: 6fae44e4a0e16bfa1bdd6be849039e9ac3580ca5c307790519581b4ec69813eb

Contents?: true

Size: 1.95 KB

Versions: 14

Compression:

Stored size: 1.95 KB

Contents

module ActiveRecord::Turntable
  module ActiveRecordExt
    module CleverLoad
      extend ActiveSupport::Concern

      included do
        class << ActiveRecord::Base
          delegate :clever_load!, :to => :all
        end
      end

      def clever_load!(association_name)
        # load records
        records = self.to_a
        klass = records.first.class
        association_key = Util.ar42_or_later? ? association_name.to_s : association_name
        reflection = klass.reflections[association_key]

        if reflection
          foreign_class = reflection.klass
          foreign_objects = case reflection.macro
                            when :has_one
                              foreign_class.where(reflection.foreign_key => records.map(&reflection.association_primary_key.to_sym).uniq)
                            when :belongs_to
                              foreign_class.where(reflection.association_primary_key => records.map(&reflection.foreign_key.to_sym).uniq)
                            else
                              []
                            end

          self.each do |obj|
            matched_object = case reflection.macro
                             when :has_one
                               foreign_objects.find {|fo|
                                 obj.send(reflection.association_primary_key) == fo.send(reflection.foreign_key)
                               }
                             when :belongs_to
                               foreign_objects.find {|fo|
                                 obj.send(reflection.foreign_key) == fo.send(reflection.association_primary_key)
                               }
                             end
            obj.association(association_name).target = matched_object
            obj.association(association_name).set_inverse_instance(matched_object) if matched_object
            obj.association(association_name).loaded!
          end
        end
        records
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
activerecord-turntable-2.5.0 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.4.0 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.3.3 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.3.2 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.3.1 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.3.0 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.2.2 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.2.1 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.2.0 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.1.1 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.1.0 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.1.0.rc2 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.1.0.rc1 lib/active_record/turntable/active_record_ext/clever_load.rb
activerecord-turntable-2.1.0.beta2 lib/active_record/turntable/active_record_ext/clever_load.rb