Class: ActiveRecord::Associations::JoinDependency::JoinPart

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_record/associations/join_dependency/join_part.rb

Overview

A JoinPart represents a part of a JoinDependency. It is inherited by JoinBase and JoinAssociation. A JoinBase represents the Active Record which everything else is being joined onto. A JoinAssociation represents an association which is joining to the base. A JoinAssociation may result in more than one actual join operations (for example a has_and_belongs_to_many JoinAssociation would result in two; one for the join table and one for the target table).

Direct Known Subclasses

JoinAssociation, JoinBase

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_klass, children) ⇒ JoinPart

Returns a new instance of JoinPart.



20
21
22
23
24
# File 'lib/active_record/associations/join_dependency/join_part.rb', line 20

def initialize(base_klass, children)
  @base_klass = base_klass
  @column_names_with_alias = nil
  @children = children
end

Instance Attribute Details

#base_klassObject (readonly)

The Active Record class which this join part is associated ‘about’; for a JoinBase this is the actual base model, for a JoinAssociation this is the target model of the association.



16
17
18
# File 'lib/active_record/associations/join_dependency/join_part.rb', line 16

def base_klass
  @base_klass
end

#childrenObject (readonly)

The Active Record class which this join part is associated ‘about’; for a JoinBase this is the actual base model, for a JoinAssociation this is the target model of the association.



16
17
18
# File 'lib/active_record/associations/join_dependency/join_part.rb', line 16

def children
  @children
end

Instance Method Details

#aliased_table_nameObject

The alias for the active_record’s table

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/active_record/associations/join_dependency/join_part.rb', line 45

def aliased_table_name
  raise NotImplementedError
end

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



34
35
36
37
# File 'lib/active_record/associations/join_dependency/join_part.rb', line 34

def each(&block)
  yield self
  children.each { |child| child.each(&block) }
end

#extract_record(row, column_names_with_alias) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_record/associations/join_dependency/join_part.rb', line 49

def extract_record(row, column_names_with_alias)
  # This code is performance critical as it is called per row.
  # see: https://github.com/rails/rails/pull/12185
  hash = {}

  index = 0
  length = column_names_with_alias.length

  while index < length
    column_name, alias_name = column_names_with_alias[index]
    hash[column_name] = row[alias_name]
    index += 1
  end

  hash
end

#instantiate(row, aliases) ⇒ Object



66
67
68
# File 'lib/active_record/associations/join_dependency/join_part.rb', line 66

def instantiate(row, aliases)
  base_klass.instantiate(extract_record(row, aliases))
end

#match?(other) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/active_record/associations/join_dependency/join_part.rb', line 30

def match?(other)
  self.class == other.class
end

#nameObject



26
27
28
# File 'lib/active_record/associations/join_dependency/join_part.rb', line 26

def name
  reflection.name
end

#tableObject

An Arel::Table for the active_record

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/active_record/associations/join_dependency/join_part.rb', line 40

def table
  raise NotImplementedError
end