Class: ActiveRecord::Associations::Preloader::Association::LoaderQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/associations/preloader/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, association_key_name) ⇒ LoaderQuery

Returns a new instance of LoaderQuery.



12
13
14
15
# File 'lib/active_record/associations/preloader/association.rb', line 12

def initialize(scope, association_key_name)
  @scope = scope
  @association_key_name = association_key_name
end

Instance Attribute Details

#association_key_nameObject (readonly)

Returns the value of attribute association_key_name.



10
11
12
# File 'lib/active_record/associations/preloader/association.rb', line 10

def association_key_name
  @association_key_name
end

#scopeObject (readonly)

Returns the value of attribute scope.



10
11
12
# File 'lib/active_record/associations/preloader/association.rb', line 10

def scope
  @scope
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/active_record/associations/preloader/association.rb', line 17

def eql?(other)
  association_key_name == other.association_key_name &&
    scope.table_name == other.scope.table_name &&
    scope.values_for_queries == other.scope.values_for_queries
end

#hashObject



23
24
25
# File 'lib/active_record/associations/preloader/association.rb', line 23

def hash
  [association_key_name, scope.table_name, scope.values_for_queries].hash
end

#load_records_for_keys(keys, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_record/associations/preloader/association.rb', line 40

def load_records_for_keys(keys, &block)
  if association_key_name.is_a?(Array)
    query_constraints = Hash.new { |hsh, key| hsh[key] = Set.new }

    keys.each_with_object(query_constraints) do |values_set, constraints|
      association_key_name.zip(values_set).each do |key_name, value|
        constraints[key_name] << value
      end
    end

    scope.where(query_constraints)
  else
    scope.where(association_key_name => keys)
  end.load(&block)
end

#load_records_in_batch(loaders) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/active_record/associations/preloader/association.rb', line 31

def load_records_in_batch(loaders)
  raw_records = records_for(loaders)

  loaders.each do |loader|
    loader.load_records(raw_records)
    loader.run
  end
end

#records_for(loaders) ⇒ Object



27
28
29
# File 'lib/active_record/associations/preloader/association.rb', line 27

def records_for(loaders)
  LoaderRecords.new(loaders, self).records
end