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

Inherits:
Object
  • Object
show all
Defined in:
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_options) ⇒ Association

Returns a new instance of Association.



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

def initialize(klass, owners, reflection, preload_options)
  @klass           = klass
  @owners          = owners
  @reflection      = reflection
  @preload_options = preload_options || {}
  @model           = owners.first && owners.first.class
  @scoped          = nil
  @owners_by_key   = nil
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File '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 '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 'lib/active_record/associations/preloader/association.rb', line 5

def owners
  @owners
end

#preload_optionsObject (readonly)

Returns the value of attribute preload_options.



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

def preload_options
  @preload_options
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



5
6
7
# File '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



46
47
48
# File 'lib/active_record/associations/preloader/association.rb', line 46

def association_key
  table[association_key_name]
end

#association_key_nameObject

The name of the key on the associated records

Raises:

  • (NotImplementedError)


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

def association_key_name
  raise NotImplementedError
end

#optionsObject



64
65
66
# File 'lib/active_record/associations/preloader/association.rb', line 64

def options
  reflection.options
end

#owner_key_nameObject

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

Raises:

  • (NotImplementedError)


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

def owner_key_name
  raise NotImplementedError
end

#owners_by_keyObject

We’re converting to a string here because postgres will return the aliased association key in a habtm as a string (for whatever reason)



57
58
59
60
61
62
# File 'lib/active_record/associations/preloader/association.rb', line 57

def owners_by_key
  @owners_by_key ||= owners.group_by do |owner|
    key = owner[owner_key_name]
    key && key.to_s
  end
end

#preloadObject

Raises:

  • (NotImplementedError)


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

def preload
  raise NotImplementedError
end

#records_for(ids) ⇒ Object



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

def records_for(ids)
  scoped.where(association_key.in(ids))
end

#runObject



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

def run
  unless owners.first.association(reflection.name).loaded?
    preload
  end
end

#scopedObject



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

def scoped
  @scoped ||= build_scope
end

#tableObject



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

def table
  klass.arel_table
end