lib/neo4j/active_node/id_property.rb in neo4j-5.0.15 vs lib/neo4j/active_node/id_property.rb in neo4j-5.1.0.rc.1
- old
+ new
@@ -32,12 +32,12 @@
# end
# end
#
module IdProperty
extend ActiveSupport::Concern
+ include Accessor
-
module TypeMethods
def define_id_methods(clazz, name, conf)
validate_conf!(conf)
if conf[:on]
@@ -88,11 +88,11 @@
default_property :#{name} do
::SecureRandom.uuid
end
def #{name}
- default_property :#{name}
+ default_property_value
end
alias_method :id, :#{name}
), __FILE__, __LINE__)
end
@@ -100,16 +100,16 @@
def define_custom_method(clazz, name, on)
clear_methods(clazz, name)
clazz.module_eval(%{
default_property :#{name} do |instance|
- raise "Specifying custom id_property #{name} on none existing method #{on}" unless instance.respond_to?(:#{on})
- instance.#{on}
+ raise "Specifying custom id_property #{name} on non-existent method #{on}" unless instance.respond_to?(:#{on})
+ instance.#{on}
end
def #{name}
- default_property :#{name}
+ default_property_value
end
alias_method :id, :#{name}
}, __FILE__, __LINE__)
end
@@ -122,10 +122,12 @@
extend self
end
module ClassMethods
+ attr_accessor :manual_id_property
+
def find_by_neo_id(id)
Neo4j::Node.load(id)
end
def find_by_id(id)
@@ -135,16 +137,18 @@
def find_by_ids(ids)
self.where(id_property_name => ids).to_a
end
def id_property(name, conf = {})
- id_property_constraint(name)
- @id_property_info = {name: name, type: conf}
- TypeMethods.define_id_methods(self, name, conf)
- constraint name, type: :unique unless conf[:constraint] == false
+ self.manual_id_property = true
+ Neo4j::Session.on_session_available do |_|
+ @id_property_info = {name: name, type: conf}
+ TypeMethods.define_id_methods(self, name, conf)
+ constraint(name, type: :unique) unless conf[:constraint] == false
- self.define_singleton_method(:find_by_id) { |key| self.where(name => key).first }
+ self.define_singleton_method(:find_by_id) { |key| self.where(name => key).first }
+ end
end
# rubocop:disable Style/PredicateName
def has_id_property?
ActiveSupport::Deprecation.warn 'has_id_property? is deprecated and may be removed from future releases, use id_property? instead.', caller
@@ -163,18 +167,22 @@
def id_property_name
id_property_info[:name]
end
+ def manual_id_property?
+ !!manual_id_property
+ end
+
alias_method :primary_key, :id_property_name
private
def id_property_constraint(name)
if id_property?
unless mapped_label.uniqueness_constraints[:property_keys].include?([name])
# Neo4j Embedded throws a crazy error when a constraint can't be dropped
- drop_constraint(id_property_name, type: :unique)
+ drop_constraint(id_property_name, type: :unique) if constraint?(mapped_label_name, id_property_name)
end
end
rescue Neo4j::Server::CypherResponse::ResponseError, Java::OrgNeo4jCypher::CypherExecutionException
end
end