Module: Neo4j::Rails::NestedAttributes

Extended by:
ActiveSupport::Concern, TxMethods
Included in:
Model
Defined in:
lib/neo4j/rails/nested_attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary (collapse)

Methods included from TxMethods

tx_methods

Instance Method Details

- (Object) update_nested_attributes(rel_type, attr, options)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/neo4j/rails/nested_attributes.rb', line 7

def update_nested_attributes(rel_type, attr, options)
  allow_destroy, reject_if = [options[:allow_destroy], options[:reject_if]] if options
  begin
    # Check if we want to destroy not found nodes (e.g. {..., :_destroy => '1' } ?
    destroy = attr.delete(:_destroy)
    found = _find_node(rel_type, attr[:id]) || Neo4j::Rails::Model.find(attr[:id])
    if allow_destroy && destroy && destroy != '0'
      found.destroy if found
    else
      if not found
        _create_entity(rel_type, attr) #Create new node from scratch
      else
        #Create relationship to existing node in case it doesn't exist already
        _add_relationship(rel_type, found) if (not _has_relationship(rel_type, attr[:id]))
        found.update_attributes(attr)
      end
    end
  end unless reject_if?(reject_if, attr)
end