Class: ActiveRecord::Associations::ClassMethods::JoinDependency

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/associations.rb

Overview

:nodoc:

Direct Known Subclasses

InnerJoinDependency

Defined Under Namespace

Classes: JoinAssociation, JoinBase

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, associations, joins) ⇒ JoinDependency

Returns a new instance of JoinDependency.



1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
# File 'lib/active_record/associations.rb', line 1827

def initialize(base, associations, joins)
  @joins                 = [JoinBase.new(base, joins)]
  @associations          = associations
  @reflections           = []
  @base_records_hash     = {}
  @base_records_in_order = []
  @table_aliases         = Hash.new { |aliases, table| aliases[table] = 0 }
  @table_aliases[base.table_name] = 1
  build(associations)
end

Instance Attribute Details

#joinsObject (readonly)

Returns the value of attribute joins.



1825
1826
1827
# File 'lib/active_record/associations.rb', line 1825

def joins
  @joins
end

#reflectionsObject (readonly)

Returns the value of attribute reflections.



1825
1826
1827
# File 'lib/active_record/associations.rb', line 1825

def reflections
  @reflections
end

#table_aliasesObject (readonly)

Returns the value of attribute table_aliases.



1825
1826
1827
# File 'lib/active_record/associations.rb', line 1825

def table_aliases
  @table_aliases
end

Instance Method Details

#instantiate(rows) ⇒ Object



1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
# File 'lib/active_record/associations.rb', line 1846

def instantiate(rows)
  rows.each_with_index do |row, i|
    primary_id = join_base.record_id(row)
    unless @base_records_hash[primary_id]
      @base_records_in_order << (@base_records_hash[primary_id] = join_base.instantiate(row))
    end
    construct(@base_records_hash[primary_id], @associations, join_associations.dup, row)
  end
  remove_duplicate_results!(join_base.active_record, @base_records_in_order, @associations)
  return @base_records_in_order
end

#join_associationsObject



1838
1839
1840
# File 'lib/active_record/associations.rb', line 1838

def join_associations
  @joins[1..-1].to_a
end

#join_baseObject



1842
1843
1844
# File 'lib/active_record/associations.rb', line 1842

def join_base
  @joins[0]
end

#join_for_table_name(table_name) ⇒ Object



1885
1886
1887
1888
1889
# File 'lib/active_record/associations.rb', line 1885

def join_for_table_name(table_name)
  join = (@joins.select{|j|j.aliased_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first) rescue nil
  return join unless join.nil?
  @joins.select{|j|j.is_a?(JoinAssociation) && j.aliased_join_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first rescue nil
end

#joins_for_table_name(table_name) ⇒ Object



1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
# File 'lib/active_record/associations.rb', line 1891

def joins_for_table_name(table_name)
  join = join_for_table_name(table_name)
  result = nil
  if join && join.is_a?(JoinAssociation)
    result = [join]
    if join.parent && join.parent.is_a?(JoinAssociation)
      result = joins_for_table_name(join.parent.aliased_table_name) +
               result
    end
  end
  result
end

#remove_duplicate_results!(base, records, associations) ⇒ Object



1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
# File 'lib/active_record/associations.rb', line 1858

def remove_duplicate_results!(base, records, associations)
  case associations
    when Symbol, String
      reflection = base.reflections[associations]
      if reflection && reflection.collection?
        records.each { |record| record.send(reflection.name).target.uniq! }
      end
    when Array
      associations.each do |association|
        remove_duplicate_results!(base, records, association)
      end
    when Hash
      associations.keys.each do |name|
        reflection = base.reflections[name]

        parent_records = records.map do |record|
          descendant = record.send(reflection.name)
          next unless descendant
          descendant.target.uniq! if reflection.collection?
          descendant
        end.flatten.compact

        remove_duplicate_results!(reflection.klass, parent_records, associations[name]) unless parent_records.empty?
      end
  end
end