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.



1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
# File 'lib/active_record/associations.rb', line 1598

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.



1596
1597
1598
# File 'lib/active_record/associations.rb', line 1596

def joins
  @joins
end

#reflectionsObject (readonly)

Returns the value of attribute reflections.



1596
1597
1598
# File 'lib/active_record/associations.rb', line 1596

def reflections
  @reflections
end

#table_aliasesObject (readonly)

Returns the value of attribute table_aliases.



1596
1597
1598
# File 'lib/active_record/associations.rb', line 1596

def table_aliases
  @table_aliases
end

Instance Method Details

#instantiate(rows) ⇒ Object



1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
# File 'lib/active_record/associations.rb', line 1617

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



1609
1610
1611
# File 'lib/active_record/associations.rb', line 1609

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

#join_baseObject



1613
1614
1615
# File 'lib/active_record/associations.rb', line 1613

def join_base
  @joins[0]
end

#join_for_table_name(table_name) ⇒ Object



1657
1658
1659
1660
1661
# File 'lib/active_record/associations.rb', line 1657

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



1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
# File 'lib/active_record/associations.rb', line 1663

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



1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
# File 'lib/active_record/associations.rb', line 1629

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