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

Inherits:
Object
  • Object
show all
Defined in:
activerecord/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) ⇒ JoinPart

Returns a new instance of JoinPart.



18
19
20
21
22
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 18

def initialize(base_klass)
  @base_klass = base_klass
  @cached_record = {}
  @column_names_with_alias = nil
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.



14
15
16
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 14

def base_klass
  @base_klass
end

Instance Method Details

#==(other) ⇒ Object

Raises:

  • (NotImplementedError)


28
29
30
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 28

def ==(other)
  raise NotImplementedError
end

#aliased_prefixObject

The prefix to be used when aliasing columns in the active_record’s table

Raises:

  • (NotImplementedError)


38
39
40
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 38

def aliased_prefix
  raise NotImplementedError
end

#aliased_primary_keyObject

The alias for the primary key of the active_record’s table



48
49
50
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 48

def aliased_primary_key
  "#{aliased_prefix}_r0"
end

#aliased_tableObject



24
25
26
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 24

def aliased_table
  Arel::Nodes::TableAlias.new table, aliased_table_name
end

#aliased_table_nameObject

The alias for the active_record’s table

Raises:

  • (NotImplementedError)


43
44
45
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 43

def aliased_table_name
  raise NotImplementedError
end

#column_names_with_aliasObject

An array of [column_name, alias] pairs for the table



53
54
55
56
57
58
59
60
61
62
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 53

def column_names_with_alias
  unless @column_names_with_alias
    @column_names_with_alias = []

    ([primary_key] + (column_names - [primary_key])).compact.each_with_index do |column_name, i|
      @column_names_with_alias << [column_name, "#{aliased_prefix}_r#{i}"]
    end
  end
  @column_names_with_alias
end

#extract_record(row) ⇒ Object



64
65
66
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 64

def extract_record(row)
  Hash[column_names_with_alias.map{|cn, an| [cn, row[an]]}]
end

#instantiate(row) ⇒ Object



72
73
74
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 72

def instantiate(row)
  @cached_record[record_id(row)] ||= base_klass.instantiate(extract_record(row))
end

#record_id(row) ⇒ Object



68
69
70
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 68

def record_id(row)
  row[aliased_primary_key]
end

#tableObject

An Arel::Table for the active_record

Raises:

  • (NotImplementedError)


33
34
35
# File 'activerecord/lib/active_record/associations/join_dependency/join_part.rb', line 33

def table
  raise NotImplementedError
end