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: Aliases, JoinAssociation, JoinBase, JoinPart

Instance Attribute Summary collapse

Class Method 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 commands 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 # =>  []


95
96
97
98
99
100
101
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 95

def initialize(base, associations, joins)
  @alias_tracker = AliasTracker.create(base.connection, joins)
  @alias_tracker.aliased_name_for(base.table_name, base.table_name) # Updates the count for base.table_name to 1
  tree = self.class.make_tree associations
  @join_root = JoinBase.new base, build(tree, base)
  @join_root.children.each { |child| construct_tables! @join_root, child }
end

Instance Attribute Details

#alias_trackerObject (readonly)

Returns the value of attribute alias_tracker



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

def alias_tracker
  @alias_tracker
end

#base_klassObject (readonly)

Returns the value of attribute base_klass



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

def base_klass
  @base_klass
end

#join_rootObject (readonly)

Returns the value of attribute join_root



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

def join_root
  @join_root
end

Class Method Details

.make_tree(associations) ⇒ Object



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

def self.make_tree(associations)
  hash = {}
  walk_tree associations, hash
  hash
end

.walk_tree(associations, hash) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 56

def self.walk_tree(associations, hash)
  case associations
  when Symbol, String
    hash[associations.to_sym] ||= {}
  when Array
    associations.each do |assoc|
      walk_tree assoc, hash
    end
  when Hash
    associations.each do |k,v|
      cache = hash[k] ||= {}
      walk_tree v, cache
    end
  else
    raise ConfigurationError, associations.inspect
  end
end

Instance Method Details

#aliasesObject



123
124
125
126
127
128
129
130
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 123

def aliases
  Aliases.new join_root.each_with_index.map { |join_part,i|
    columns = join_part.column_names.each_with_index.map { |column_name,j|
      Aliases::Column.new column_name, "t#{i}_r#{j}"
    }
    Aliases::Table.new(join_part, columns)
  }
end

#instantiate(result_set, aliases) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 132

def instantiate(result_set, aliases)
  primary_key = aliases.column_alias(join_root, join_root.primary_key)
  type_caster = result_set.column_type primary_key

  seen = Hash.new { |h,parent_klass|
    h[parent_klass] = Hash.new { |i,parent_id|
      i[parent_id] = Hash.new { |j,child_klass| j[child_klass] = {} }
    }
  }

  model_cache = Hash.new { |h,klass| h[klass] = {} }
  parents = model_cache[join_root]
  column_aliases = aliases.column_aliases join_root

  result_set.each { |row_hash|
    primary_id = type_caster.type_cast row_hash[primary_key]
    parent = parents[primary_id] ||= join_root.instantiate(row_hash, column_aliases)
    construct(parent, join_root, row_hash, result_set, seen, model_cache, aliases)
  }

  parents.values
end

#join_constraints(outer_joins) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 107

def join_constraints(outer_joins)
  joins = join_root.children.flat_map { |child|
    make_inner_joins join_root, child
  }

  joins.concat outer_joins.flat_map { |oj|
    if join_root.match? oj.join_root
      walk join_root, oj.join_root
    else
      oj.join_root.children.flat_map { |child|
        make_outer_joins oj.join_root, child
      }
    end
  }
end

#reflectionsObject



103
104
105
# File 'activerecord/lib/active_record/associations/join_dependency.rb', line 103

def reflections
  join_root.drop(1).map!(&:reflection)
end