Module: MongoModel::AttributeMethods::Nested::ClassMethods

Defined in:
lib/mongomodel/concerns/attribute_methods/nested.rb

Instance Method Summary collapse

Instance Method Details

#accepts_nested_attributes_for(*attr_names) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mongomodel/concerns/attribute_methods/nested.rb', line 12

def accepts_nested_attributes_for(*attr_names)
  options = attr_names.extract_options!

  attr_names.each do |attr_name|
    type = property_type(attr_name)

    nested_attributes_options = self.nested_attributes_options.dup
    nested_attributes_options[attr_name.to_sym] = options
    self.nested_attributes_options = nested_attributes_options

    class_eval <<-EORUBY, __FILE__, __LINE__ + 1
      if method_defined?(:#{attr_name}_attributes=)
        remove_method(:#{attr_name}_attributes=)
      end

      def #{attr_name}_attributes=(attributes)
        assign_nested_attributes_for_#{type}(:#{attr_name}, attributes)
      end
    EORUBY
  end
end