Class: ActiveRecord::Associations::ClassMethods::JoinDependency
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::ClassMethods::JoinDependency
- Defined in:
- lib/active_record/associations.rb
Overview
:nodoc:
Defined Under Namespace
Classes: JoinAssociation, JoinBase
Instance Attribute Summary collapse
-
#joins ⇒ Object
readonly
Returns the value of attribute joins.
-
#reflections ⇒ Object
readonly
Returns the value of attribute reflections.
-
#table_aliases ⇒ Object
readonly
Returns the value of attribute table_aliases.
Instance Method Summary collapse
- #count_aliases_from_table_joins(name) ⇒ Object
- #graft(*associations) ⇒ Object
-
#initialize(base, associations, joins) ⇒ JoinDependency
constructor
A new instance of JoinDependency.
- #instantiate(rows) ⇒ Object
- #join_associations ⇒ Object
- #join_base ⇒ Object
- #remove_duplicate_results!(base, records, associations) ⇒ Object
Constructor Details
#initialize(base, associations, joins) ⇒ JoinDependency
Returns a new instance of JoinDependency.
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 |
# File 'lib/active_record/associations.rb', line 1843 def initialize(base, associations, joins) @joins = [JoinBase.new(base, joins)] @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
#joins ⇒ Object (readonly)
Returns the value of attribute joins.
1841 1842 1843 |
# File 'lib/active_record/associations.rb', line 1841 def joins @joins end |
#reflections ⇒ Object (readonly)
Returns the value of attribute reflections.
1841 1842 1843 |
# File 'lib/active_record/associations.rb', line 1841 def reflections @reflections end |
#table_aliases ⇒ Object (readonly)
Returns the value of attribute table_aliases.
1841 1842 1843 |
# File 'lib/active_record/associations.rb', line 1841 def table_aliases @table_aliases end |
Instance Method Details
#count_aliases_from_table_joins(name) ⇒ Object
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 |
# File 'lib/active_record/associations.rb', line 1870 def count_aliases_from_table_joins(name) # quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase quoted_name = join_base.active_record.connection.quote_table_name(name.downcase).downcase join_sql = join_base.table_joins.to_s.downcase join_sql.blank? ? 0 : # Table names join_sql.scan(/join(?:\s+\w+)?\s+#{quoted_name}\son/).size + # Table aliases join_sql.scan(/join(?:\s+\w+)?\s+\S+\s+#{quoted_name}\son/).size end |
#graft(*associations) ⇒ Object
1854 1855 1856 1857 1858 1859 1860 |
# File 'lib/active_record/associations.rb', line 1854 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
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 |
# File 'lib/active_record/associations.rb', line 1881 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_associations ⇒ Object
1862 1863 1864 |
# File 'lib/active_record/associations.rb', line 1862 def join_associations @joins[1..-1].to_a end |
#join_base ⇒ Object
1866 1867 1868 |
# File 'lib/active_record/associations.rb', line 1866 def join_base @joins[0] end |
#remove_duplicate_results!(base, records, associations) ⇒ Object
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 |
# File 'lib/active_record/associations.rb', line 1893 def remove_duplicate_results!(base, records, associations) case associations when Symbol, String reflection = base.reflections[associations] remove_uniq_by_reflection(reflection, records) when Array associations.each do |association| remove_duplicate_results!(base, records, association) end when Hash associations.keys.each do |name| reflection = base.reflections[name] remove_uniq_by_reflection(reflection, records) parent_records = [] records.each do |record| if descendant = record.send(reflection.name) if reflection.collection? parent_records.concat descendant.target.uniq else parent_records << descendant end end end remove_duplicate_results!(reflection.klass, parent_records, associations[name]) unless parent_records.empty? end end end |