Class: ActiveRecord::FinderMethods::SunstoneJoinDependency

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

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ SunstoneJoinDependency

Returns a new instance of SunstoneJoinDependency.



5
6
7
# File 'ext/active_record/relation/finder_methods.rb', line 5

def initialize(klass)
  @klass = klass
end

Instance Method Details

#apply_column_aliases(relation) ⇒ Object



13
14
15
# File 'ext/active_record/relation/finder_methods.rb', line 13

def apply_column_aliases(relation)
  relation
end

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



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

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'ext/active_record/relation/finder_methods.rb', line 71

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'ext/active_record/relation/finder_methods.rb', line 102

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'ext/active_record/relation/finder_methods.rb', line 17

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



9
10
11
# File 'ext/active_record/relation/finder_methods.rb', line 9

def reflections
  []
end