lib/neo4j/active_node/has_n.rb in neo4j-7.2.3 vs lib/neo4j/active_node/has_n.rb in neo4j-8.0.0.alpha.1
- old
+ new
@@ -21,15 +21,12 @@
end
# States:
# Default
def inspect
- if @cached_result
- result_nodes.inspect
- else
- "#<AssociationProxy @query_proxy=#{@query_proxy.inspect}>"
- end
+ formatted_nodes = ::Neo4j::ActiveNode::NodeListFormatter.new(result_nodes)
+ "#<AssociationProxy #{@query_proxy.context} #{formatted_nodes.inspect}>"
end
extend Forwardable
%w(include? empty? count find first last ==).each do |delegated_method|
def_delegator :@enumerable, delegated_method
@@ -210,11 +207,13 @@
record.instance_variable_set('@source_proxy_result_cache', records)
end
end
end
+ # rubocop:disable Metrics/ModuleLength
module ClassMethods
+ # rubocop:enable Style/PredicateName
# rubocop:disable Style/PredicateName
# :nocov:
def has_association?(name)
ActiveSupport::Deprecation.warn 'has_association? is deprecated and may be removed from future releases, use association? instead.', caller
@@ -227,25 +226,22 @@
def association?(name)
!!associations[name.to_sym]
end
+ def parent_associations
+ superclass == Object ? {} : superclass.associations
+ end
+
def associations
- @associations ||= {}
+ (@associations ||= parent_associations.dup)
end
def associations_keys
@associations_keys ||= associations.keys
end
- # make sure the inherited classes inherit the <tt>_decl_rels</tt> hash
- def inherited(klass)
- klass.instance_variable_set(:@associations, associations.clone)
- @associations_keys = klass.associations_keys.clone
- super
- end
-
# For defining an "has many" association on a model. This defines a set of methods on
# your model instances. For instance, if you define the association on a Person model:
#
#
# .. code-block:: ruby
@@ -377,11 +373,11 @@
define_method("#{name}=") do |other_nodes|
association_proxy_cache.clear
clear_deferred_nodes_for_association(name)
- Neo4j::Transaction.run { association_proxy(name).replace_with(other_nodes) }
+ self.class.run_transaction { association_proxy(name).replace_with(other_nodes) }
end
end
def define_has_many_id_methods(name)
define_method_unless_defined("#{name.to_s.singularize}_ids") do
@@ -453,11 +449,11 @@
def define_has_one_setter(name)
define_method("#{name}=") do |other_node|
if persisted?
other_node.save if other_node.respond_to?(:persisted?) && !other_node.persisted?
association_proxy_cache.clear # TODO: Should probably just clear for this association...
- Neo4j::Transaction.run { association_proxy(name).replace_with(other_node) }
+ self.class.run_transaction { association_proxy(name).replace_with(other_node) }
# handle_non_persisted_node(other_node)
else
defer_create(name, other_node, clear: true)
other_node
end
@@ -507,26 +503,35 @@
target_classes_or_nil
end
def default_association_query_proxy
Neo4j::ActiveNode::Query::QueryProxy.new("::#{self.name}".constantize, nil,
- session: neo4j_session, query_proxy: nil, context: "#{self.name}")
+ session: neo4j_session, query_proxy: nil, context: self.name.to_s)
end
def build_association(macro, direction, name, options)
options[:model_class] = options[:model_class].name if options[:model_class] == self
Neo4j::ActiveNode::HasN::Association.new(macro, direction, name, options).tap do |association|
- @associations ||= {}
- @associations[name] = association
+ add_association(name, association)
create_reflection(macro, name, association, self)
end
- associations_keys << name
+ @associations_keys = nil
# Re-raise any exception with added class name and association name to
# make sure error message is helpful
rescue StandardError => e
raise e.class, "#{e.message} (#{self.class}##{name})"
+ end
+
+ def add_association(name, association_object)
+ fail "Association `#{name}` defined for a second time. "\
+ 'Associations can only be defined once' if duplicate_association?(name)
+ associations[name] = association_object
+ end
+
+ def duplicate_association?(name)
+ associations.key?(name) && parent_associations[name] != associations[name]
end
end
end
end