Module: ActiveRecord::NestedAttributes::ClassMethods

Defined in:
lib/active_record/nested_attributes/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#_rails_accepts_nested_attributes_forObject



10
# File 'lib/active_record/nested_attributes/class_methods.rb', line 10

alias _rails_accepts_nested_attributes_for accepts_nested_attributes_for

#accepts_nested_attributes_for(*attr_names) ⇒ Object

rubocop:disable Metrics/AbcSize



13
14
15
16
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 'lib/active_record/nested_attributes/class_methods.rb', line 13

def accepts_nested_attributes_for(*attr_names)
  _rails_accepts_nested_attributes_for(*attr_names)

  # remove any options from the end of the array
  attr_names.extract_options!

  unless respond_to?(:nested_attribute_destruction_watch_associations)
    self.class.define_method(:nested_attribute_destruction_watch_associations) do
      @nested_attribute_destruction_watch_associations || {}
    end
  end

  unless respond_to?(:nested_attribute_destruction_watch_association)
    self.class.define_method(:nested_attribute_destruction_watch_association) do |assoc, type|
      @nested_attribute_destruction_watch_associations ||= {}
      @nested_attribute_destruction_watch_associations[assoc.to_sym] = type
    end
  end

  NestedAttributeDestruction::Monitor.define_hooks(self) if attr_names.any?

  attr_names.each do |association_name|
    reflection = _reflect_on_association(association_name)
    next unless reflection

    type = (reflection.collection? ? :many : :one)

    nested_attribute_destruction_watch_association(association_name, type)
    NestedAttributeDestruction::Monitor.define_predicate(self, association_name)
  end
end