lib/neo4j/active_rel/persistence.rb in neo4j-7.2.3 vs lib/neo4j/active_rel/persistence.rb in neo4j-8.0.0.alpha.1

- old
+ new

@@ -38,47 +38,73 @@ # Increments concurrently a numeric attribute by a centain amount # @param [Symbol, String] name of the attribute to increment # @param [Integer, Float] amount to increment def concurrent_increment!(attribute, by = 1) - query_rel = Neo4j::Session.query.match('()-[n]-()').where(n: {neo_id: neo_id}) - increment_by_query! query_rel, attribute, by + increment_by_query! query_as(:n), attribute, by end def create_model validate_node_classes! rel = _create_rel return self unless rel.respond_to?(:props) init_on_load(rel, from_node, to_node, @rel_type) true end + def query_as(var) + # This should query based on the nodes, not the rel neo_id, I think + # Also, picky point: Should the var be `n`? + self.class.query_as(neo_id, var) + end + module ClassMethods # Creates a new relationship between objects # @param [Hash] props the properties the new relationship should have - def create(*args) - new(*args).tap(&:save) + def create(props = {}) + relationship_props = extract_association_attributes!(props) || {} + new(props).tap do |obj| + relationship_props.each do |prop, value| + obj.send("#{prop}=", value) + end + obj.save + end end # Same as #create, but raises an error if there is a problem during save. def create!(*args) - new(*args).tap(&:save!) + props = args[0] || {} + relationship_props = extract_association_attributes!(props) || {} + new(props).tap do |obj| + relationship_props.each do |prop, value| + obj.send("#{prop}=", value) + end + obj.save! + end end def create_method creates_unique? ? :create_unique : :create end def load_entity(id) - Neo4j::Relationship.load(id) + query_as(id).pluck(:r).first end + + def query_as(neo_id, var = :r) + Neo4j::ActiveBase.new_query.match("()-[#{var}]-()").where(var => {neo_id: neo_id}) + end end def create_method self.class.create_method end private + + def destroy_query + query_as(:r).delete(:r) + end def validate_node_classes! [from_node, to_node].each do |node| type = from_node == node ? :_from_class : :_to_class type_class = self.class.send(type)