lib/neo4j/active_rel/persistence.rb in neo4j-3.0.0.rc.3 vs lib/neo4j/active_rel/persistence.rb in neo4j-3.0.0.rc.4

- old
+ new

@@ -1,19 +1,12 @@ module Neo4j::ActiveRel module Persistence extend ActiveSupport::Concern include Neo4j::Shared::Persistence - class RelInvalidError < RuntimeError - attr_reader :record + class RelInvalidError < RuntimeError; end - def initialize(record) - @record = record - super(@record.errors.full_messages.join(", ")) - end - end - class ModelClassInvalidError < RuntimeError; end def save(*) update_magic_properties create_or_update @@ -29,11 +22,11 @@ confirm_node_classes create_magic_properties set_timestamps properties = convert_properties_to :db, props rel = _create_rel(from_node, to_node, properties) - init_on_load(rel, to_node, from_node, @rel_type) + init_on_load(rel, from_node, to_node, @rel_type) true end module ClassMethods @@ -59,21 +52,37 @@ private def confirm_node_classes [from_node, to_node].each do |node| - check = from_node == node ? :_from_class : :_to_class - next if self.class.send(check) == :any - unless self.class.send(check) == node.class - raise ModelClassInvalidError, "Node class was #{node.class}, expected #{self.class.send(check)}" + type = from_node == node ? :_from_class : :_to_class + next if allows_any_class?(type) + unless class_as_constant(type) == node.class + raise ModelClassInvalidError, "Node class was #{node.class}, expected #{self.class.send(type)}" end end end def _create_rel(from_node, to_node, *args) props = self.class.default_property_values(self) props.merge!(args[0]) if args[0].is_a?(Hash) set_classname(props) from_node.create_rel(type, to_node, props) end + + def class_as_constant(type) + given_class = self.class.send(type) + case + when given_class.is_a?(String) + given_class.constantize + when given_class.is_a?(Symbol) + given_class.to_s.constantize + else + given_class + end + end + + def allows_any_class?(type) + self.class.send(type) == :any || self.class.send(type) == false + end end -end +end \ No newline at end of file