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
# File 'lib/object_attorney/nested_objects.rb', line 12

def mark_for_destruction
  @marked_for_destruction = true
end

#mark_for_destruction_if_necessary(object, attributes) ⇒ Object



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

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)


16
17
18
# File 'lib/object_attorney/nested_objects.rb', line 16

def marked_for_destruction?
  @marked_for_destruction
end

#nested_objects(macro = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/object_attorney/nested_objects.rb', line 26

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