Class: ActiveRecord::Associations::JoinDependency

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/associations/join_dependency.rb,
activerecord/lib/active_record/associations/join_dependency/join_base.rb,
activerecord/lib/active_record/associations/join_dependency/join_part.rb,
activerecord/lib/active_record/associations/join_dependency/join_association.rb

Overview

:nodoc:

Defined Under Namespace

Classes: JoinAssociation, JoinBase, JoinPart

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, associations, joins) ⇒ JoinDependency

base is the base class on which operation is taking place. associations is the list of associations which are joined using hash, symbol or array. joins is the list of all string join commnads and arel nodes.

Example :

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, through: :appointments
end

If I execute `@physician.patients.to_a` then
  base #=> Physician
  associations #=> []
  joins #=>  [#<Arel::Nodes::InnerJoin: ...]

However if I execute `Physician.joins(:appointments).to_a` then
  base #=> Physician
  associations #=> [:appointments]
  joins #=>  []


31
32
33
34
35
36
37
38
39
40
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 31

def initialize(base, associations, joins)
  @base_klass    = base
  @table_joins   = joins
  @join_parts    = [JoinBase.new(base)]
  @associations  = {}
  @reflections   = []
  @alias_tracker = AliasTracker.new(base.connection, joins)
  @alias_tracker.aliased_name_for(base.table_name) # Updates the count for base.table_name to 1
  build(associations)
end

Instance Attribute Details

#alias_trackerObject (readonly)

Returns the value of attribute alias_tracker



8
9
10
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 8

def alias_tracker
  @alias_tracker
end

#base_klassObject (readonly)

Returns the value of attribute base_klass



8
9
10
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 8

def base_klass
  @base_klass
end

#join_partsObject (readonly)

Returns the value of attribute join_parts



8
9
10
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 8

def join_parts
  @join_parts
end

#reflectionsObject (readonly)

Returns the value of attribute reflections



8
9
10
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 8

def reflections
  @reflections
end

Instance Method Details

#columnsObject



64
65
66
67
68
69
70
71
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 64

def columns
  join_parts.collect { |join_part|
    table = join_part.aliased_table
    join_part.column_names_with_alias.collect{ |column_name, aliased_name|
      table[column_name].as Arel.sql(aliased_name)
    }
  }.flatten
end

#graft(*associations) ⇒ Object



42
43
44
45
46
47
48
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 42

def graft(*associations)
  associations.each do |association|
    join_associations.detect {|a| association == a} ||
      build(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type)
  end
  self
end

#instantiate(rows) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 73

def instantiate(rows)
  primary_key = join_base.aliased_primary_key
  parents = {}

  records = rows.map { |model|
    primary_id = model[primary_key]
    parent = parents[primary_id] ||= join_base.instantiate(model)
    construct(parent, @associations, join_associations, model)
    parent
  }.uniq

  remove_duplicate_results!(base_klass, records, @associations)
  records
end

#join_associationsObject



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

def join_associations
  join_parts.drop 1
end

#join_baseObject



54
55
56
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 54

def join_base
  join_parts.first
end

#join_relation(relation) ⇒ Object



58
59
60
61
62
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 58

def join_relation(relation)
  join_associations.inject(relation) do |rel,association|
    association.join_relation(rel)
  end
end