Module: ObjectAttorney::NestedObjects

Defined in:
lib/object_attorney/nested_objects.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#initialize_nested_attributesObject



6
7
8
9
10
# File 'lib/object_attorney/nested_objects.rb', line 6

def initialize_nested_attributes
  self.class.reflect_on_all_associations.each do |reflection|
    self.instance_variable_set("@#{reflection.name}_attributes", {})
  end
end

#mark_for_destructionObject



12
13
14
15
16
# File 'lib/object_attorney/nested_objects.rb', line 12

def mark_for_destruction
  represented_object.respond_to?(:mark_for_destruction) && represented_object.mark_for_destruction

  @marked_for_destruction = true
end

#mark_for_destruction_if_necessary(object, attributes) ⇒ Object



26
27
28
29
30
# File 'lib/object_attorney/nested_objects.rb', line 26

def mark_for_destruction_if_necessary(object, attributes)
  return nil unless attributes.is_a?(Hash)

  object.mark_for_destruction if attributes_order_destruction? attributes
end

#marked_for_destruction?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# File 'lib/object_attorney/nested_objects.rb', line 18

def marked_for_destruction?
  if represented_object.respond_to?(:marked_for_destruction?) && represented_object.marked_for_destruction?
    @marked_for_destruction = true
  end

  @marked_for_destruction
end

#nested_objects(macro = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/object_attorney/nested_objects.rb', line 32

def nested_objects(macro = nil)
  nested_objects_list = []

  self.class.reflect_on_all_associations(macro).each do |reflection|
    [*self.send(reflection.name)].each do |nested_object|
      nested_objects_list << [reflection, nested_object]
    end
  end

  nested_objects_list
end

#reset_nested_objects(reflection_name = nil) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/object_attorney/nested_objects.rb', line 44

def reset_nested_objects(reflection_name = nil)
  self.class.reflect_on_all_associations.each do |reflection|
    next if !reflection_name.nil? && reflection_name != reflection.name

    self.instance_variable_set("@#{reflection.name}_attributes", {})
    self.instance_variable_set("@#{reflection.name}", nil)
  end
end