Class: ActiveRecord::Associations::JoinDependency
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::JoinDependency
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/active_record/associations/join_dependency.rb,
lib/active_record/associations/join_dependency/join_base.rb,
lib/active_record/associations/join_dependency/join_part.rb,
lib/active_record/associations/join_dependency/join_association.rb more...
Overview
:nodoc:
Defined Under Namespace
Classes: Aliases, JoinAssociation, JoinBase, JoinPart
Class Method Summary collapse
Instance Method Summary collapse
- #apply_column_aliases(relation) ⇒ Object
- #base_klass ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(base, table, associations, join_type) ⇒ JoinDependency
constructor
A new instance of JoinDependency.
- #instantiate(result_set, strict_loading_value, &block) ⇒ Object
- #join_constraints(joins_to_add, alias_tracker, references) ⇒ Object
- #reflections ⇒ Object
Constructor Details
#initialize(base, table, associations, join_type) ⇒ JoinDependency
Returns a new instance of JoinDependency.
71 72 73 74 75 |
# File 'lib/active_record/associations/join_dependency.rb', line 71 def initialize(base, table, associations, join_type) tree = self.class.make_tree associations @join_root = JoinBase.new(base, table, build(tree, base)) @join_type = join_type end |
Class Method Details
.make_tree(associations) ⇒ Object
[View source]
47 48 49 50 51 |
# File 'lib/active_record/associations/join_dependency.rb', line 47 def self.make_tree(associations) hash = {} walk_tree associations, hash hash end |
.walk_tree(associations, hash) ⇒ Object
[View source]
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/active_record/associations/join_dependency.rb', line 53 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 if v end else raise ConfigurationError, associations.inspect end end |
Instance Method Details
#apply_column_aliases(relation) ⇒ Object
[View source]
153 154 155 156 |
# File 'lib/active_record/associations/join_dependency.rb', line 153 def apply_column_aliases(relation) @join_root_alias = relation.select_values.empty? relation._select!(-> { aliases.columns }) end |
#base_klass ⇒ Object
[View source]
77 78 79 |
# File 'lib/active_record/associations/join_dependency.rb', line 77 def base_klass join_root.base_klass end |
#each(&block) ⇒ Object
[View source]
158 159 160 |
# File 'lib/active_record/associations/join_dependency.rb', line 158 def each(&block) join_root.each(&block) end |
#instantiate(result_set, strict_loading_value, &block) ⇒ Object
[View source]
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/active_record/associations/join_dependency.rb', line 105 def instantiate(result_set, strict_loading_value, &block) primary_key = aliases.column_alias(join_root, join_root.primary_key) seen = Hash.new { |i, parent| i[parent] = Hash.new { |j, child_class| j[child_class] = {} } }.compare_by_identity model_cache = Hash.new { |h, klass| h[klass] = {} } parents = model_cache[join_root] column_aliases = aliases.column_aliases(join_root) column_names = [] result_set.columns.each do |name| column_names << name unless /\At\d+_r\d+\z/.match?(name) end if column_names.empty? column_types = {} else column_types = result_set.column_types unless column_types.empty? attribute_types = join_root.attribute_types column_types = column_types.slice(*column_names).delete_if { |k, _| attribute_types.key?(k) } end column_aliases += column_names.map! { |name| Aliases::Column.new(name, name) } end = ActiveSupport::Notifications.instrumenter payload = { record_count: result_set.length, class_name: join_root.base_klass.name } .instrument("instantiation.active_record", payload) do result_set.each { |row_hash| parent_key = primary_key ? row_hash[primary_key] : row_hash parent = parents[parent_key] ||= join_root.instantiate(row_hash, column_aliases, column_types, &block) construct(parent, join_root, row_hash, seen, model_cache, strict_loading_value) } end parents.values end |
#join_constraints(joins_to_add, alias_tracker, references) ⇒ Object
[View source]
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/active_record/associations/join_dependency.rb', line 85 def join_constraints(joins_to_add, alias_tracker, references) @alias_tracker = alias_tracker @joined_tables = {} @references = {} references.each do |table_name| @references[table_name.to_sym] = table_name if table_name.is_a?(Arel::Nodes::SqlLiteral) end unless references.empty? joins = make_join_constraints(join_root, join_type) joins.concat joins_to_add.flat_map { |oj| if join_root.match? oj.join_root walk(join_root, oj.join_root, oj.join_type) else make_join_constraints(oj.join_root, oj.join_type) end } end |
#reflections ⇒ Object
[View source]
81 82 83 |
# File 'lib/active_record/associations/join_dependency.rb', line 81 def reflections join_root.drop(1).map!(&:reflection) end |