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

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

Overview

:nodoc:

Direct Known Subclasses

CollectionAssociation, SingularAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, owners, reflection, preload_scope) ⇒ Association

Returns a new instance of Association.



8
9
10
11
12
13
14
15
16
17
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 8

def initialize(klass, owners, reflection, preload_scope)
  @klass         = klass
  @owners        = owners
  @reflection    = reflection
  @preload_scope = preload_scope
  @model         = owners.first && owners.first.class
  @scope         = nil
  @owners_by_key = nil
  @preloaded_records = []
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass



5
6
7
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 5

def klass
  @klass
end

#modelObject (readonly)

Returns the value of attribute model



5
6
7
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 5

def model
  @model
end

#ownersObject (readonly)

Returns the value of attribute owners



5
6
7
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 5

def owners
  @owners
end

#preload_scopeObject (readonly)

Returns the value of attribute preload_scope



5
6
7
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 5

def preload_scope
  @preload_scope
end

#preloaded_recordsObject (readonly)

Returns the value of attribute preloaded_records



6
7
8
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 6

def preloaded_records
  @preloaded_records
end

#reflectionObject (readonly)

Returns the value of attribute reflection



5
6
7
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 5

def reflection
  @reflection
end

Instance Method Details

#association_keyObject

This is overridden by HABTM as the condition should be on the foreign_key column in the join table



50
51
52
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 50

def association_key
  table[association_key_name]
end

#association_key_nameObject

The name of the key on the associated records

Raises:

  • (NotImplementedError)


44
45
46
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 44

def association_key_name
  raise NotImplementedError
end

#optionsObject



71
72
73
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 71

def options
  reflection.options
end

#owner_key_nameObject

The name of the key on the model which declares the association

Raises:

  • (NotImplementedError)


55
56
57
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 55

def owner_key_name
  raise NotImplementedError
end

#owners_by_keyObject



59
60
61
62
63
64
65
66
67
68
69
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 59

def owners_by_key
  @owners_by_key ||= if key_conversion_required?
                       owners.group_by do |owner|
                         owner[owner_key_name].to_s
                       end
                     else
                       owners.group_by do |owner|
                         owner[owner_key_name]
                       end
                     end
end

#preload(preloader) ⇒ Object

Raises:

  • (NotImplementedError)


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

def preload(preloader)
  raise NotImplementedError
end

#query_scope(ids) ⇒ Object



35
36
37
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 35

def query_scope(ids)
  scope.where(association_key.in(ids))
end

#records_for(ids) ⇒ Object



31
32
33
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 31

def records_for(ids)
  query_scope(ids)
end

#run(preloader) ⇒ Object



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

def run(preloader)
  preload(preloader)
end

#scopeObject



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

def scope
  @scope ||= build_scope
end

#tableObject



39
40
41
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 39

def table
  klass.arel_table
end