Class: ActiveRecord::Associations::JoinDependency

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

Defined Under Namespace

Classes: Aliases, JoinAssociation

Instance Method Summary collapse

Instance Method Details

#construct(ar_parent, parent, row, seen, model_cache, strict_loading_value) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
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
# File 'lib/composite_primary_keys/associations/join_association.rb', line 92

def construct(ar_parent, parent, row, seen, model_cache, strict_loading_value)
  return if ar_parent.nil?

  parent.children.each do |node|
    if node.reflection.collection?
      other = ar_parent.association(node.reflection.name)
      other.loaded!
    elsif ar_parent.association_cached?(node.reflection.name)
      model = ar_parent.association(node.reflection.name).target
      construct(model, node, row, seen, model_cache, strict_loading_value)
      next
    end

    key = aliases.column_alias(node, node.primary_key)
    # CPK
    if key.is_a?(Array)
      id = Array(key).map do |column_alias|
        row[column_alias]
      end
      # At least the first value in the key has to be set.  Should we require all values to be set?
      id = nil if id.first.nil?
    else # original
      id = row[key]
    end

    if id.nil?
      nil_association = ar_parent.association(node.reflection.name)
      nil_association.loaded!
      next
    end

    model = seen[ar_parent][node][id]

    if model
      construct(model, node, row, seen, model_cache, strict_loading_value)
    else
      model = construct_model(ar_parent, node, row, model_cache, id, strict_loading_value)

      seen[ar_parent][node][id] = model
      construct(model, node, row, seen, model_cache, strict_loading_value)
    end
  end
end

#instantiate(result_set, strict_loading_value, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/composite_primary_keys/associations/join_association.rb', line 37

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

  message_bus = ActiveSupport::Notifications.instrumenter

  payload = {
    record_count: result_set.length,
    class_name: join_root.base_klass.name
  }

  message_bus.instrument("instantiation.active_record", payload) do
    result_set.each { |row_hash|
      # CPK
      # parent_key = primary_key ? row_hash[primary_key] : row_hash
      parent_key = if primary_key.kind_of?(Array)
                     primary_key.map {|key| row_hash[key]}
                   else
                     primary_key ? row_hash[primary_key] : row_hash
                   end

      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