module ActiveGraph::Node::Query::QueryProxyMethodsOfMassUpdating
Public Instance Methods
# File lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb 58 def add_rels(node_or_nodes, original_ids) 59 node_or_nodes.map do |obj| 60 obj if original_ids.include?(obj.id) || _create_relation_or_defer(obj) 61 end.compact 62 end
Deletes the relationship between a node and its last link in the QueryProxy
chain. Executed in the database, callbacks will not run.
# File lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb 38 def delete(node) 39 self.match_to(node).query.delete(rel_var).exec 40 clear_source_object_cache 41 end
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
@param identifier [String,Symbol] the optional identifier of the link in the chain to delete.
# File lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb 26 def delete_all(identifier = nil) 27 query_with_target(identifier) do |target| 28 begin 29 self.query.with(target).optional_match("(#{target})-[#{target}_rel]-()").delete("#{target}, #{target}_rel").exec 30 rescue Neo4j::Driver::Exceptions::ClientException # <=- Seems hacky 31 self.query.delete(target).exec 32 end 33 clear_source_object_cache 34 end 35 end
Deletes the relationships between all nodes for the last step in the QueryProxy
chain. Executed in the database, callbacks will not be run.
# File lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb 44 def delete_all_rels 45 return unless start_object && start_object._persisted_obj 46 self.query.delete(rel_var).exec 47 end
# File lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb 64 def delete_rels_for_nodes(original_ids, new_ids) 65 ids = original_ids.select { |id| !new_ids.include?(id) } 66 return unless ids.present? 67 if association.dependent 68 start_object.public_send("dependent_#{association.dependent}_callback", association, ids) 69 else 70 self.where(id: ids).delete_all_rels 71 end 72 end
Returns all relationships between a node and its last link in the QueryProxy
chain, destroys them in Ruby. Callbacks
will be run.
# File lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb 75 def destroy(node) 76 self.rels_to(node).map!(&:destroy) 77 clear_source_object_cache 78 end
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.
# File lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb 51 def replace_with(node_or_nodes) 52 node_or_nodes = Array(node_or_nodes).map { |arg| arg.is_a?(ActiveGraph::Node) ? arg : @model.find(arg) } 53 original_ids = self.pluck(:id) 54 delete_rels_for_nodes(original_ids, node_or_nodes.collect(&:id)) 55 add_rels(node_or_nodes, original_ids) 56 end
Updates some attributes of a group of nodes within a QP chain. The optional argument makes sense only of `updates` is a string. @param [Hash,String] updates An hash or a string of parameters to be updated. @param [Hash] params An hash of parameters for the update string. It's ignored if `updates` is an Hash.
# File lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb 9 def update_all(updates, params = {}) 10 # Move this to Node module? 11 update_all_with_query(identity, updates, params) 12 end
Updates some attributes of a group of relationships within a QP chain. The optional argument makes sense only of `updates` is a string. @param [Hash,String] updates An hash or a string of parameters to be updated. @param [Hash] params An hash of parameters for the update string. It's ignored if `updates` is an Hash.
# File lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb 18 def update_all_rels(updates, params = {}) 19 fail 'Cannot update rels without a relationship variable.' unless @rel_var 20 update_all_with_query(@rel_var, updates, params) 21 end
Private Instance Methods
# File lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb 93 def clear_source_object_cache 94 self.source_object.clear_association_cache if self.source_object.respond_to?(:clear_association_cache) 95 end
# File lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb 82 def update_all_with_query(var_name, updates, params) 83 query = all.query 84 85 case updates 86 when Hash then query.set(var_name => updates).pluck("count(#{var_name})").first 87 when String then query.set(updates).params(params).pluck("count(#{var_name})").first 88 else 89 fail ArgumentError, "Invalid parameter type #{updates.class} for `updates`." 90 end 91 end