lib/neo4j/active_node/has_n.rb in neo4j-9.1.8 vs lib/neo4j/active_node/has_n.rb in neo4j-9.2.0
- old
+ new
@@ -362,11 +362,11 @@
#
def has_many(direction, name, options = {}) # rubocop:disable Style/PredicateName
name = name.to_sym
build_association(:has_many, direction, name, options)
- define_has_many_methods(name)
+ define_has_many_methods(name, options)
end
# For defining an "has one" 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:
#
@@ -379,31 +379,37 @@
#
def has_one(direction, name, options = {}) # rubocop:disable Style/PredicateName
name = name.to_sym
build_association(:has_one, direction, name, options)
- define_has_one_methods(name)
+ define_has_one_methods(name, options)
end
private
- def define_has_many_methods(name)
+ def define_has_many_methods(name, association_options)
+ default_options = association_options.slice(:labels)
+
define_method(name) do |node = nil, rel = nil, options = {}|
# return [].freeze unless self._persisted_obj
options, node = node, nil if node.is_a?(Hash)
+ options = default_options.merge(options)
+
association_proxy(name, {node: node, rel: rel, source_object: self, labels: options[:labels]}.merge!(options))
end
define_has_many_setter(name)
define_has_many_id_methods(name)
define_class_method(name) do |node = nil, rel = nil, options = {}|
options, node = node, nil if node.is_a?(Hash)
+ options = default_options.merge(options)
+
association_proxy(name, {node: node, rel: rel, labels: options[:labels]}.merge!(options))
end
end
def define_has_many_setter(name)
@@ -433,20 +439,24 @@
def define_method_unless_defined(method_name, &block)
define_method(method_name, block) unless method_defined?(method_name)
end
- def define_has_one_methods(name)
- define_has_one_getter(name)
+ def define_has_one_methods(name, association_options)
+ default_options = association_options.slice(:labels)
+ define_has_one_getter(name, default_options)
+
define_has_one_setter(name)
define_has_one_id_methods(name)
define_class_method(name) do |node = nil, rel = nil, options = {}|
options, node = node, nil if node.is_a?(Hash)
+ options = default_options.merge(options)
+
association_proxy(name, {node: node, rel: rel, labels: options[:labels]}.merge!(options))
end
end
def define_has_one_id_methods(name)
@@ -461,12 +471,14 @@
define_method_unless_defined("#{name}_neo_id") do
association_proxy(name).pluck(:neo_id).first
end
end
- def define_has_one_getter(name)
+ def define_has_one_getter(name, default_options)
define_method(name) do |node = nil, rel = nil, options = {}|
options, node = node, nil if node.is_a?(Hash)
+
+ options = default_options.merge(options)
association_proxy = association_proxy(name, {node: node, rel: rel}.merge!(options))
# Return all results if options[:chainable] == true or a variable-length relationship length was given
if options[:chainable] || (options[:rel_length] && !options[:rel_length].is_a?(Integer))