Module: Neo4j::Rails::NestedAttributes::ClassMethods

Defined in:
lib/neo4j/rails/nested_attributes.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) accepts_nested_attributes_for(*attr_names)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/neo4j/rails/nested_attributes.rb', line 30

def accepts_nested_attributes_for(*attr_names)
  options = attr_names.pop if attr_names[-1].is_a?(Hash)

  attr_names.each do |association_name|
    # Do some validation that we have defined the relationships we want to nest
    rel = self._decl_rels[association_name.to_sym]
    raise "No relationship declared with has_n or has_one with type #{association_name}" unless rel
    raise "Can't use accepts_nested_attributes_for(#{association_name}) since it has not defined which class it has a relationship to, use has_n(#{association_name}).to(MyOtherClass)" unless rel.target_class

    if rel.has_one?
      send(:define_method, "#{association_name}_attributes=") do |attributes|
        update_nested_attributes(association_name.to_sym, attributes, options)
      end
    else
      send(:define_method, "#{association_name}_attributes=") do |attributes|
        if attributes.is_a?(Array)
          attributes.each do |attr|
            update_nested_attributes(association_name.to_sym, attr, options)
          end
        else
          attributes.each_value do |attr|
            update_nested_attributes(association_name.to_sym, attr, options)
          end
        end
      end
    end

  end
end