Module: DbCharmer::ActiveRecord::AssociationPreload

Defined in:
lib/db_charmer/active_record/association_preload.rb

Constant Summary collapse

ASSOCIATION_TYPES =
[ :has_one, :has_many, :belongs_to, :has_and_belongs_to_many ]

Class Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/db_charmer/active_record/association_preload.rb', line 6

def self.extended(base)
  ASSOCIATION_TYPES.each do |association_type|
    base.class_eval <<-EOF, __FILE__, __LINE__ + 1
      def self.preload_#{association_type}_association(records, reflection, preload_options = {})
        if self.db_charmer_top_level_connection? || reflection.options[:polymorphic] ||
              self.db_charmer_default_connection != reflection.klass.db_charmer_default_connection
          return super(records, reflection, preload_options)
        end
        reflection.klass.on_db(self) do
          super(records, reflection, preload_options)
        end
      end
    EOF
  end
end