lib/neo4j/active_node/dependent/association_methods.rb in neo4j-4.1.5 vs lib/neo4j/active_node/dependent/association_methods.rb in neo4j-5.0.0.rc.1

- old
+ new

@@ -1,28 +1,46 @@ module Neo4j module ActiveNode module Dependent module AssociationMethods + def validate_dependent(value) + fail ArgumentError, "Invalid dependent value: #{value.inspect}" if not valid_dependent_value?(value) + end + def add_destroy_callbacks(model) return if dependent.nil? - # Bound value for procs - assoc = self - fn = case dependent - when :delete - proc { |o| o.send("#{assoc.name}_query_proxy").delete_all } - when :delete_orphans - proc { |o| o.as(:self).unique_nodes(assoc, :self, :n, :other_rel).query.delete(:n, :other_rel).exec } - when :destroy - proc { |o| o.send("#{assoc.name}_query_proxy").each_for_destruction(o) { |node| node.destroy } } - when :destroy_orphans - proc { |o| o.as(:self).unique_nodes(assoc, :self, :n, :other_rel).each_for_destruction(o) { |node| node.destroy } } - else - fail "Unknown dependent option #{dependent}" - end + model.before_destroy(&method("dependent_#{dependent}_callback")) + rescue NameError + raise "Unknown dependent option #{dependent}" + end - model.before_destroy fn + private + + def valid_dependent_value?(value) + return true if value.nil? + + self.respond_to?("dependent_#{value}_callback", true) end + + # Callback methods + def dependent_delete_callback(object) + object.association_query_proxy(name).delete_all + end + + def dependent_delete_orphans_callback(object) + object.as(:self).unique_nodes(self, :self, :n, :other_rel).query.delete(:n, :other_rel).exec + end + + def dependent_destroy_callback(object) + object.association_query_proxy(name).each_for_destruction(object, &:destroy) + end + + def dependent_destroy_orphans_callback(object) + object.as(:self).unique_nodes(self, :self, :n, :other_rel).each_for_destruction(object, &:destroy) + end + + # End callback methods end end end end