Module: ActiveGraph::Node::Query::QueryProxyMethodsOfMassUpdating

Included in:
QueryProxy
Defined in:
lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb

Instance Method Summary collapse

Instance Method Details

#add_rels(node_or_nodes, original_ids) ⇒ Object



58
59
60
61
62
# File 'lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb', line 58

def add_rels(node_or_nodes, original_ids)
  node_or_nodes.map do |obj|
    obj if original_ids.include?(obj.id) || _create_relation_or_defer(obj)
  end.compact
end

#delete(node) ⇒ Object

Deletes the relationship between a node and its last link in the QueryProxy chain. Executed in the database, callbacks will not run.



38
39
40
41
# File 'lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb', line 38

def delete(node)
  self.match_to(node).query.delete(rel_var).exec
  clear_source_object_cache
end

#delete_all(identifier = nil) ⇒ Object

Deletes a group of nodes and relationships within a QP chain. When identifier is omitted, it will remove the last link in the chain. The optional argument must be a node identifier. A relationship identifier will result in a Cypher Error

Parameters:

  • identifier (String, Symbol) (defaults to: nil)

    the optional identifier of the link in the chain to delete.



26
27
28
29
30
31
32
33
34
35
# File 'lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb', line 26

def delete_all(identifier = nil)
  query_with_target(identifier) do |target|
    begin
      self.query.with(target).optional_match("(#{target})-[#{target}_rel]-()").delete("#{target}, #{target}_rel").exec
    rescue Neo4j::Driver::Exceptions::ClientException # <=- Seems hacky
      self.query.delete(target).exec
    end
    clear_source_object_cache
  end
end

#delete_all_relsObject

Deletes the relationships between all nodes for the last step in the QueryProxy chain. Executed in the database, callbacks will not be run.



44
45
46
47
# File 'lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb', line 44

def delete_all_rels
  return unless start_object && start_object._persisted_obj
  self.query.delete(rel_var).exec
end

#delete_rels_for_nodes(original_ids, new_ids) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb', line 64

def delete_rels_for_nodes(original_ids, new_ids)
  ids = original_ids.select { |id| !new_ids.include?(id) }
  return unless ids.present?
  if association.dependent
    start_object.public_send("dependent_#{association.dependent}_callback", association, ids)
  else
    self.where(id: ids).delete_all_rels
  end
end

#destroy(node) ⇒ Object

Returns all relationships between a node and its last link in the QueryProxy chain, destroys them in Ruby. Callbacks will be run.



75
76
77
78
# File 'lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb', line 75

def destroy(node)
  self.rels_to(node).map!(&:destroy)
  clear_source_object_cache
end

#replace_with(node_or_nodes) ⇒ Object

Deletes the relationships between all nodes for the last step in the QueryProxy chain and replaces them with relationships to the given nodes. Executed in the database, callbacks will not be run.



51
52
53
54
55
56
# File 'lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb', line 51

def replace_with(node_or_nodes)
  node_or_nodes = Array(node_or_nodes).map { |arg| arg.is_a?(ActiveGraph::Node) ? arg : @model.find(arg) }
  original_ids = self.pluck(:id)
  delete_rels_for_nodes(original_ids, node_or_nodes.collect(&:id))
  add_rels(node_or_nodes, original_ids)
end

#update_all(updates, params = {}) ⇒ Object

Updates some attributes of a group of nodes within a QP chain. The optional argument makes sense only of ‘updates` is a string.

Parameters:

  • updates (Hash, String)

    An hash or a string of parameters to be updated.

  • params (Hash) (defaults to: {})

    An hash of parameters for the update string. It’s ignored if ‘updates` is an Hash.



9
10
11
12
# File 'lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb', line 9

def update_all(updates, params = {})
  # Move this to Node module?
  update_all_with_query(identity, updates, params)
end

#update_all_rels(updates, params = {}) ⇒ Object

Updates some attributes of a group of relationships within a QP chain. The optional argument makes sense only of ‘updates` is a string.

Parameters:

  • updates (Hash, String)

    An hash or a string of parameters to be updated.

  • params (Hash) (defaults to: {})

    An hash of parameters for the update string. It’s ignored if ‘updates` is an Hash.



18
19
20
21
# File 'lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb', line 18

def update_all_rels(updates, params = {})
  fail 'Cannot update rels without a relationship variable.' unless @rel_var
  update_all_with_query(@rel_var, updates, params)
end