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.



1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
# File 'lib/active_record/associations.rb', line 1750

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.



1748
1749
1750
# File 'lib/active_record/associations.rb', line 1748

def joins
  @joins
end

#reflectionsObject (readonly)

Returns the value of attribute reflections.



1748
1749
1750
# File 'lib/active_record/associations.rb', line 1748

def reflections
  @reflections
end

#table_aliasesObject (readonly)

Returns the value of attribute table_aliases.



1748
1749
1750
# File 'lib/active_record/associations.rb', line 1748

def table_aliases
  @table_aliases
end

Instance Method Details

#instantiate(rows) ⇒ Object



1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
# File 'lib/active_record/associations.rb', line 1769

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



1761
1762
1763
# File 'lib/active_record/associations.rb', line 1761

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

#join_baseObject



1765
1766
1767
# File 'lib/active_record/associations.rb', line 1765

def join_base
  @joins[0]
end

#join_for_table_name(table_name) ⇒ Object



1809
1810
1811
1812
1813
# File 'lib/active_record/associations.rb', line 1809

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



1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
# File 'lib/active_record/associations.rb', line 1815

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



1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
# File 'lib/active_record/associations.rb', line 1781

def remove_duplicate_results!(base, records, associations)
  case associations
    when Symbol, String
      reflection = base.reflections[associations]
      if reflection && [:has_many, :has_and_belongs_to_many].include?(reflection.macro)
        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]
        is_collection = [:has_many, :has_and_belongs_to_many].include?(reflection.macro)

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

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