lib/neo4j/active_node/id_property.rb in neo4j-4.1.5 vs lib/neo4j/active_node/id_property.rb in neo4j-5.0.0.rc.1
- old
+ new
@@ -18,31 +18,56 @@
# def title_to_url
# self.title.urlize # uses https://github.com/cheef/string-urlize gem
# end
# end
#
+ # @example using already exsting ids that you don't want a constraint added to
+ # class Person
+ # include Neo4j::ActiveNode
+ # property :title
+ # validates :title, :presence => true
+ # id_property :id, on: :id_builder, constraint: false
+ #
+ # def id_builder
+ # # only need to fill this out if you're gonna write to the db
+ # end
+ # end
+ #
module IdProperty
extend ActiveSupport::Concern
module TypeMethods
def define_id_methods(clazz, name, conf)
- fail "Expected a Hash, got #{conf.class} (#{conf}) for id_property" unless conf.is_a?(Hash)
+ validate_conf!(conf)
+
if conf[:on]
define_custom_method(clazz, name, conf[:on])
elsif conf[:auto]
- fail "only :uuid auto id_property allowed, got #{conf[:auto]}" unless conf[:auto] == :uuid
define_uuid_method(clazz, name)
elsif conf.empty?
define_property_method(clazz, name)
- else
- fail "Illegal value #{conf.inspect} for id_property, expected :on or :auto"
end
end
private
+ def validate_conf!(conf)
+ fail "Expected a Hash, got #{conf.class} (#{conf}) for id_property" if !conf.is_a?(Hash)
+
+ return if conf[:on]
+
+ if conf[:auto]
+ fail "only :uuid auto id_property allowed, got #{conf[:auto]}" if conf[:auto] != :uuid
+ return
+ end
+
+ return if conf.empty?
+
+ fail "Illegal value #{conf.inspect} for id_property, expected :on or :auto"
+ end
+
def define_property_method(clazz, name)
clear_methods(clazz, name)
clazz.module_eval(%(
def id
@@ -88,21 +113,12 @@
alias_method :id, :#{name}
}, __FILE__, __LINE__)
end
def clear_methods(clazz, name)
- if clazz.method_defined?(name)
- clazz.module_eval(%(
- undef_method :#{name}
- ), __FILE__, __LINE__)
- end
-
- if clazz.attribute_names.include?(name.to_s)
- clazz.module_eval(%(
- undef_property :#{name}
- ), __FILE__, __LINE__)
- end
+ clazz.module_eval(%(undef_method :#{name}), __FILE__, __LINE__) if clazz.method_defined?(name)
+ clazz.module_eval(%(undef_property :#{name}), __FILE__, __LINE__) if clazz.attribute_names.include?(name.to_s)
end
extend self
end
@@ -119,26 +135,16 @@
def find_by_ids(ids)
self.where(id_property_name => ids).to_a
end
def id_property(name, conf = {})
- begin
- if id_property?
- unless mapped_label.uniqueness_constraints[:property_keys].include?([name])
- drop_constraint(id_property_name, type: :unique)
- end
- end
- rescue Neo4j::Server::CypherResponse::ResponseError
- end
-
+ id_property_constraint(name)
@id_property_info = {name: name, type: conf}
TypeMethods.define_id_methods(self, name, conf)
- constraint name, type: :unique
+ constraint name, type: :unique unless conf[:constraint] == false
- self.define_singleton_method(:find_by_id) do |key|
- self.where(name => key).first
- end
+ self.define_singleton_method(:find_by_id) { |key| self.where(name => key).first }
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
@@ -158,8 +164,20 @@
def id_property_name
id_property_info[:name]
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)
+ end
+ end
+ rescue Neo4j::Server::CypherResponse::ResponseError, Java::OrgNeo4jCypher::CypherExecutionException
+ end
end
end
end