Class: FlattenRecord::Meta::AssociatedAttr

Inherits:
NormalizedAttr show all
Defined in:
lib/flatten_record/meta/associated_attr.rb

Direct Known Subclasses

BelongsTo, HasMany, HasOne

Instance Attribute Summary

Attributes inherited from Node

#children, #model, #parent, #target_model

Instance Method Summary collapse

Methods inherited from NormalizedAttr

#[], #all_columns, #associated_models, #id_column, #prefix, #traverse_by

Methods inherited from Node

#prefix, #traverse_by

Constructor Details

#initialize(parent, association, association_klass, model) ⇒ AssociatedAttr

Returns a new instance of AssociatedAttr.



4
5
6
7
8
9
# File 'lib/flatten_record/meta/associated_attr.rb', line 4

def initialize(parent, association, association_klass, model)
  super(parent, association_klass, model)
  @association = Struct.
    new(:foreign_key, :name, :options).
    new(association.foreign_key, association.name, association.options) 
end

Instance Method Details

#denormalize(instance, to_record) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/flatten_record/meta/associated_attr.rb', line 11

def denormalize(instance, to_record)
  normal_s = instance.send(@association.name) 
  return nullify(to_record) if normal_s.blank?

  if normal_s.respond_to?(:find_each)
    to_record = multiply_and_denormalize(normal_s, to_record)
  else
    to_record = denormalize_children(normal_s, to_record)
    to_record = [to_record]
  end
  to_record.flatten
end

#foreign_keyObject



24
25
26
# File 'lib/flatten_record/meta/associated_attr.rb', line 24

def foreign_key
  @association.foreign_key
end

#nullify(to_record) ⇒ Object



28
29
30
31
32
33
# File 'lib/flatten_record/meta/associated_attr.rb', line 28

def nullify(to_record)
  children.each do |child|
    child.nullify(to_record)
  end
  to_record
end