Class: ActiveRecord::FinderMethods::SunstoneJoinDependency

Inherits:
Object
  • Object
show all
Defined in:
ext/active_record/finder_methods.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ SunstoneJoinDependency

Returns a new instance of SunstoneJoinDependency.



84
85
86
# File 'ext/active_record/finder_methods.rb', line 84

def initialize(klass)
  @klass = klass
end

Instance Method Details

#apply_column_aliases(relation) ⇒ Object



92
93
94
# File 'ext/active_record/finder_methods.rb', line 92

def apply_column_aliases(relation)
  relation
end

#construct(parent, relations, seen, model_cache, strict_loading_value) ⇒ Object



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
# File 'ext/active_record/finder_methods.rb', line 124

def construct(parent, relations, seen, model_cache, strict_loading_value)
  relations.each do |key, attributes|
    reflection = parent.class.reflect_on_association(key)
    next unless reflection

    if reflection.collection?
      other = parent.association(reflection.name)
      other.loaded!
    else
      if parent.association_cached?(reflection.name)
        model = parent.association(reflection.name).target
        construct(model, attributes.select{|k,v| !model.class.column_names.include?(k.to_s) }, seen, model_cache, strict_loading_value)
      end
    end

    if !reflection.collection?
      construct_association(parent, reflection, attributes, seen, model_cache, strict_loading_value)
    else
      attributes.each do |row|
        construct_association(parent, reflection, row, seen, model_cache, strict_loading_value)
      end
    end

  end
end

#construct_association(parent, reflection, attributes, seen, model_cache, strict_loading_value) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'ext/active_record/finder_methods.rb', line 150

def construct_association(parent, reflection, attributes, seen, model_cache, strict_loading_value)
  return if attributes.nil?

  klass = if reflection.polymorphic?
    parent.send(reflection.foreign_type).constantize.base_class
  else
    reflection.klass
  end
  id = attributes[klass.primary_key]
  model = seen[parent.object_id][klass][id]

  if model
    construct(model, attributes.select{|k,v| !klass.column_names.include?(k.to_s) }, seen, model_cache, strict_loading_value)

    other = parent.association(reflection.name)

    if reflection.collection?
      other.target.push(model)
    else
      other.target = model
    end

    other.set_inverse_instance(model)
  else
    model = construct_model(parent, reflection, id, attributes.select{|k,v| klass.column_names.include?(k.to_s) }, seen, model_cache, strict_loading_value)
    seen[parent.object_id][model.class.base_class][id] = model
    construct(model, attributes.select{|k,v| !klass.column_names.include?(k.to_s) }, seen, model_cache, strict_loading_value)
  end
end

#construct_model(record, reflection, id, attributes, seen, model_cache, strict_loading_value) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'ext/active_record/finder_methods.rb', line 181

def construct_model(record, reflection, id, attributes, seen, model_cache, strict_loading_value)
  klass = if reflection.polymorphic?
    record.send(reflection.foreign_type).constantize
  else
    reflection.klass
  end

  model = model_cache[klass][id] ||= klass.instantiate(attributes)
  other = record.association(reflection.name)

  if reflection.collection?
    other.target.push(model)
  else
    other.target = model
  end

  other.set_inverse_instance(model)
  model
end

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



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
# File 'ext/active_record/finder_methods.rb', line 96

def instantiate(result_set, strict_loading_value, &block)
  seen = Hash.new { |i, object_id|
    i[object_id] = Hash.new { |j, child_class|
      j[child_class] = {}
    }
  }

  model_cache = Hash.new { |h, klass| h[klass] = {} }
  parents = model_cache[@klass]

  message_bus = ActiveSupport::Notifications.instrumenter

  payload = {
    record_count: result_set.length,
    class_name: @klass.name
  }

  message_bus.instrument("instantiation.active_record", payload) do
    result_set.each { |row_hash|
      parent_key = @klass.primary_key ? row_hash[@klass.primary_key] : row_hash
      parent = parents[parent_key] ||= @klass.instantiate(row_hash.select{|k,v| @klass.column_names.include?(k.to_s) }, &block)
      construct(parent, row_hash.select{|k,v| !@klass.column_names.include?(k.to_s) }, seen, model_cache, strict_loading_value)
    }
  end

  parents.values
end

#reflectionsObject



88
89
90
# File 'ext/active_record/finder_methods.rb', line 88

def reflections
  []
end